#! /bin/sh
# nuke-stale-cvs-data --- clean up after cvs server

# Author: Noah Friedman <friedman@splode.com>
# Created: 1997-01-29
# Public domain.

# $Id: cvs-nuke-stale-server-data,v 1.5 1999/06/17 06:31:43 friedman Exp $

# Commentary:

# Looks for /tmp/cvs-serv* data left from CVS server processes which exited
# without reaping those temporary files.
#
# Requires a SVR4 style `ps' command or /proc filesystem.

# Code:

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options}

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
-h, --help                   You're looking at it.
-f, --force                  Do not query before removing stale data.
-q, --quick                  Do not summarize data sizes, which can be slow.
"

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
force=
quick=

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --d* )
      debug=t
      shift
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -f | --force | --f* )
      force=t
      shift
     ;;
    -q | --quick | --q* )
      quick=t
      shift
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac

PATH="/bin:$PATH"
export PATH

prefix=/tmp/cvs-serv

foo=`echo -n test`
case "$foo" in
  "-n test" ) n=   c='\c' ;;
  * )         n=-n c=     ;;
esac

for pid in `ls -1d ${prefix}* 2> /dev/null | sed -e "s=$prefix=="`; do
  dir="$prefix$pid"

  if [ -d /proc ]; then
    if ls -d /proc/$pid > /dev/null 2>&1 ; then
     continue
    fi
  elif ps -p $pid > /dev/null; then
    continue
  fi

  test -d "$dir" || continue

  echo $n "Stale CVS data directory: $c"
  {
    if test "$quick" = "t" ; then
      echo $n "?? $c"
    else
      blocks=`du -sk "$dir" | sed -e 's/[^0-9].*//'`
      echo $n "$blocks $c"
    fi
    ls -ld "$dir"
  } | awk '{ print  $4, $1 "k", $7, $8, $9, $10 }'

  case "$force" in
    t ) rm -rf "$prefix$pid" ;;
    * )
      echo $n "Delete recursively (y/n)? $c"
      read ans

      case "$ans" in
        y* | Y* )
          echo rm -rf "$prefix$pid"
          rm -rf "$prefix$pid"
         ;;
      esac
      echo
     ;;
  esac
done

# nuke-stale-cvs-data ends here
