#!/bin/sh
#
# NetPlug's cool 'installer' (unix)
#
# $Id: instNP,v 1.5 1996/10/03 21:04:30 dl Exp dl $

wishes="wish8.0 wish4.2 wish4.1 wishx wish"
plugdir=/usr/local/lib

# utility function
createdir() 
{
if [ ! -d $1 ] ; then 
  echo "Making you $1 dir..."
  mkdir $1
else
  echo "You already have $1 dir, good !..."
fi
}

# some os don't have cp -f some needs one...
mycp()
{
dest=$1
shift
for F do 
  ddest=$dest/$F
  if [ -f $ddest ]; then
    echo "   removing old $ddest";
    rm -f $ddest
  fi
  echo "   copying $F as $ddest";
  cp $F $ddest
done
}


echo "Searching for Tk 'wish' (4.1 or better 8.0 or 4.2) in your path..."
for prog in $wishes; do 
  IFS=: ; for dir in $PATH; do 
    if [ -x $dir/$prog ] ; then
      WISH=$dir/$prog
      break 2
    fi
  done
done
if [ -z "$WISH" ] ; then
  cat << _EOF1_
*** Sorry, I can't find any of the Tk interpreters ($wishes)
    in your path ($PATH) !
*** Fix your path and/or install Tk8.0 or do a manual Install !
_EOF1_
  exit 1
fi
echo "  -> Found your wish as '$WISH'"

echo "Creating 'netplug' with correct wish..."
( echo "#! $WISH" ; cat netplug.tcl ) > netplug
chmod 755 netplug
echo "  -> done"

BINDIR=`dirname $WISH`

createdir $HOME/.netplug;

cat << _EOF2_

*** Good, now, Do you want a system or personal install ?

  + system install will put netplug executable with wish (in $BINDIR)
    and plugins in $plugdir/netplug
  + personal install will let you copy netplug executable where you want
    and will put plugins in your home dir .netplug

System installation ? (default yes, enter 'n' for personal install)
_EOF2_
read answer
if [ "$answer" = "n" ] ; then
 echo "  -> personal install !"
 echo "Copying 'zero.dat' to your .netplug dir"
 mycp $HOME/.netplug zero.dat 
 createdir $HOME/.netplug/plugins;
 echo "Copying all the plugins to ~/.netplug/plugins..."
 mycp $HOME/.netplug plugins/*.pli 
 echo "Enter the 'netplug' destination directory (default $BINDIR)"
 read bindir
 if [ -z "$bindir" ] ; then bindir=$BINDIR; fi
 echo "Copying netplug to $bindir"
 mycp $bindir netplug 
else
 echo "  -> system install !"
 createdir $plugdir;
 createdir $plugdir/netplug;
 createdir $plugdir/netplug/plugins;
 echo "Copying 'zero.dat' to the $plugdir/netplug dir"
 mycp $plugdir/netplug zero.dat
 echo "Copying all the plugins to the $plugdir/netplug/plugins dir"
 mycp $plugdir/netplug plugins/*.pli
 echo "Copying netplug to $BINDIR"
 mycp $BINDIR netplug
fi
cat << _EOF3_

*** INSTALLATION COMPLETED SUCCESSFULLY ! CONGRATS !"
    (eventually 'rehash' and launch 'netplug' to check !)

_EOF3_
