#! /bin/bash
#
# The System Manager
# An Administration Tool For UNIX
#
# Copyright (c) 1994 Stuart Herbert
# Released under the GNU GPL v2
#
# Bugs/comments/contributions to S.Herbert@Sheffield.ac.uk

function InstallATar()
{
  local TarName
  
  if [ "`echo $1 | cut -c 1`" = "/" ]; then
    TarName="$1"
  else
    TarName="$SysmanStartDir/$1"
  fi
  
  echo "This tar file will create the following :" > $SysmanFile1
  echo "" >> $SysmanFile1
  
  if YesOrNo "$1 : is this a compressed file?" ; then
    Compressed="YES"
  else
    Compressed="NO"
  fi
  
  InfoBox "Examining file ... please wait"
  
  if [ "$Compressed" = "YES" ]; then
    tar ztf $TarName | sort | $bin/ParseTar >> $SysmanFile1
  else
    tar tf $TarName | sort | $bin/ParseTar >> $SysmanFile1
  fi
  
  echo "" >> $SysmanFile1
  echo "wherever you install it!!" >> $SysmanFile1
  
  TextBox $SysmanFile1
  
  YesOrNo "Do you want to install $1?"
  if [ $? = 1 ]; then
    MsgBox "$1 skipped"
    return
  fi
  
  Dialog --inputbox "Where do you want to install $1?\n\
(Just press ENTER for $SysmanStartDir)" 9 71 2> $SysmanFile1
  
  if [ ! -s $SysmanFile1 ]; then
    TargetDir="$SysmanStartDir"
  else
    Temp="`cat $SysmanFile1`"
    if [ "`echo $Temp | cut -c 1`" = "/" ]; then
      TargetDir="$Temp"
    else
      TargetDir="$SysmanStartDir/$Temp"
    fi
  fi
  
  echo $TargetDir
  
  if [ ! -d $TargetDir ]; then
    Abort "Directory doesn't exist"
    return
  fi
  
  pushd . > /dev/null
  cd $TargetDir
  
  InfoBox "Installing $1 into $SysmanStartDir ... please wait"
  
  if [ "$Compressed" = "YES" ]; then
    tar -zxf $TarName
  else
    tar -xf $TarName
  fi
  
  popd > /dev/null
  
  MsgBox "$1 has been installed"
}

function main()
{
  local FileList
  local x
  
  if [ -f $SysmanFile1 ]; then
    rm -f $SysmanFile1
  fi
  
  touch $SysmanFile1
  
  pushd . > /dev/null
  cd $SysmanStartDir
  
  for x in * ; do
    if [ -f $x ]; then
      `echo -n "$x $x off " >> $SysmanFile1`
    fi
  done
  
  popd > /dev/null
  
  FileList="`cat $SysmanFile1`"
  rm -f $SysmanFile1
  
  Dialog --checklist "Which files do you wish to install?" 20 71 13 $FileList 2> $SysmanFile1
  
  if [ ! -s $SysmanFile1 ]; then
    return
  fi
  
  FileList="`cat $SysmanFile1`"
  
  for x in $FileList ; do
    InstallATar "`echo $x | tr -d "\042" `"
  done
}

# main code
if [ -n "$1" ]; then
  case "$1" in
    --SysmanInstall)
      echo "Y : Y : TAR : Install A Tar :$0"
      exit
      ;;
    --SysmanAuthor)
      echo "TAR : Install A Tar"
      echo "Copyright (c) 1994 Stuart Herbert"
      exit
      ;;
    --SysmanManifest)
      exit
      ;;
    *)
      InstallATar $1
      ;;
  esac
else
  main
fi

# clean up here
for x in $SysmanFile1 $SysmanFile2 $SysmanFile3 ; do
  if [ -f $x ]; then rm -f $x ; fi
done

# that's it, folks
