#!/bin/sh
# Scriptname: gemview-1.0.1-1 installation script

abort () 
{
  echo
  echo "The installation of $RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE has been aborted."
  exit 1
}       

answer_is_yes() 
{
  read answer
  case "$answer" in
  n* | N*)
    return 1;;
  q* | Q*)
    abort;;
  *)          
    return 0;;
  esac 
} 

RPM_PACKAGE_NAME=gemview
RPM_PACKAGE_VERSION=1.0.1
RPM_PACKAGE_RELEASE=1
current_directory=$PWD
clear 
echo "This script installs $RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE."  
if [ -f gemview-1.0.1-1.tar.gz ] 
then
  installation_directory_is_correct=""
  until [ "$installation_directory_is_correct" ] 
  do
    echo "Where would you like to install $RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE? [/usr/local/gp/gemview]" 
    echo -n "> "
    read installation_directory
    if [ "$installation_directory" = "" ]
    then 
      installation_directory="/usr/local/gp/gemview"
    else 
      case "$installation_directory" in
      /*) 
        :;;
      *) 
        installation_directory=$current_directory/$installation_directory;;
      esac
    fi
    echo -n "$installation_directory: Is this correct? [(n)o (q)uit] [y]"
    if 'answer_is_yes'
    then
      installation_directory_is_correct="yes"
      if [ -d $installation_directory ]
      then
        echo "$installation_directory already exists."
        if [ -w $installation_directory ]
        then :
        else
          echo "$installation_directory is write-protected."
          installation_directory_is_correct=""
        fi                       
      elif [ -f $installation_directory ]
      then
        echo "$installation_directory is a file."   
        installation_directory_is_correct=""
      else
        echo "$installation_directory does not exist."
        echo "$installation_directory will be created."
        install -d $installation_directory 2>/dev/null
        if $status
        then :
        else 
          echo "$installation_directory could not be created."
          installation_directory_is_correct=""
        fi
      fi
    fi
  done
  install -d $installation_directory/logs 2>/dev/null
  if [ ! -d $installation_directory/logs ] 
  then
    echo "$installation_directory/logs could not be created."
    abort
  fi
  cp -f gemview-1.0.1-1.tar.gz $installation_directory 2> $installation_directory/logs/errors.log
  cd $installation_directory 2> $installation_directory/logs/errors.log
  /bin/gzip -dc gemview-1.0.1-1.tar.gz | tar -xvvf - 2> $installation_directory/logs/errors.log
  cd gemview-1.0.1 2> $installation_directory/logs/errors.log
  
# was  for file in gemview sltohta libqtintf.so libqt.so.2 libfj9f6.so.1 libfj9i6.so.1 gdat2-06.har gip73a.sl4 orang867.har orng98sr.sl4 gemview-help.html
  for file in gemview sltohta libqtintf.so libqt.so.2 gdat2-06.har gip73a.sl4 orang867.har orng98sr.sl4 gemview-help.html
  do
    if [ -f $file ] 
    then
      install $file $installation_directory/$file 2> $installation_directory/errors.log
    else
      echo "$PWD/$file does not exist." >> $installation_directory/errors.log
      abort
    fi
  done

  P=$installation_directory

  rm -f $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-startup.sh
 cat > $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-startup.sh << EOF
#!/bin/sh
if [ "\$LD_LIBRARY_PATH" ]
then
  export LD_LIBRARY_PATH=$P:\$LD_LIBRARY_PATH
else
  export LD_LIBRARY_PATH=$P
fi
$P/gemview
EOF

  chmod +x $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-startup.sh
  ln -fs $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-startup.sh /usr/bin/$RPM_PACKAGE_NAME 
  echo "$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE was successfully installed in $P."
  echo "A symbolic link called $RPM_PACKAGE_NAME has been created in /usr/bin."
  echo "/usr/bin/$RPM_PACKAGE_NAME is a symbolic link to $P/gemview."
  rm -f $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-uninstall.sh
  cat > $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-uninstall.sh << EOF 
#!/bin/sh
rm -f /usr/bin/$RPM_PACKAGE_NAME
rm -f $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-startup.sh
rm -f $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-uninstall.sh
echo "$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE was successfully removed from your system."
if [ -d "$P" ]
then
  echo "$P was not removed from your system for security reasons."
fi
EOF
  chmod +x $P/$RPM_PACKAGE_NAME-$RPM_PACKAGE_VERSION-$RPM_PACKAGE_RELEASE-uninstall.sh
else
  echo "You need to copy gemview-1.0.1-1.tar.gz into your current directory $current_directory."
  echo "Please consult GEMPACK Document No. GPD-10 gpd10.pdf."
  abort
fi
echo
echo "${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE} has been installed in $installation_directory."

