#!/bin/sh
# $Id: slmk,v 1.1 2000/03/31 22:56:33 dmetz Exp $
# $crtd:  by Derald Metzger  on  940410 $
# $cmnt:
#   Place a symlinks in current dir for files in a target-dir.
#           Copyright (c) 1998 1999  Derald Metzger 
#  This file is part of the cfm pkg (GPL).
# $

actflg=1
base=`basename $0`
dirflg=0
filflg=0
ln_tgt=
ln_pfx=
osv=`uname`-`uname -r | cut -c1`
pkginst=

usage="
NAME
  slmk - symlink make
SYNOPSIS
  $base [-dfnpt] tgt_dir
DESCR
  Create/replace symlinks in current dir for all executables in a tgt_dir.
  See cmd \`slrm' to remove all of a package's symlinks from a dir.
  Note that an executable that is not accessable, eg not mounted, won't be
  recognized as an executable.
WARNING:
  This cmd is dangerous. It can save you a lot of work if you are careful,
  or create a lot to undo if you aren't.
OPTIONS
    -d  do dirs. By default symlinks are not created for dirs.
    -f  do non-executable files.
        By default symlinks are created only for executable files.
        You'll need this for man pages.
    -n  no change. Just report what would be done, don't do it.
    -p  symlink name prefix, eg a \`g' for gnu cmds.
    -t  symlink target. By default the executable file,
        but frequently a wrapper script in the current dir.
ALSO
  See slrm
"

# Note: `test -x' doesn't work for root on some systems.
case $osv in
  unicos-*) echo=/bin/echo; testx=test-x ;;
  SunOS-4) echo=/usr/5bin/echo; testx=test-x ;;
  SunOS-5) echo=/usr/bin/echo; testx="test -x" ;;
  Linux-2|IRIX*) echo=/bin/echo; testx="test -x" ;;
  *) echo "### $base: unsupported operating system $usage"; exit 1
esac

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

while getopts dfnp:t: c
do
  case $c in
    d) dirflg=1 ;;
    f) filflg=1 ;;
    n) actflg=0 ;;
    p) ln_pfx=$OPTARG ;;
    t) ln_tgt=$OPTARG ;;
    \?) $echo "$usage"; exit 2; ;;
  esac
done

shift `expr $OPTIND - 1`

tgt_dir=$1
for i in `ls $tgt_dir`
do
  tgt_fil=$tgt_dir/$i
# # Don't do dirs unless dirflg is set
  [ -d $tgt_fil ] && [ $dirflg != 1 ] && continue
# # Don't do non-executables unless filflg is True.
  $testx $tgt_fil
  [ $? -ne 0 ] && [ $filflg != 1 ] && continue
  tgt=$tgt_fil
# # If an alternate target is specified use it.
  [ -n "$ln_tgt" ] && tgt=$ln_tgt
  echo "## replacing-creating $ln_pfx$i -> $tgt"
  [ $actflg != 1 ] && continue
  rm -f $ln_pfx$i
  ln -s $tgt $ln_pfx$i
  [ -z "$pkginst" ] && continue
  installf $pkginst `pwd`/$ln_pfx$i=$tgt s
  installf -f $pkginst
done
