#!/bin/sh

# Author   : Benoit PAPILLAULT <benoit.papillault@sourcemage.org>
# Creation : 2004-11-25

# Design document:
# http://benoit.papillault.free.fr/sourcemage/cast-binary.php

# usage: update_sorcery_packages spell

update_sorcery_packages()
{
  local SPELL="$1"

  local packages="${INSTALL_ROOT}/var/state/sorcery/packages"
  if [ -f "${packages}" ]; then
    grep -v "^${SPELL}:" "${packages}" > /tmp/packages &&
	mv /tmp/packages "${packages}"
  fi
}

# usage: update_sorcery_depends spell

update_sorcery_depends()
{
  local SPELL="$1"

  local depends="${INSTALL_ROOT}/var/state/sorcery/depends"
  if [ -f "${depends}" ]; then
    grep -v "^${SHELL}:" "${depends}" > /tmp/depends &&
	mv /tmp/depends "${depends}"
  fi
}

# usage : remove_local_depends spell

remove_local_depends()
{
  local SPELL="$1"

  # copy /etc/sorcery/local/depends files
  local DEPENDS_DIR="${INSTALL_ROOT}/etc/sorcery/local/depends"
  local DEPENDS="${DEPENDS_DIR}/${SPELL}"
  rm -f "${DEPENDS}" &&
  rm -f "${DEPENDS}.p" 

}

# usage: install_from_system spell

uninstall_from_system()
{
  local SPELL="$1"

# we check if the spell is in the source system
  local packages="${INSTALL_ROOT}/var/state/sorcery/packages"
  if [ ! -f "${packages}" ]; then
    echo "spell ${SPELL} is missing"
	exit 1
  fi

  if ! grep -q "^${SPELL}:" "${packages}"; then
    echo "spell ${SPELL} is missing"
    exit 1
  fi

  local VERSION=`grep "^${SPELL}:" "${packages}" | cut -d: -f4 | head -n 1`

  echo "spell ${SPELL} version ${VERSION} is being investigated"

# find the install log
  local FILES="${INSTALL_ROOT}/var/log/sorcery/install/${SPELL}-${VERSION}"
  if [ ! -f "${FILES}" ]; then
    echo "spell ${SPELL} is missing an install log"
    exit 1
  fi

# uninstall dependencies

  local depends="${INSTALL_ROOT}/var/state/sorcery/depends"
  if [ -f "${depends}" ]; then
    grep "^${SPELL}:.*:on:.*" "${depends}" | cut -d: -f2 | cut -d'(' -f1|
	while read dep_spell;
	do
	  spell-binary-uninstall "${dep_spell}"
	done
  fi &&

# warning: the install log contains the install log itself thus,
# $FILES will be removed. It should not be a problem however.

# we sort files in reverse order to get directories after the files
# they contain

  cat "${FILES}" | sort -ur |
  while read file;
  do
	if [ -d "${INSTALL_ROOT}/${file}" ]; then
      rmdir "${INSTALL_ROOT}/${file}" || true
    else
	  rm -f "${INSTALL_ROOT}/${file}"
    fi || exit 1
  done &&

# update sorcery files

  update_sorcery_packages "${SPELL}" &&
  update_sorcery_depends "${SPELL}" &&
  remove_local_depends "${SPELL}" &&

  echo "spell ${SPELL} version ${VERSION} is uninstalled"
}

main()
{
  if [ $# -ne 1 ]; then
    echo "usage: $0 spell"
    exit 1
  fi

  local SPELL="$1"
  local C_SPELL=`echo "${SPELL}" | sed s/_/-/g`

# default values
  if [ -z "${BASE}" ]; then
    BASE=.
  fi

  if [ -z "${INSTALL_ROOT}" ]; then
    INSTALL_ROOT=/
  fi

# check if the spell is already installed first!
  local PACKAGES="${INSTALL_ROOT}/var/state/sorcery/packages"
  if [ ! -f "${PACKAGES}" ]; then
    echo "spell ${SPELL} is not installed"
  fi

  if ! grep -q "^${SPELL}:" "${PACKAGES}"; then
    echo "spell ${SPELL} is not installed"
    exit 0
  fi

# find the cache file
  uninstall_from_system "${SPELL}"

}

main "$@"
