#! /bin/bash
#
# REMOVE : Remove a package
# 
# This script was written to remove packages for The System Manager
# and was inspired by the Linux kernel configure script by 
# raymondc@microsoft.com (well, I think it's a neat hack :-)
#
# I'm sure this script can be used for any general purpose installation,
# and I hope it'll catch on big-time.  Let me say that again, louder.
#
# PLEASE USE THIS SCRIPT TO PROVIDE AN INSTALLATION SYSTEM FOR YOUR OWN
# SOFTWARE.  PLEASE USE THIS SCRIPT ON AS MANY UNIX PLATFORMS AS POSSIBLE.
# PLEASE MAKE USE OF IT'S SISTER SCRIPT, REMOVE, TO PROVIDE A WAY TO REMOVE
# YOUR SOFTWARE.
#
# Please send comments/suggestions/bug fixes to S.Herbert@sheffield.ac.uk

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--

set -h

COLOR_BLACK="\033[0;30m"
COLOR_RED="\033[1;31m"
COLOR_GREEN="\033[1;32m"
COLOR_YELLOW="\033[1;33m"
COLOR_BLUE="\033[1;34m"
COLOR_CYAN="\033[1;36m"
COLOR_LIGHT_GREY="\033[0;37m"
COLOR_DARK_GREY="\033[1;30m"
COLOR_WHITE="\033[1;37m"

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--
# $1 = manifest of files to remove

NailIt()
{
  echo -ne "$COLOR_DARK_GREY"
  echo     "--*-- --*-- --*-- --*-- --*--"
  echo -ne "$COLOR_LIGHT_GREY"
  echo     "Removing '$COLOR_WHITE$1$COLOR_LIGHT_GREY'"
  echo -ne "$COLOR_DARK_GREY"
  echo     "--*-- --*-- --*-- --*-- --*--"
  
  mv ~/.INSTALLed/$1 /tmp/REMOVE.$USER.$1
  
  cat /tmp/REMOVE.$USER.$1 | while read x ; do
    if [ ! -d $x ]; then
      if NotRequired $x ; then
        Report "0" "$x"
        rm -f $x
      else
        Report "255" "$x" "required by $PACKAGE"
      fi
    fi
  done

# this should remove empty directories
# perhaps attempting this is silly?

  cat /tmp/REMOVE.$USER.$1 | while read x ; do
    Contents="`echo $x/*`"
    if [ -d $x -a "$Concents" = "$x/*" ]; then
      Report "0" "$x" "directory"
      rm -rf $x
    fi
  done
  
  rm -f /tmp/REMOVE.$USER.$1
}

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--
# $1 = a required file

NotRequired()
{
  if [ -e ~/.INSTALLed/* ]; then
    if grep "^$1\$" ~/.INSTALLed/* > /dev/null ; then
      return 1
    fi
  fi
  return 0
}

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--
# $1 = succeed/found/warning/fail (0/1/254/255)
# $2 = general report
# $3 = fail report

Report()
{
  if [ "$1" -eq 255 ]; then
    echo -ne "$COLOR_RED"
    echo -n  "Skipping"
  elif [ "$1" -eq 254 ]; then
    echo -ne "$COLOR_YELLOW"
    echo -n  "Warning "
  elif [ "$1" -eq 1 ]; then
    echo -ne "$COLOR_CYAN"
    echo -n  "Found   "
  else
    echo -ne "$COLOR_GREEN"
    echo -n  "Removing"
  fi
  echo -ne "$COLOR_LIGHT_GREY"
  echo -n  " : $2"
  if [ ! "$3" = "" ]; then
    echo " ($3)"
  else
    echo ""
  fi
}

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--
# no arguements

Usage()
{
  if [ -d ~/.INSTALLed ]; then
    echo "REMOVE: Remove a package"
    echo "Packages available for removal are :"
    echo ""
    ls ~/.INSTALLed
    echo ""
    echo "Type 'REMOVE <package> ...' to remove a package or two."
  else
    echo "REMOVE: Nothing installed"
  fi
}

#--*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*-- --*--
# this is where the fun begins

# if no arguements, show usage and quit
if [ "$1" = "" ]; then
  Usage
  exit 0
fi

# right, now work through arguements ...
for x in $@ ; do
  if [ -r ~/.INSTALLed/$x ]; then
    Report "0" "$x" "package"
    NailIt $x
  else
    Report "255" "$x" "package not found"
  fi
done

# that's it folks!
