#!/bin/bash
#---------------------------------------------------------------------
## @Synopsis Dispel is the spell removal utility. It can be called by the user or by intone.
##
## @Copyright Original version Copyright 2001 by Kyle Sallee
## @Copyright Additions/corrections Copyright 2002 by the Source Mage Team
##
## Dispel is the spell removal utility. It can be called by the user or by intone.
##
#---------------------------------------------------------------------


help()	{

  cat  <<  EOF

Dispel uninstalls single or multiple spells.

Example:	dispel  hdparm vim emacs
Usage:		dispel  [parameters]  [spells]

Optional Parameters:

-e | --exile  		Removes spells and blocks them
                        from being automatically reinstalled.
			
-d | --downgrade spell version
			Removes the spell and reinstalls
			the selected version from cache
			
--notriggers		Disables triggers for this dispel
--nosustain		Turns off dispel protection for vital spells
			(you usually don't want to do this)
--noreap		Dispels spells but doesn't remove its files
--no-reap-depends	Dont remove dependency information, dont use this
			unless you know exactly what you are doing, and
			dont complain if it breaks your box.

EOF

  exit  1

}

#-----
## Downgrade a spell to a previously installed version
## @param Spell
## @param Previous version
#-----
function downgrade()  {

  SPELL=$1
  REQUESTED_VERSION=$2
  CACHE_COMP="$INSTALL_CACHE/$SPELL-$REQUESTED_VERSION-$HOST.tar$EXTENSION"

  if    [  !  -f  $CACHE_COMP   ];  then
    message  "${FILE_COLOR}$CACHE_COMP"        \
             "${PROBLEM_COLOR}was not found."  \
             "${DEFAULT_COLOR}"
    return 1
  fi

  if  spell_installed  $SPELL;  then
    previously_installed=yes
    dispel_spell  $SPELL
    if [ $? != 0 ] ; then
      message  "${PROBLEM_COLOR}Unable to downgrade"   \
               "${SPELL_COLOR}${SPELL}${DEFAULT_COLOR}" \
               "dispel failed"
      DISPEL_EXIT_STATUS=${DISPEL_EXIT_STATUS:-1}
      return 1
    fi
  fi

  if  spell_held  $SPELL;  then
    message  "${PROBLEM_COLOR}Unable to downgrade"   \
             "${SPELL_COLOR}${SPELL}${DEFAULT_COLOR}" \
             "because it is held, please unhold it first."
    DISPEL_EXIT_STATUS=${DISPEL_EXIT_STATUS:-1}
    return 1
  fi

  if  spell_exiled  $SPELL;  then
    message  "${PROBLEM_COLOR}Unable to downgrade"   \
             "${SPELL_COLOR}${SPELL}${DEFAULT_COLOR}" \
             "because it is exiled, please unexile it first."
    return 1
  fi

  pushd $INSTALL_ROOT/ &>/dev/null
  if  [  -n  "$EXTENSION"  ];  then
    $COMPRESSBIN  -cd  $CACHE_COMP  |  tar  -Px
  else
    tar  -Pxf  $CACHE_COMP
  fi
  popd &>/dev/null

  codex_set_current_spell_by_name  $SPELL

  add_spell  $SPELL installed $REQUESTED_VERSION
  if [[ $previously_installed ]] ; then
    message  "${RESURRECT_COLOR}Downgraded:"         \
             "${SPELL_COLOR}${SPELL}"                \
             "${DEFAULT_COLOR}"                      \
             "to version"                            \
             "${VERSION_COLOR}${REQUESTED_VERSION}"  \
             "${DEFAULT_COLOR}"
  else 
    message  "${RESURRECT_COLOR}Installed:"         \
             "${SPELL_COLOR}${SPELL}"                \
             "${DEFAULT_COLOR}"                      \
             "to version"                            \
             "${VERSION_COLOR}${REQUESTED_VERSION}"  \
             "${DEFAULT_COLOR}"
  fi
  activity_log  "cast"  "$SPELL"  "$REQUESTED_VERSION"  "success"
  return 0
}

#-----
## Parse the dispel script's parameters
## @Args Parameters
#-----
function process_parameters()  {
  while  [  -n  "$1"  ];  do
    if  echo  ""  $1  |  grep  -q  "^ -";  then
      case  $1  in
         -e|--exile)  EXILE="yes";        shift  1  ;;
     -d|--downgrade)  downgrade  $2  $3
                      DISPEL_EXIT_STATUS=${DISPEL_EXIT_STATUS:-$?}
                      shift  3  
                      ;;
           --noreap)  REAP="off";         shift  1  ;;
  --no-reap-depends)  NO_REAP_DEPENDS="on";  shift  1  ;;
        --nosustain)  SUSTAIN="off";      shift  1  ;;
       --notriggers)  TRIGGER="off";      shift  1  ;;
                  *)  help                          ;;
      esac
    else
      shift
   fi
  done
}

#-----
## Remove the parsed parameters
## This is a silly way to do it
## @Args Parameters
#-----
function strip_parameters()  {
  while  [  -n  "$1"  ];  do
    if  echo  "" $1  |  grep  -q  "^ -";  then
      case  $1  in
            -e|--exile)  shift  1  ;;
        -d|--downgrade)  shift  3  ;;
              --noreap)  shift  1  ;;
     --no-reap-depends)  shift  1  ;;
           --nosustain)  shift  1  ;;
          --notriggers)  shift  1  ;;
                     *)  shift  1  ;;
      esac
    else
      echo  $1
      shift
   fi
  done
}

#-----
## Main loop for dispel
## @Args Parameters
#-----
function main() {

  process_parameters        $*
  SPELLS=`strip_parameters  $*`

  for  SPELL  in  $SPELLS;  do
    dispel_spell $SPELL ||
    DISPEL_EXIT_STATUS=${DISPEL_EXIT_STATUS:-$?}
  done
  return  ${DISPEL_EXIT_STATUS:-0}
}

. /etc/sorcery/config
if    [  $#      ==  0  ];  then  help  |  $PAGER
elif  [[  $1 == -h  ]]  ||  [[  $1 == --help  ]] ; then help
elif  [  "$UID"  ==  0  ];  then 
  mk_tmp_dirs dispel
  main  $*
  rc=$?
  cleanup_tmp_dir $TMP_DIR
  exit $rc
else  
  echo  "Enter the root password, please."  1>&2
  su -c "$0 $*" root
fi


#---------------------------------------------------------------------
##=back
##
##=head1 LICENSE
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
