#!/bin/sh
############################################################
#                                                          #
# lrm - de-install Lunar modules                           #
#                                                          #
############################################################
# dispel is part of the sorcery spell management utility   #
# Copyright 2001 by Kyle Sallee                            #
############################################################
#                                                          #
# this WAS dispel script of a source based Linux distro,   #
# calls Sorcerer GNU/Linux, or SGL. SGL is no longer       #
# available with GPL license. Since this script was taken  #
# before licensing scheme change, no legal problems I      #
# guess.                                                   #
#                                                          #
# the code is re-written for Lunar. The previous Copyright #
# notices are kept; just in case some code is left :=)     #
# Kagan Kongar <kongar@tsrsb.org.tr>, 20020519             #
#                                                          #
############################################################
#                                                          #
# Parts Copyrighted Jason Johnston  2002 under GPLv2       #
#                                                          #
# Parts Copyrighted Kagan Kongar 2002 under GPLv2          #
#                                                          #
############################################################



help()	{
  cat  <<  EOF
lrm uninstalls single or multiple modules.

Example:	lrm  hdparm vim emacs
Usage:		lrm  [parameters]  [modules]

Optional Parameters:

-e | --exile  modules			Removes modules and blocks 
					them from being automatically
					reintalled.
-d | --downgrade  module  version

Downgrade is an option for removing the selected module 
and restoring a previously installed version.
EOF
  exit  1
}
pre_remove() {
  if  [  -x  $SCRIPT_DIRECTORY/PRE_REMOVE  ];  then  unset  LD_PRELOAD
           . $SCRIPT_DIRECTORY/PRE_REMOVE
  fi
}
post_remove() {
  LD_PRELOAD_OLD="$LD_PRELOAD"
  unset  LD_PRELOAD
  if  [   -d  $SCRIPT_DIRECTORY/xinetd.d  ]  &&
      ps  -C  xinetd  >  /dev/null;          then
    /etc/init.d/xinetd  restart
  fi
  if  [   -d  $SCRIPT_DIRECTORY/init.d    ]; then
    for SCRIPT in $SCRIPT_DIRECTORY/init.d/* ; do
        SCRIPTNAME=`basename $SCRIPT`
            [ -x /etc/init.d/$SCRIPTNAME ] &&
            rm -f /etc/rc?.d/???$SCRIPTNAME
    done  
  fi

  export  LD_PRELOAD="$LD_PRELOAD_OLD"

  if  [  -x  $SCRIPT_DIRECTORY/POST_REMOVE  ]
  then     . $SCRIPT_DIRECTORY/POST_REMOVE
  fi
}
downgrade()  {
  MODULE=$1
  REQUESTED_VERSION=$2
  CACHE_BZ="$INSTALL_CACHE/$MODULE-$REQUESTED_VERSION-$BUILD.tar.bz2"
 
  if  [      -e   $CACHE_BZ  ]  &&
      bzip2  -tf  $CACHE_BZ;    then

    if  module_installed  $MODULE;  then
      lrm  $MODULE
    fi

    bzcat   $CACHE_BZ  |  tar  -Pkx  2>/dev/null
    SECTION=`find_section  $MODULE`

    if  [  -x  $MOONBASE/$SECTION/$MODULE/DETAILS  ];  then
            .  $MOONBASE/$SECTION/$MODULE/DETAILS  >  /dev/null  
    fi

    if  [  "$VERSION"  ==  "$REQUESTED_VERSION"  ];  then
      add_module  $MODULE installed $VERSION
      message  "${RESURRECT_COLOR}Installed:"  \
               "${MODULE_COLOR}${MODULE}"        \
               "${DEFAULT_COLOR}"              \
               "version"                       \
               "${VERSION_COLOR}${VERSION}"    \
               "${DEFAULT_COLOR}"
    else
      add_module  $MODULE held $REQUESTED_VERSION
      message  "${RESURRECT_COLOR}Downgraded:"         \
               "${MODULE_COLOR}${MODULE}"                \
               "${DEFAULT_COLOR}"                      \
               "to version"                            \
               "${VERSION_COLOR}${REQUESTED_VERSION}"  \
               "${DEFAULT_COLOR}"
    fi
    activity_log  "lin"  "$MODULE"  "$VERSION"  "success"
  else
    message  "${FILE_COLOR}$CACHE_BZ"          \
             "${PROBLEM_COLOR}was not found."  \
             "${DEFAULT_COLOR}"

    message  "${PROBLEM_COLOR}Unable to downgrade"   \
             "${MODULE_COLOR}${MODULE}"                \
             "${DEFAULT_COLOR}"                      \
             "to version"                            \
             "${VERSION_COLOR}${REQUESTED_VERSION}"  \
             "${DEFAULT_COLOR}"
  fi
}
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;  shift  3  ;;
           --noreap)  REAP="off";         shift  1  ;;
        --nosustain)  SUSTAIN="off";      shift  1  ;;
                  *)  help                          ;;
      esac
    else
      shift
   fi
  done
}
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  ;;
           --nosustain)  shift  1  ;;
                     *)  shift  1  ;;
      esac
    else
      echo  $1
      shift
   fi
  done
}
not_possible()  {
  if  !  grep  -q  "^$MODULE:"  $MODULE_STATUS  &&
      !     [  -n  "$EXILE"  ]
  then
    message  "${MODULE_COLOR}${MODULE}"             \
             "${PROBLEM_COLOR}is not installed."  \
             "${DEFAULT_COLOR}"
  else
    false
  fi
}
sustained()  {
  if  [  "$SUSTAIN"  ==  "on"  ]  &&
      grep  -q  "^$MODULE\$"  $SUSTAINED
  then
    message  "${MODULE_COLOR}${MODULE}"         \
             "${PROBLEM_COLOR}is sustained."  \
             "${DEFAULT_COLOR}"
  else
    false
  fi
}
reap_depends()  {
  if    [  "$SUSTAIN"  !=  "off"  ]
  then  remove_depends  $MODULE
  fi
}
main()	{
  process_parameters        $*
  MODULES=`strip_parameters  $*`

  LRM_EXIT_STATUS=0

  for  MODULE  in  $MODULES;  do
    # if  [  "$MODULE"  ==  "glibc"  ];  then
    #   message  "${PROBLEM_COLOR}Cowardly refusing to lrm glibc.${DEFAULT_COLOR}"
    #   exit  1
    # fi

    run_details $MODULE      
    if  sustained  ||
        not_possible
    then
      LRM_EXIT_STATUS=1
      continue
    fi

     VERSION=`installed_version  $MODULE`
    INST_LOG=$INSTALL_LOGS/$MODULE-$VERSION
     MD5_LOG=$MD5SUM_LOGS/$MODULE-$VERSION

    pre_remove
    reaper  $INST_LOG  $MD5_LOG
    reap_depends
    post_remove
    remove_module  $MODULE
    message  "${LRM_COLOR}Removed module:"  \
             "${MODULE_COLOR}${MODULE}"           \
             "${DEFAULT_COLOR}"
    activity_log  "lrm"  "$MODULE"  "$VERSION"  "success"
  done

  return  $LRM_EXIT_STATUS
}
. /etc/lunar/config

root_check || exit 1

if    [  $#      ==  0  ];  then  
   help  |  less
   exit 0
fi

main  $*
