#!/usr/bin/ksh

IPKG=$PKG
TNAME=scoxe

#
# When passed in a package name, checks to see if there is already an
# installed version on the machine which is equal or greater than the
# one we wish to install.  It echos out either a "Y" if we need to install
# our newer version, or a "N" to skip installing this particular package.
#
SWCheck ()
{
  rc="Y"
  tpkg="$1"
  pkginfo -liq $tpkg >/dev/null 2>&1
  pkgrc=$?
  if [ $pkgrc -eq 0 ]; then
    # The package exists, so check the version
    opkgver=`pkginfo -l $tpkg | grep "VERSION:" | sed -e 's/[ ]*VERSION:[ ]*//g' -e 's/Version //g'`
    x="\$`echo ${tpkg}VER`"
    tpkgver=`eval "echo $x"`
    rc=`awk -v OPKG=$opkgver -v IPKG=$tpkgver 'BEGIN { \
      split(OPKG, op, "."); \
      split(IPKG, ip, "."); \
      REPL="N"; \
      i = 1; \
      while (op[i] != "") { \
        if (int(op[i]) < int(ip[i])) { \
          REPL="Y"; \
          break; \
        } else { \
          if (int(op[i]) > int(ip[i])) { \
            break; \
          } else { \
            if (op[i] != ip[i]) { \
              if (op[i] < ip[i]) { \
                REPL="Y"; \
                break; \
              } else { \
                break;
              } \
            } \
          } \
        } \
        i ++; \
      } \
      if (REPL == "N" && (op[i] == "" && ip[i] != "")) { \
        REPL = "Y"; \
      } \
      printf "%s", REPL;
    } { \
    }' < /dev/null`
  fi
  echo "$rc"
}

#
# First, determine if there is enough space in /tmp
#
ROOTDIR="`mount | grep \"^/tmp \"`"
export ROOTDIR
if [ -z "$ROOTDIR" ]; then
  ROOTDIR=/
else
  ROOTDIR=/tmp
fi
if [ -x /tmp/tmpdir ]; then
  . /tmp/tmpdir
  ROOTDIR=$TMPDIR
  export TMPDIR
fi
echo "ROOTDIR=$ROOTDIR"
BSPACE=`df -t "$ROOTDIR" | grep -v "total:" | sed -e 's/ blocks.*//g' -e 's/.* //g'`
bc > /tmp/xxx.$$ <<!EOD
${BSPACE}*512
quit
!EOD
SPACE="`cat /tmp/xxx.$$`"
MINSPACE="85983232"
echo "SPACE=$SPACE MINSPACE=$MINSPACE"
echo "AVAIL=\c" > /tmp/xxx.$$
bc > /tmp/xxx.$$ <<!EOD
${SPACE}-${MINSPACE}
quit
!EOD
AVAIL="`cat /tmp/xxx.$$`"
rm /tmp/xxx.$$
NEG="`echo $AVAIL | cut -c1`"
# Check if there is 82MB available in TMPDIR
if [ "${NEG}" = "-" ]; then
  # There isn't enough tmp space available
  echo "Not enough space available in $ROOTDIR!"
  echo "$SPACE bytes available, $MINSPACE bytes required."
  echo "Either increase $ROOTDIR to have 82MB of available space, or"
  echo "create a directory in a filesystem with 82MB of available space,"
  echo "and create an executable file /tmp/tmpdir with the line"
  echo "'TMPDIR=/new_directory' in it, where /new_directory is the"
  echo "directory with 82MB of available space."
  exit 1
fi
unset ROOTDIR SPACE MINSPACE

#
# Next, determine which packages need to be installed
#
TCLASS=""; export TCLASS
for class in $CLASSES
do
  inst="`SWCheck $class`"
  if [ "$inst" = "Y" ]; then
    TCLASS="$TCLASS $class"
  fi
done

# The below lines are commented out as old perl version installation code.
## Prompt the user if they want the new perl installed, if
##    /usr/local/bin/perl exists, and we plan to install perl
#ans="Y"
#if [ -n "$TCLASS" -a -n "`echo $TCLASS | grep 'perl '`" ]; then
#   if [ -f /usr/local/bin/perl ]
#   then
#      ans="x"
#      while [ "$ans" != "" -a "$ans" != "Y" -a "$ans" != "y" -a "$ans" != "n" -a "$ans" != "N" ]
#      do
#         echo "There appears to be a conflicting version of perl already"
#         echo "installed on this system.  The version to be installed is"
#         echo "Perl 5.8.0."
#         echo "Do you want to replace your current version (Y/n)?\c "
#         read ans
#         if [ "$ans" = "N" -o "$ans" = "n" ]
#         then
#            echo "NOTE: you have chosen to keep the currently installed version"
#            echo "      of perl.  After the set installation is completed,"
#            echo "      please run the following command, as root:\n"
#            echo "      ln -s /usr/gnu/lib/perl5/i386-svr4/5.00404/CORE/libperl.so.404 /usr/gnu/lib/perl/i386-svr4/5.00404/CORE/libperl.so\n"
#            echo "Press <Enter> to continue:\c "
#            read ans
#            ans="N"
#         elif [ "$ans" = "" -o "$ans" = "Y" -o "$ans" = "y" ]
#         then
#            ans="Y"
#         else
#            echo "Invalid response.  Try again.\n"
#         fi
#      done
#   fi
#fi
#
if [ -n "$TCLASS" ]; then
# The below lines are commented out as old perl version installation code.
#  # It may have been requested to NOT install perl
#  if [ "$ans" = "N" ]; then
#     if [ -n "`echo $TCLASS | grep 'perl '`" ]; then
#       TCLASS="`echo $TCLASS | sed -e 's/perl //g' -e 's/perl$//g'`"
#     fi
#  fi
  CLASSES="$TCLASS"
fi
if [ -z "`echo $TCLASS | grep '[A-Za-z0-9]'`" ]; then
  CLASSES=""
fi
 
PKGLIST="$CLASSES"; export PKGLIST

if [ -z "$PKGLIST" ]; then
	echo "\nAll packages for this set have already been installed."
	exit 77
fi

# Run the request scripts for each package in the set that has one
QUIETMODE=true; export QUIETMODE
if [ -n "$PKGLIST" ]; then
  for PKG in $PKGLIST
  do
    if [ -f $REQDIR/${PKG}/request ]; then
      (
      set -a
      pkgparam -v -f $REQDIR/${PKG}/pkginfo >/tmp/${TNAME}params.$$
      . /tmp/${TNAME}params.$$
      PKGINST=$PKG
      echo "## Executing $PKGINST request script."
      sh $REQDIR/${PKG}/request $RESPDIR/${PKG} </dev/tty >/dev/tty 2>&1
      [ $? != 0 ] && {
        exit 1
      }
      rm -f /tmp/${TNAME}params.$$
		)
    fi
  done
fi

PKG=$IPKG

NPKG_LIST=${PKGLIST}; export NPKG_LIST
echo "PKGLIST=${PKGLIST}" >> $1

exit 0
