#----------------------------------------------------------------#
#  Name: install                                                 #
#                                                                #
#  Description: Install script for UNIX-SPIRS (AIX) client       #
#               software.                                        #
#                                                                #
#  Notes: You must have superuser permissions to use this script #
#         to install the software.                               #
#----------------------------------------------------------------#

PRODUCT='SilverPlatter Inc. UNIX-SPIRS Client Software (AIX)'
VERSION=1.01
TARFILE1=unixspirs.tar
TARFILE2=UNIXSPIRS.tar

# TOOK THIS OUT (JPM)
#if [ $0 != ./install ] 
#then 
#   echo Use "./install" to start the installation 
#   exit
#fi
	
#-----------------------------------#
# Search for the UNIX-SPIRS Tarfile #
#-----------------------------------#
echo Running  $0 
TARFILE_FOUND=0
if [ -f $TARFILE1 ]
then
   TARFILE=$TARFILE1
else
   if [ -f $TARFILE2 ]
   then
      TARFILE=$TARFILE2
   else
      if [ -f "$TARFILE1".Z ]
      then
         uncompress "$TARFILE1".Z 	
         TARFILE=$TARFILE1
      else
         if [ -f "$TARFILE2".Z ]
         then
            uncompress "$TARFILE2".Z
            TARFILE=$TARFILE2
         fi
      fi
   fi
fi	

if [ -f $TARFILE ]
then
   echo 
else
   echo "Unable to find files necessary for installation. Exiting ..."
   exit
fi

#--------------------#
# Begin Installation #
#--------------------#
echo -n  Install  $PRODUCT  $VERSION   [Y]/N : 
choice=
read choice
if [ $choice ]
then
   if [ $choice = 'Y' ] 
   then 
      echo 
   else 
      if [ $choice = 'y' ]
      then
         echo 
      else
         echo "Exiting ......"
         exit
      fi
   fi
fi

tar -xvf $TARFILE  modfile.sh request postinstall uinst.sh  > /dev/null
chmod +x request postinstall uinst.sh modfile.sh

USERNAME=
. ./request

#----------------#
# Get Group Name #
#----------------#
INSTALLTMP=$PWD
cd $PATHNAME
echo "Installing  $PRODUCT in $PATHNAME ..."

if [ -f $PATHNAME/spirs ]
then
   echo "There is an existing installation of this product."
   echo  -n Overwrite it [Y]/N: 
   INSTALL=0     
   read choice
   if  [ $choice ]
   then
      if  [ $choice = 'Y' ] 
      then
         INSTALL=1
      fi
      if  [ $choice = 'y' ] 
      then
         INSTALL=1
      fi
   else
      INSTALL=1
   fi
   if [ $INSTALL -eq  0 ]
   then
      echo "Product not installed. Exiting ..."
      exit
   fi
fi

echo "Extracting files from $TARFILE ..."
tar -xvf $INSTALLTMP/$TARFILE   >/dev/null || exit $?
. $INSTALLTMP/postinstall
. $INSTALLTMP/modfile.sh
chown $USERNAME ./
chgrp $GROUPNAME ./
chmod 770 .
chmod 770 dbase
chmod 770 download
chmod g-s .
chmod g-s dbase
chmod g-s download
export PATHNAME
export USERNAME
export GROUPNAME

echo "Creating Un-Install script ..."
echo INSTALLDIR=$PWD  >uninstall 
echo >>uninstall
cd $PATHNAME
cat  $INSTALLTMP/uinst.sh  >>uninstall
tar -tvf $INSTALLTMP/$TARFILE  | awk '{print "rm -f", $9}' >>uninstall 
rm -f $PATHNAME/request
rm -f $PATHNAME/postinstall
rm -f $PATHNAME/modfile.sh
rm -f $PATHNAME/uinst.sh    

chmod 550 uninstall
chown $USERNAME uninstall
chgrp $GROUPNAME uninstall
echo  "$PRODUCT installed succesfully."
echo "Use 'uninstall' for future UNINSTALLATION"
cd $INSTALLTMP 

rm -f request
rm -f postinstall
rm -f uinst.sh    
rm -f modfile.sh

#echo  -n Delete temporary  installation files in directory $PWD [y]/n:
#read choice

#REMOVE=0
#if  [ $choice ]
#then
#   if  [ $choice = 'Y' ] 
#   then
#      REMOVE=1
#   fi
#
#if  [ $choice = 'y' ] 
#then
#   REMOVE=1
#fi
#else
#   REMOVE=1
#fi
#
#if [ $REMOVE neq 0 ]
#then
#   rm install
#   rm spclient.tar 
#   rm change_owner
#   rm change_group
#   rm change_perm
#fi
#

