#!/bin/sh
# $Header: /cvsroot/sadm/cfm-1/sbin/slrm,v 1.1 2000/03/31 22:56:33 dmetz Exp $
# $crtd:  by  Derald Metzger  on  17may94 $
# $cmnt:  Remove symlinks from a dir.
#           Copyright (c) 1998 1999  Derald Metzger 
#  This file is part of the cfm pkg (GPL).
# $

actflg=1
base=`basename $0`

usage="
NAME
  slrm - remove symlinks matching a pattern from target dir
SYNOPSIS
  $base tgt_dir grep_pat
DESCR
  Remove symlinks which match regular expression grep_pat from tgt_dir.
  Symlinks that pass thru the following pipe are removed:
    ls -l | grep \$grep_pat
  See slmk to generate a new set of symlinks for an application bin dir.
OPTIONS
   tgt_dir  - dir to remove symlinks from.
   grep_pat - grep arg used to filter the long dir listing of tgt_dir
              for symlinks to be removed.
WARNING
  This cmd is dangerous. It can save a lot of work if used carefully.
  It can cause a lot of work if used carelessly.
  NOTE:  Preliminary use of the -n option is strongly advised.
ALSO
  See slmk(8), grep(1)
"
case `uname` in
  Linux)  echo=/bin/echo ;;
  SunOS)  echo=/usr/5bin/echo ;;
  unicos) echo=/bin/echo ;;
  *) echo "## $base: unsupported operating system $usage"; exit 1 ;;
esac

#   Get possible opts
OPTS=""
while getopts n c
do
  case $c in
    n) actflg=0 ;;
    \?) $echo "$usage"; exit 2; ;;
  esac
done
shift `expr $OPTIND - 1`

if [ $# -lt 2 ]; then $echo "### $base: insufficient args $usage"; exit 3; fi

tgt_dir=$1
grep_str=$2
cd $tgt_dir
if [ $? -ne 0 ]; then $echo "## Can't access tgt_dir. $usage"; exit 4; fi

for i in `ls`; do
  [ ! -L $i ] && continue
  ls -l $i | grep $grep_str | cut -c15- || continue
  [ $actflg -eq 1 ] && rm $i
done
