#!/bin/bash
# This script is used to provide a GUI for 'activate'
# It has the same syntax like 'activate', moreover it accepts HTTP addresses
# to be mounted remotely using httpfs
# (you may xactivate a XZM module over HTTP, no need to download it!)

PATH=.:$(dirname $0):/usr/lib:$PATH
. liblinuxlive || exit 127
FILE="$1"
TITLE="Porteus activator"
DMP=/tmp/dmp$$.tmp

# get extension of file
EXT=${FILE##*.}
t=/tmp/xzmod$$

# Convert an sq4 module to .xzm
convert_lzm_xzm() {
Xdialog --title "$TITLE" --yesno "Would you like to convert this module to xzm before activating?" 6 70
[ $? -eq 1 ] && exit

#[ ! -d $t ] && mkdir $t
#rm -rf $t/*
mkdir $t
FPATH=`dirname "$FILE"`
FMOD=${FILE##*/}
NAMETMP=${FMOD%.*}
NAME=`echo $NAMETMP|sed 's/\.sq4.*//'`
CHK=`file $FILE|grep endian`

# Convert module based on sq3/sq4 sniff
if [ -z "$CHK" ]; then
   # Uncomment this next line for no sq3 support
   # Xdialog --title "$TITLE" --msgbox "This is a squash3 module (probably 32bit). Refusing conversion." 0 0; exit

   # Comment these next 6 lines out for no sq3 support
   Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Unsquashing: $FILE" 0 0 3000 &
   unsquashfs3 -f -dest $t $FILE
   wait
   Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Repacking: $FILE" 0 0 4000 &
   dir2xzm $t $FPATH/$NAME.xzm
   wait
    else
   Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Please wait: unsquashing $FILE" 0 0 3000 &
   unsquashfs -f -dest $t $FILE
   wait
   Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Please wait: repacking $FILE" 0 0 4000 &
   dir2xzm $t $FPATH/$NAME.xzm
   wait
fi

# Uncomment these for no sq3 support
#( xzm2dir $FILE $t ) | Xdialog --no-buttons --no-close --infobox "Unsquashing: $FILE" 0 0 0
#( dir2xzm $t $FPATH/$name.xzm ) | Xdialog --no-buttons --no-close --infobox "Repacking: $FILE" 0 0 0
rm -rf $t
Xdialog --title "$TITLE" --no-buttons --no-close --infobox "Your module has been converted: \n $FPATH/$NAME.xzm" 0 0 3000

FILE=$FPATH/$NAME.xzm
}

# If extension is .lzm make sure file is sq4
# uncomment these for Porteus v2 (no .lzm support)
if [ "$EXT" == "lzm" ]; then
#   sq=`file $1 | grep "endian"`
#      if [ -n $sq ]; then
#         kdialog --msgbox "Sorry, this is a squash 3 file. Please convert to squash 4"
#        exit
#            else
        convert_lzm_xzm
#      fi
fi

# make sure to work only with *.xzm files
while [ "$(echo "$1" | egrep '.xzm$|.lzm$')" = "" -a "$1" != "" ]; do shift; done

if [ "$FILE" = "" ]; then
   Xdialog --title "$TITLE" --backtitle "Choose a module to be activated: " --fselect /mnt/*.xzm 0 0 2> $DMP
   if [ $? -ne 0 ]; then exit 1; fi
   FILE=`cat $DMP` && rm $DMP
   FILE=$(echo "$FILE" | sed -r "s,file://,,")
fi

BASE=$(basename "$FILE" 2>/dev/null)

# if the module is already activated, suggest deactivation
if ismountpoint "/mnt/live/memory/images/$BASE"; then
   Xdialog --title "$TITLE" --yesno "$BASE:\nModule is already activated. Deactivate?" 0 0
   if [ $? -eq 0 ]; then deactivate $BASE; exit 0; fi
   exit 2
fi

#SLIK=${FILE:0:4}

# handle slik (Slax Klik) and HTTP requests/urls
#if [ "$SLIK" = "slik" -o "$SLIK" = "http" ]; then

   # if a module is loaded from untrusted site (everything else than slax.org, ask for confirmation)
   #DOMAIN=$(echo "$FILE" | cut -b 8- | egrep -o "^(www.)?([^/]+)")
   #DOMAIN=${DOMAIN##www.}
   #if [ "$DOMAIN" != "slax.org" ]; then
    #  Xdialog --title "$TITLE" --yesno "Warning! You are about to load a module from $DOMAIN...\nThis may be unsafe. Are you sure?" 0 0
    #  if [ $? -ne 0 ]; then exit 127; fi
   #fi

   #MNT="/mnt/live/memory/httpfs/$BASE"
   #mkdir -p "$MNT"

   #Xdialog --title "$TITLE" --no-buttons --no-close --infobox "$BASE: module insertion in progress, wait please..." 0 0 9000 &
   #KILLPID=$!
   #ERR=$(httpfs http${FILE:4} "$MNT" 2>&1)
   #if [ $? -ne 0 ]; then
    #  kill $KILLPID
    #  Xdialog --title "$TITLE" --msgbox "Can't mount the module remotely.\nError message: $ERR\n\nTry to download the module to your computer." 0 0
    #  exit 128;
   #fi
   #echo "http${FILE:4}" >$MNT/slik.url
   #kill $KILLPID
   #FILE="$MNT/$BASE"
#fi
echo "About to insert module"
# 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 insertion in progress, wait please..." 0 0 9000 &
KILLPID=$!

# 'Stale NFS' workaround
cd /usr/share/applications

# insert the module to live filesystem
/opt/porteus-scripts/activate "$FILE" -k

if [ $? -ne 0 ]; then
   kill $KILLPID
   Xdialog --title "$TITLE" --msgbox "Some error occured while activating the module." 0 0
   exit 3
else
   kill $KILLPID
   Xdialog --title "$TITLE" --no-close --no-buttons --infobox "Well done! $BASE: module activated successfully." 0 0 3000 &
fi

exit 0
