#!/bin/sh
# [--- --  -   -                                                             ]
# [ Program Name: glFtpD Installer                                           ]
# [ Program  Ver: 1.10                                                       ]
# [ Purpose:      To install glFtpD                                          ]
# [ Dependancies: glFtpD dist. available from http://glftpd.deepwell.com     ]
# [ Notes:        none                                                       ]
# [ Installation: cd into the untarred directory, then run sh ./glinstall.sh ]
# [                                                             -   -  -- ---]
# [--- --  -   -                                                             ]
# [ CHANGELOG                                                                ]
# [------                                                                    ]
# [ v1.10 - ROMRacer                                                         ]
# [  * fixed lib copy code.. again.. last time                               ]
# [  + added passing location of distro to script                            ]
# [  * modified bin copy code                                                ]
# [  * fixed the check for bin/reset already being in crontab                ]
# [  + added choosing the name of your conf file                             ]
# [  + added check for bins the script needs before it runs                  ]
# [  + added support for / as default home directory                         ]
# [------                                                                    ]
# [ v1.09 - ROMRacer                                                         ]
# [  + added default instdir                                                 ]
# [  + added /bin/reset to crontab                                           ]
# [  * modified lib copy code.  might work on bsd now (Lord Detain)          ]
# [  * fixed a rare tcpd bug                                                 ]
# [------                                                                    ]
# [ v1.08 - ROMRacer                                                         ]
# [  * fixed lib copy code, again.                                           ]
# [  * brought it a little closer to full BSD compat                         ]
# [------                                                                    ]
# [ v1.07 - ROMRacer                                                         ]
# [  * modified lib copy code.  detects bsd now.                             ]
# [  * cleaned up the code                                                   ]
# [------                                                                    ]
# [ v1.06 - ROMRacer                                                         ]
# [  + added dir selection                                                   ]
# [  * fixed error code problems.. could be run as part of another install   ]
# [    exit 1 = failure; exit 0 = all is good                                ]
# [  * modified lib copy code.. might break bsd compat.. let me know         ]
# [  * fixed more tcpd code                                                  ]
# [  * modified some bin calling code.. if compat breaks let me know         ]
# [------                                                                    ]
# [ v1.05 and below                                                          ]
# [  + BSD Detection and tcpd fixed by JabberA                               ]
# [  + TCPD Fixed by inCIDent                                                ]
# [  + Major bugs fixed by VR_Dude                                           ]
# [------                                                                    ]
# [ Original design idea by Archimede                                        ]
# [                                                             -   -  -- ---]
ver="v1.10"
progs="sh kill cat echo grep unzip ls bash wc find"
clear
echo "[-----------------------------------------------]"
echo "[       glFtpD Installation script $ver        ]"
echo "[       --------------------------------        ]"
echo "[  This is a beta and may not work, please use  ]"
echo "[  at your own peril                            ]"
echo "[                    -ROMRacer/romracer@isos.net]"
echo "[-----------------------------------------------]"
echo "Press any key to begin script..."
read $NULL

