#!/bin/sh
# dotpup handler 0.0.2 - GuestToo - Sep 15, 2005
### always, never options
### removed geometry for gxmessage
### changed rm -rf $DPDIR to rm -rf /root/DotPupTmpDir (just in case)
### read DPDIR from $CONFIG ??

FNAME=`basename "$1"`
CONFIG=/etc/dotpup
DPDIR=/root/DotPupTmpDir

if which gxmessage
then
  ##### MSG='gxmessage -geometry 640x160'
  MSG=gxmessage
else
  MSG=xmessage
fi

[ -d $CONFIG ] || mkdir $CONFIG

if [ ! -r "$1" ]
then
  $MSG -center -title "DotPup Error" "File not found: $FNAME"
  exit 1
fi


# ----- test zip integrity

if ! unzip -t "$1"
then
  $MSG -center -title "DotPup Error" "File integrity ERROR: $FNAME

Suggestion: Download the Pup file again"
  exit 1
fi


# -----

if [ ! -r $CONFIG/always-unzip ]
then
  $MSG -buttons "Unzip the Pup File:21,Always:22,Cancel" -center -title "DotPup" "File integrity is OK: $FNAME"
  case $? in
    21) true ;;
    22) touch $CONFIG/always-unzip ;;
    *) exit ;;
  esac
fi


# ----- setup clean workspace

[ -d "$DPDIR" ] && rm -rf /root/DotPupTmpDir
mkdir "$DPDIR"
cd "$DPDIR"


# ----- unzip the dotpup

if ! unzip -o "$1"
then
  $MSG -center -title "DotPup Error" "ERROR unzipping: $FNAME"
  exit 1
fi


# ----- run dotpup.sh script

if [ -r dotpup.sh ]
then
  if [ ! -r $CONFIG/always-run ]
  then
    $MSG -buttons "Run the Installer:21,Always:22,Cancel" -center -title "DotPup" "DO NOT RUN THE INSTALLER
IF YOU ARE NOT SURE IT IS SAFE"
    case $? in
      21) true ;;
      22) touch $CONFIG/always-run ;;
      *) exit ;;
    esac
  fi
  chmod a+x dotpup.sh
  "$DPDIR/dotpup.sh"
  # popup a message if an error code was returned??
fi


# ----- cleanup the working space

rm -rf /root/DotPupTmpDir


# ----- delete the dotpup file? (this section is not really necessary)

sleep 1
ALWAYS=$CONFIG/always-delete-dotpup
NEVER=$CONFIG/never-delete-dotpup
[ -r $ALWAYS ] && exec rm -f "$1"
[ -r $NEVER ] && exit
$MSG -buttons "Delete the DotPup File:21,Always:22,Never:23,No" -center -title "DotPup" "Delete the file $FNAME ?"
case $? in
  21) rm -f "$1" ;;
  22) touch $ALWAYS
      rm -f $NEVER "$1" ;;
  23) touch $NEVER
      rm -f $ALWAYS ;;
  *) exit ;;
esac
