#!/bin/bash

FILE="$1"
BASE=$(basename "$FILE" .xzm).xzm
HTTP=/mnt/live/memory/httpfs

# show info message and remember PID of the process (we'll need to kill the message later on)
Xdialog --title "$TITLE" --no-buttons --no-close --infobox "$BASE: module deactivation in progress, wait please..." 0 0 9000 & KILLPID=$!

# remove the module from the live filesystem
/opt/porteus-scripts/deactivate -k "$FILE"
err=$?

if [ $err -eq 1 ]; then
   kill $KILLPID
   Xdialog --title "deactivate error" --msgbox "Module is not activated" 0 0
   exit 4
fi

if [ $err -ne 0 ]; then
   kill $KILLPID
   Xdialog --title "deactivate error" --msgbox "Module can't be deactivated. Perhaps some files from the module are still used, opened or executed." 0 0
   exit 3
else
   # if http/slik module was mounted, clean up
   if [ -d "$HTTP/$BASE" ]; then
      umount "$HTTP/$BASE"
      rm -f "$HTTP/$BASE/slik.url"
      rmdir "$HTTP/$BASE"
   fi
   # Rebuild the system configuration cache
   # not sure if necessary but wont hurt
   kill $KILLPID
   Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Well done! $BASE: module deactivated." 0 0 3000 &
fi

exit 0
