#!/bin/sh
#
#   The installer runs the method script several times. The first time
#   through, it is looking for file conflicts, warnings, etc. Certain 
#   situations, such as replace link with copy, might cause a warning during
#   this phase.
#
#   The installer deliberately tries to avoid deleting files.
#
#   inst-method <phase> <verbose> <method name> <cname> <cvers> <copt>
#
#   Where:
#
#           <phase>       install phase  (pre/post/safe)
#           <verbose>     installer verbosity level (currently, only 0/1)
#           <method name> method name (copy, update, conflict, or error)
#           <cname>       internal name of component
#           <cvers>       component version number, used to update manifest
#           <copt>        option argument to install method, used in creating
#                         symbolic links
#
#   Output:
#
#          inst-method generates and executes a shell script. The logic
#              for script generation is found in method specific awk scripts.
#

utils="SystemResources/Utilities"
comps="SystemResources/Components"

#  The error warning means that the component manifests call for a
#  component not on the media. This happens when the media manifest is
#  not properly written.

#  The conflict warning is issued if a component to be installed requires
#  a downgrade of another component. This only happens if version numbers 
#  are set in the wrong sequence on the media manifest.

if [ -f $ITMPDIR/AbortInstall ]; then
   exit
fi

vs="$ITMPDIR/InstEnv"

if [ $1 = "pre" ]; then

   if [ $3 = "conflict" ]; then
      printm Conflict
      echo "conflict $4" >> $ITMPDIR/InstallManifest
   elif [ $3 = "error" ]; then
      printm IncompleteMedia
      echo "errormethod $4" >> $ITMPDIR/InstallManifest
   else
      cat $vs $comps/$4 | awk -f $utils/preinst.awk > $ITMPDIR/PreScript
      . $ITMPDIR/PreScript
   fi

elif [ $1 = "post" ]; then

   if [ $3 = "copy" ]; then
      cat $vs $comps/$4 | awk -f $utils/copy.awk | sh 2> $ITMPDIR/Errors
      echo "installed $4 $5" >> $ITMPDIR/InstallManifest
      if [ $2 -eq 1 ]; then
         cat $ITMPDIR/Errors
      fi
   elif [ $3 = "update" ]; then
      cat $vs $comps/$4 | awk -f $utils/update.awk | sh 2> $ITMPDIR/Errors
      if [ $2 -eq 1 ]; then
         cat $ITMPDIR/Errors
      fi
   elif [ $3 = "link" ]; then
     (echo "var linktarget $6"; cat $vs $comps/$4) | awk -f $utils/link.awk | sh 2> $ITMPDIR/Errors
     echo "linked $4 $5 $6" >> $ITMPDIR/InstallManifest
   fi 

   #  Error processing is done through two scripts. Errors are placed in
   #  the installation manifest by prepending the word "error" followed
   #  by the information (as of right now, nothing fancy, just copy the
   #  line). The second processing is done to abort further installation if
   #  an out of space is detected.

   cat $ITMPDIR/Errors | awk '{ print "error " $0 }' >> $ITMPDIR/InstallManifest

   cat $ITMPDIR/Errors | grep "No space left on device" > /dev/null 2>&1
   if [ "$?" -eq 0 ]; then
      touch $ITMPDIR/AbortInstall
   fi

   cat $ITMPDIR/Errors | grep "write failed" > /dev/null 2>&1
   if [ "$?" -eq 0 ]; then
      touch $ITMPDIR/AbortInstall
   fi

elif [ $1 = "safe" ]; then

   if [ $3 = "copy" -o $3 = "update" ]; then
      cat $vs $comps/$4 | awk -f $utils/safe.awk
   elif [ $3 = "link" ]; then
      (echo "var linktarget $6"; cat $vs $comps/$4) | awk -f $utils/linksafe.awk
   fi

fi

