#!/bin/sh

groupadd >/dev/null 2>&1

if [ $? -eq 127 ] ; then
   echo
   echo "Could not find groupadd utility."
   echo
   echo "Read the README.DOC file to install Secure Locate manually."
   echo
   exit 1
fi

user=`id -un`

instdir="/usr/bin"
echo
echo "Copying slocate to $instdir"
install -m 0755 slocate /usr/bin
groupadd slocate

CWD="`pwd`"

cd $instdir
echo
echo "Changing permisions on slocate"
chown $user:slocate slocate
chmod g+s ./slocate
cd $CWD
echo "Making Database Directory"
install -d -m 0750 /var/lib/slocate
chown $user:slocate /var/lib/slocate
echo "Creating Symlinks"

if [ -f /usr/bin/locate ] ; then
   rm -f /usr/bin/locate.old
   mv /usr/bin/locate /usr/bin/locate.old
fi

ln -s /usr/bin/slocate /usr/bin/locate

if [ -f /usr/bin/updatedb ] ; then
   rm -f /usr/bin/updatedb.old
   mv /usr/bin/locate /usr/bin/locate.old
fi

ln -s /usr/bin/slocate /usr/bin/updatedb

echo "Installing man page"
if [ -d /usr/man ] ; then
  mandir="/usr/man"
elif [ -d /usr/share/man ] ; then
  mandir="/usr/share/man"
fi

if [ ! -f ./locate.1.gz ] ; then
   gzip slocate.1 >/dev/null 2>&1
fi

if [ ! -f ./updatedb.1.gz ] ; then
   gzip updatedb.1 >/dev/null 2>&1
fi

if [ -f $mandir/man1/locate.1.gz ] ; then
   mv -f $mandir/man1/locate.1.gz $mandir/man1/locate.old.1.gz
fi

if [ -f $mandir/man1/locate.1.gz ] ; then
   mv -f $mandir/man1/updatedb.1.gz $mandir/man1/updatedb.old.1.gz
fi

install -m 0644 slocate.1.gz $mandir/man1/locate.1.gz
install -m 0644 updatedb.1.gz $mandir/man1/

gunzip slocate.1.gz >/dev/null 2>&1
gunzip updatedb.1.gz >/dev/null 2>&1

echo
echo "Install Complete!"
echo