echo 
echo -n "Checking for whoami..."
whoami=`which whoami`
if [ -x "$whoami" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for rm..."
rm=`which rm`
if [ -x "$rm" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for mkdir..."
mkdir=`which mkdir`
if [ -x "$mkdir" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for cp..."
cp=`which cp`
if [ -x "$cp" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for mknod..."
mknod=`which mknod`
if [ -x "$mknod" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for sh..."
sh=`which sh`
if [ -x "$sh" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for kill..."
kill=`which kill`
if [ -x "$kill" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for grep..."
grep=`which grep`
if [ -x "$grep" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for cat..."
cat=`which cat`
if [ -x "$cat" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for echo..."
echo=`which echo`
if [ -x "$echo" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for unzip..."
unzip=`which unzip`
if [ -x "$unzip" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for ls..."
ls=`which ls`
if [ -x "$ls" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for bash..."
bash=`which bash`
if [ -x "$bash" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for wc..."
wc=`which wc`
if [ -x "$wc" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for uname..."
uname=`which uname`
if [ -x "$uname" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for sed..."
sed=`which sed`
if [ -x "$sed" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for ldd..."
ldd=`which ldd`
if [ -x "$ldd" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for cut..."
cut=`which cut`
if [ -x "$cut" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for sort..."
sort=`which sort`
if [ -x "$sort" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for uniq..."
uniq=`which uniq`
if [ -x "$uniq" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for tr..."
tr=`which tr`
if [ -x "$tr" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for chmod..."
chmod=`which chmod`
if [ -x "$chmod" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for killall..."
killall=`which killall`
if [ -x "$killall" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for crontab..."
crontab=`which crontab`
if [ -x "$crontab" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for chroot..."
chroot=`which chroot`
if [ -x "$chroot" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for basename..."
basename=`which basename`
if [ -x "$basename" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi
echo -n "Checking for find..."
find=`which find`
if [ -x "$find" ] ; then
  echo "found"
else
  echo "failure.. script aborted.."
  exit 1
fi

echo
echo -n "Doing sanity checks..."
if [ ! $("$whoami") = "root" ] ; then
  echo "failure"
  echo "root required to install glFtpD"
  exit 1
fi
if [ "$1" = "" ] ; then
  distdir=$PWD
else
  distdir="$1"
fi
if [ ! -r $distdir/bin/glftpd ] ; then
  echo "failure"
  echo "Cannot find a glFtpD distribution in $distdir"
  exit 1
fi
if [ -x "/usr/libexec/tcpd" ] ; then
  TCPD=/usr/libexec/tcpd
elif [ -x "/usr/sbin/tcpd" ] ; then
  TCPD=/usr/sbin/tcpd
else
  usetcpd="0"
fi
echo "done"

echo
echo "[- -                                          ]"
echo "[   Where would you like to install glFtpD?   ]"
echo "[                                          - -]"
echo -n "Directory [/glftpd]: "
read instdir
if [ "$instdir" = "" ] ; then
   instdir="/glftpd"
   insecure="N"
elif [ "$instdir" = "/" ] ; then
  echo "[- -                                          ]"
  echo "[           *** ./. WARNING ./. ***           ]"
  echo "[      You have chosen / as your glftpd       ]"
  echo "[  installation directory.  THIS IS INSECURE  ]"
  echo "[           *** ./. WARNING ./. ***           ]"
  echo "[                                          - -]"
  echo -n "Continue [y/n]: "
  read insecure
  if [ "$insecure" = "y" ] ; then
    insecure="Y"
  fi
  if [ "$insecure" = "Y" ] ; then
    echo "Doing this causes glftpd show your entire tree"
    echo "to others but doesn't force you to remount drives"
    echo "under /glftpd/site.  Several changes will still"
    echo "need to be made.  /glftpd will need to be removed"
    echo "for this to work, if it exists."
    instdir="/glftpd"
  else
    echo "User error.  Install aborted."
    exit 1
  fi
else
  insecure="N"
fi
if [ -d $instdir ] ; then
  echo -n "$instdir exists.  Remove [y/n]: "
  read abort
  if [ "$abort" = "y" ] ; then
    abort="Y"
  fi
  if [ "$abort" = "Y" ] ; then
    "$rm" -Rf $instdir/
  else
    echo
    echo "Install dir caused abort.."
    exit 1
  fi
fi

echo
echo "[x] Installing glFtpD on "`uname -s`" Kernel "`uname -r`" on a "`uname -m`
echo "[x] Source Directory: $distdir"
echo "[x] Installation Directory: $instdir/"
echo
echo "[- -                                         - -]"
echo "[Press return to start to install or ^C to abort]"
echo "[                                            - -]"
read $NULL

echo -n "Making directory..."
if [ -d $instdir ]; then
  echo "failure"
  echo "Directory error.  E-mail romracer@isos.net or find me on iRC"
  exit 1
else
  "$mkdir" $instdir/
  echo "done"
fi

echo -n "Moving distribution to $instdir/..."
cd $distdir/
"$cp" -Rf * $instdir/
echo "done"

if [ ! $insecure = "Y" ] ; then
  echo -n "Making psuedo devices..."
  cd $instdir/dev
  "$mknod" -m666 null c 1 3
  "$mknod" -m666 zero c 1 5
  echo "done"
fi

echo -n "Copying binaries..."
for i in $progs ; do
  if [ ! $insecure = "Y" ] ; then
    "$cp" -f `which $i` $instdir/bin/
  else
    "$cp" -f `which $i` /bin/
  fi
done
echo "done"

if [ ! $insecure = "Y" ] ; then
  echo -n "Copying libraries..."
  if [ $("$uname" -s | "$sed" "s/.*BSD.*/BSD/") = "BSD" ] ; then
    bsd="1"
  elif [ $("$uname" -s | "$sed" 's/.*bsd.*/BSD/') = "BSD" ] ; then
    bsd="1"
  else
    bsd="0"
  fi
  if [ $bsd = "1" ] ; then
    for h in $(\
      for i in $instdir/bin/* ; do
        if [ -f $i ] ; then
          OLDIFS="$IFS"
          IFS="
"
          for j in $("$ldd" $i | "$grep" =) ; do
            lib=$(echo $j | "$cut" -d'>' -f2 | "$cut" -d' ' -f2)
            echo "$lib"
          done
          IFS="$OLDIFS"
        fi
      done | "$sort" | "$uniq") ; do
      "$cp" -f $h $instdir/lib
    done
    echo "done"
    echo "Make sure the BSD libs copied correctly.."
  else
    for i in `"$ldd" /lib/* 2>/dev/null | "$grep" =\> | "$sort" | "$uniq" | "$tr" -s " " | "$cut" -f 3 -d " "` ; do
      "$cp" -f $i $instdir/lib
    done
    cd $instdir
    "$mkdir" usr/
    "$mkdir" usr/lib/
    for i in `"$ldd" /usr/lib/* 2>/dev/null | "$grep" =\> | "$sort" | "$uniq" | "$tr" -s " " | "$cut" -f 3 -d " "` ; do
      "$cp" -f $i $instdir/usr/lib
    done
    echo "done"
    echo "Its up to you to ensure the libs copied to $instdir/lib correctly.."
  fi
fi

echo -n "Configuring dependencies..."
if [ $insecure = "Y" ] ; then
  if [ -x /sbin/ldconfig ] ; then
    /sbin/ldconfig 2> /dev/null
    echo "done"
  elif [ -x /bin/ldconfig ] ; then
    /bin/ldconfig 2> /dev/null
    echo "done"
  else
    echo "failure"
    echo "Can't find ldconfig.. Find it and run it.."
  fi
else
  if [ -x /sbin/ldconfig ] ; then
    "$cp" -f /sbin/ldconfig $instdir/bin
    "$chroot" $instdir/ /bin/ldconfig 2> /dev/null
    echo "done"
  elif [ -x /bin/ldconfig ] ; then
    "$cp" -f /bin/ldconfig $instdir/bin
    "$chroot" $instdir/ /bin/ldconfig 2> /dev/null
    echo "done"
  else
    echo "failure"
    echo "Can't find ldconfig.. Find it and copy it to $instdir/bin"
    echo "Then run chroot $instdir/ /bin/ldconfig"
  fi
fi

echo -n "Setting permissions..."
"$chmod" -R 755 $instdir/
"$chmod" -R 777 $instdir/site/
echo "done"

echo "[- -                                          ]"
echo "[  What do you want to name your conf file ?  ]"
echo "[                                          - -]"
echo -n "Name [glftpd.conf]: "
read conffile
if [ "$conffile" = "" ] ; then
  conffile="glftpd.conf"
else
  conffile=$("$basename" $conffile)
fi
conffile="/etc/$conffile"
if [ -f "$conffile" ] ; then
  echo -n "$conffile exists.  Overwrite [y/n]: "
  read confdestroy
  if [ "$confdestroy" = "y" ] ; then
    confdestroy="Y"
  fi
  if [ "$confdestroy" = "Y" ] ; then
    if [ $insecure = "Y" ] ; then
      "$cp" -f $instdir/glftpd.conf-ROOT "$conffile"
    else
      "$cp" -f $instdir/glftpd.conf "$conffile"
    fi
  fi
else
  "$cp" -f $instdir/glftpd.conf "$conffile"
fi


echo -n "Configuring inetd.conf..."
if `"$grep" ^glftpd /etc/inetd.conf > /dev/null` ; then
  echo "failure"
  echo "inetd.conf entry already exists, skipping"
else 
  if [ "$usetcpd" = "0" ] ; then
    echo "glftpd	stream	tcp	nowait	root	$instdir/bin/glftpd	glftpd -l -o -i -r $conffile" >> /etc/inetd.conf
  else
    echo "glftpd	stream	tcp	nowait	root	$TCPD	$instdir/bin/glftpd -l -o -i -r $conffile" >> /etc/inetd.conf
  fi
  echo "done"
fi

echo -n "Configuring services..."
if `"$grep" ^glftpd /etc/services > /dev/null` ; then
  echo "failure"
  echo "services entry already exists, skipping"
else   
  echo
  echo "[- -                                          ]"
  echo "[ What port would you like glFtpD to run on ? ]"
  echo "[                                          - -]"
  echo -n "Port: "
  read port
  echo "glftpd	$port/tcp" >> /etc/services
  echo "done"
fi

echo -n "Restarting inetd..."
"$killall" -HUP inetd
echo "done"

echo -n "Adding $instdir/bin/reset to crontab..."
"$crontab" -l > tempfile
if `"$grep" -i --regexp=$instdir/bin/reset[[:space:]]-r[[:space:]]$conffile tempfile > /dev/null` ; then
  echo "failure"
  echo "$instdir/bin/reset -r $conffile already in crontab, skipping"
else
  echo "0 0 * * *       $instdir/bin/reset -r $conffile" >> tempfile
  "$crontab" tempfile
  echo "done"
fi
"$rm" -f tempfile

if [ $insecure = "Y" ] ; then
  echo -n "Modifying glftpd's passwd file..."
  $rm $instdir/etc/passwd
  echo "glftpd:6uwHOjJ3grid6:0:0:0:/:/bin/false" > $instdir/etc/passwd
  $chmod 755 $instdir/etc/passwd
  echo "done"
  echo "Make sure default_homedir in your conf file is set to /"
fi

echo
echo
echo "[-----------------------------------------------]"
echo "[       glFtpD Installation script $ver        ]"
echo "[       --------------------------------        ]"
echo "[                                               ]"
echo "[     glFtpD has been sucessfully installed.    ]"
echo "[      Please refer to glftpd.docs for help     ]"
echo "[                                               ]"
echo "[  You will now be presented with an editor to  ]"
echo "[   configure the glFtpD configuration file.    ]"
echo "[                                               ]"
echo "[   For more information about glFtpD, visit    ]"
echo "[  http://glftpd.deepwell.com or #glftpd@EFnet  ]"
echo "[                                               ]" 
echo "[                    -ROMRacer/romracer@isos.net]"
echo "[-----------------------------------------------]"
echo 
echo "[- - - -                               - - - - -]"
echo "[       Press return to start the editor        ]"
echo "[- - - -                               - - - - -]"
read $NULL

if [ ! -z $EDITOR ] ; then
  $EDITOR "$conffile"
elif [ -x `which pico` ] ; then
  `which pico` -w "$conffile"
elif [ -x `which joe` ] ; then
  `which joe` "$conffile"
elif [ -x `which vi`] ; then
  `which vi` "$conffile"
else
  echo
  echo "Could not find a usable file editor"
  echo "Please locate one yourself and edit /etc/glftpd.conf"
fi
