#!/bin/sh
#
# VMTux 2.0
#
# vmt-uninstall
#
. /etc/vmt/vmt-colors
. /etc/vmt/vmt-conf

PATH=/bin:/sbin

umask 022

if [ "$(id -u)" -eq 0 ]; then
  echo "${BRed}Please run as user.${Reset}" 
  exit 1; 
fi

if [ ! -d $PKG ]; then
  echo "${BRed}No packages directory found.${Reset}" 
  exit 1; 
fi

if [ -z "$1" ]; then echo "Usage: $0 package-name(s)"; exit 0; fi

function UnInstall()
{
  APP="${1%.squ}"
  APP="${APP%.tcz}"
  
  BASE=${LOOP}/${APP}

  echo -n "${BBlue}Un-Installing ${BYellow}${APP}${Reset} "
  
# busybox find has no -lname option (yet)   
#  sudo find /usr -lname "${LOOP}/${APP}/*" -delete

  find /usr/* -type l -exec readlink -nf {} ';' -exec echo " {}" ';' | \
    grep "${LOOP}/${APP}/" | \
    awk '{ print $2 }' | sudo xargs rm -f

  sync ; sync
  
  sudo ldconfig
  sudo depmod

  if [ -d $BASE ]; then 
    sudo umount "${BASE}"
    sudo rmdir "${BASE}"
  fi

  if [ -f "${BOOTLIST}" ]; then
    sed "/^${APP}$/d" -i ${BOOTLIST}
  fi
  
  rm -f ${PKG}/${APP}.squ
  rm -f ${PKG}/${APP}.squ.dep
  rm -f ${PKG}/${APP}.squ.md5.txt

  rm -f ${PKG}/${APP}.tcz
  rm -f ${PKG}/${APP}.tcz.dep
  rm -f ${PKG}/${APP}.tcz.md5.txt

  echo "${BGreen}done.${Reset}"
}


for APP in "$@"; do
  UnInstall ${APP}
  sed "/^${APP}$/d" -i ${BOOTLIST}
done

exit 0
