#!/bin/ksh

unset IFS

TAG_DIRECTORY=`/twc/bin/db_exp -f c_adtag_directory -s dif /twc/dif/wxl_dif`

cd $TAG_DIRECTORY

FILELIST=`ls *.tar 2>/dev/null`

# Do not keep previous runs of this script
rm $TAG_DIRECTORY/extract_and_install_tags.log

for file in $FILELIST
do
  # Take the extension off the filename to get the directory name
  IFS=.
  echo "$file" | read filename extension
  IFS=  

  # Get the version from the DIF
  CURRENT_VERSION=`/twc/bin/db_exp -s dif -f $filename""_version_stamp /twc/dif/wxl_dif`
  if [[ "x" = "x$CURRENT_VERSION" ]]
  then
    CURRENT_VERSION="-1"
  fi

  # If it exists, save off the current version that has been extracted.
  if [[ -f $filename/version ]]
  then
    mv $filename/version $filename/current_version
  fi

  # If it exists, save off the current expiration that has been extracted.
  if [[ -f $filename/expiration ]]
  then
    mv $filename/expiration $filename/current_expiration
  fi
  
  # Get the new version
  tar xvf "$file" $filename/version

  # Get the new expiration
  tar xvf "$file" $filename/expiration

  # If there is no version file in the new tarfile, skip this one
  if [[ ! -f $filename/version ]]
  then
    echo "Error: no $filename/version file in tarfile $file" >> $TAG_DIRECTORY/extract_and_install_tags.log
    echo "Tag: $filename NOT installed!" >> $TAG_DIRECTORY/extract_and_install_tags.log
  else
    # If the version file is there, compare to the existing version.
    exec 0< $filename/version
    read NEW_VERSION
    if [[ "x" = "x$NEW_VERSION" ]]
    then
      NEW_VERSION="-1"
    fi

    # If there is no expiration file in the new tarfile, skip this one
    if [[ ! -f $filename/expiration ]]
    then
      echo "Error: no $filename/expiration file in tarfile $file" >> $TAG_DIRECTORY/extract_and_install_tags.log
      echo "Tag: $filename NOT installed!" >> $TAG_DIRECTORY/extract_and_install_tags.log
    else
      exec 0< $filename/expiration
      read EXPIRE_TIME
      if [[ "x" = "x$EXPIRE_TIME" ]]
      then
        EXPIRE_TIME="-1"
      fi
      CURRENT_TIME=`amtime1970`

      # Make sure this is a version upgrade and it's not already expired
      # Do not load if expired or version is the same or less than current.
      #
      if (( $NEW_VERSION > $CURRENT_VERSION && $CURRENT_TIME < $EXPIRE_TIME ))
      then
        # extract the files from the tar file.
        echo "Extracting and Installing $file" >> $TAG_DIRECTORY/extract_and_install_tags.log
        tar xvf "$file"

        # Run the install script for the given tag.
        echo "" >> $TAG_DIRECTORY/extract_and_install_tags.log
        $filename/install.sh >> $TAG_DIRECTORY/extract_and_install_tags.log

        # Get rid of the old version file (if any).
        if [[ -f $filename/current_version ]]
        then
          rm $filename/current_version
        fi
        # Get rid of the old expiration file (if any).
        if [[ -f $filename/current_expiration ]]
        then
          rm $filename/current_expiration
        fi
      else

        # If we go here, one of the two tests in the if failed, find which one.
        if (( $NEW_VERSION <= $CURRENT_VERSION ))
        then
          echo "Error: tag version: $NEW_VERSION not newer that current version: $CURRENT_VERSION" >> $TAG_DIRECTORY/extract_and_install_tags.log
        else
          echo "Error: tag expiration: $EXPIRE_TIME is in the past." >> $TAG_DIRECTORY/extract_and_install_tags.log
          echo "Current time is $CURRENT_TIME" >> $TAG_DIRECTORY/extract_and_install_tags.log
        fi
        echo "Tag: $filename NOT installed!" >> $TAG_DIRECTORY/extract_and_install_tags.log

        # If it exists, restore the current version file that was moved.
        if [[ -f $filename/current_version ]]
        then
          rm $filename/version
          mv $filename/current_version $filename/version
        else
          # If it does not exist, clean whole directory out.
          rm -rf $filename
        fi
        # If it exists, restore the current expiration file that was moved.
        if [[ -f $filename/current_expiration ]]
        then
          rm $filename/expiration
          mv $filename/current_expiration $filename/expiration
        else
          # If it does not exist, clean whole directory out.
          rm -rf $filename
        fi
      fi
    fi
  fi

  #
  # Now, remove the tar file whether or not it needed to be installed.
  rm "$file"
done
