#!/bin/sh

# Calling Pashua GUI maker
itoggle_run() {

    # Write config file
    pashua_configfile=`/usr/bin/mktemp /tmp/pashua_XXXXXXXXX`
    echo "$1" > $pashua_configfile

    # Find Pashua binary. We do search both . and dirname "$0"
    # , as in a doubleclickable application, cwd is /
    # BTW, all these quotes below are necessary to handle paths
    # containing spaces.
    bundlepath="Pashua.app/Contents/MacOS/Pashua"
    if [ "$3" = "" ]
    then
        mypath=`dirname "$0"`
        for searchpath in "$mypath/Pashua" "$mypath/$bundlepath" "./$bundlepath" \
                          "/Applications/$bundlepath" "$HOME/Applications/$bundlepath"
        do
            if [ -f "$searchpath" -a -x "$searchpath" ]
            then
                pashuapath=$searchpath
                break
            fi
        done
    else
        # Directory given as argument
        pashuapath="$3/$bundlepath"
    fi

    if [ ! "$pashuapath" ]
    then
        echo "Error: Pashua could not be found"
        exit 1
    fi

    # Manage encoding
    if [ "$2" = "" ]
    then
        encoding=""
    else
        encoding="-e $2"
    fi

    # Get result
    result=`"$pashuapath" $encoding $pashua_configfile | sed 's/ /;;;/g'`

    # Remove config file
    rm $pashua_configfile

    # Parse result
    for line in $result
    do
        key=`echo $line | sed 's/^\([^=]*\)=.*$/\1/'`
        value=`echo $line | sed 's/^[^=]*=\(.*\)$/\1/' | sed 's/;;;/ /g'`       
        varname=$key
        varvalue="$value"
        eval $varname='$varvalue'
    done# itoggle_run()

# Checking if iPod IP address is in temporary memory
if [ -f "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.podip" ]
then
read IPOD < "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.podip"
fi

# Checking if iPod Sync Dir is in temporary memory
if [ -f "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.istore" ]
then
read ISTR < "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.istore"
fi

# Checking if info is in temporary memory
if [ -f "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.pstore" ]
then
read INFO < "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.pstore"
else
INFO="alpine"
fi

D="Disable iSync"
E="Re-Enable iSync"
EL="Re-Enable iSync with Battery Life Enhancement (0.4b minimum)"
UD="Uninstall iSync and Delete Sync Folder on Device"
UX="Uninstall iSync, Delete Sync Folder and Uninstall MobileFinder"
UM="Uninstall MobileFinder"

# Setting up GUI
conf="
*.transparency=0.94
*.title = iSync Disabler

podip.type = textfield
podip.label = iPod Network IP Address:
podip.default = $IPOD
podip.width = 180
podip.tooltip = Please do not enter your device's external internet IP address here

istore.type = textfield
istore.label = Name of iPod Sync Dir:
istore.width = 240
istore.default = $ISTR
istore.tooltip = What is the Sync dir on your iPod called?

pop.type = popup
pop.label = What would you like to do?
pop.width = 430
pop.option = $UX
pop.option = $UD
pop.option = $UM
pop.option = $D
pop.option = $E
pop.option = $EL
pop.default = $UX

cb.type=cancelbutton

";

icon0=$(dirname "$0")'/Installer.png';

if [ -e "$icon0" ]
then
    conf="$conf
         img0.type = image
         img0.x = 375
         img0.y = 250
         img0.path = $icon0"
fi

icon2=$(dirname "$0")'/Phone.png';

if [ -e "$icon2" ]
then
    conf="$conf
         img2.type = image
         img2.x = 90
         img2.y = 250
         img2.path = $icon2"
fi

icon1=$(dirname "$0")'/iSync.png';

if [ -e "$icon1" ]
then
    conf="$conf
         img1.type = image
         img1.x = 180
         img1.y = 70
         img1.path = $icon1"
fi

icon3=$(dirname "$0")'/SendFile.png';

if [ -e "$icon3" ]
then
    conf="$conf
         img3.type = image
         img3.x = 8
         img3.y = 253
         img3.path = $icon3"
fi

itoggle_run "$conf"

if [ "$cb" == 1 ]
then
echo iSync was not uninstalled. Operation canceled by user.
exit
fi

if [ "$pop" == "$D" ]
then
echo Disabling iSyncplease type the password ${INFO}.....
ssh -t root@${podipcrontab -r
echo iSync has now been Disabled. Use this utility to re-enable it.
fi

if [ "$pop" == "$E" ]
then
echo Re-Enabling iSyncplease type the password ${INFO}.....
ssh -t root@${podipcrontab /Applications/Scripts/crontab.txt
echo iSync has now been Re-Enabled.
fi

if [ "$pop" == "$EL" ]
then
echo Re-Enabling iSyncplease type the password ${INFO}.....
ssh -t root@${podipcrontab /Applications/Scripts/bltab.txt
echo iSync has now been Re-Enabled.
fi

if [ "$pop" == "$UD" ]
then
echo Uninstallingplease type the password ${INFO}.....
mkdir "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/"
echo "crontab -r" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /private/var/root/${istore}" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /private/var/root/.ssh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "mkdir /private/var/root/.ssh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /Applications/Scripts/" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm um.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
scp -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh" root@${podip}:/private/var/root
echo "ssh -t root@${podip} sh -i /private/var/root/um.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
echo Finalizingplease type the password ${INFO}.....
sh -i "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
echo iSync has been uninstalled
rm -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/"
fi

if [ "$pop" == "$UX" ]
then
echo Uninstallingplease type the password ${INFO}.....
mkdir "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/"
echo "crontab -r" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /private/var/root/${istore}" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /private/var/root/.ssh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "mkdir /private/var/root/.ssh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /Applications/Finder.app" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm -r /Applications/Scripts/" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo launchctl stop com.apple.SpringBoard >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
echo "rm um.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh"
scp -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/um.sh" root@${podip}:/private/var/root
echo "ssh -t root@${podip} sh -i /private/var/root/um.sh" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
echo Finalizingplease type the password ${INFO}.....
sh -i "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/lum.sh"
echo Uninstall successful - your iPod has been restarted.
rm -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/TScripts/"
fi

if [ "$pop" == "$UM" ]
then
echo Uninstalling MobileFinderplease type the password ${INFO}.....
ssh -t root@${podiprm -r /Applications/Finder.app
echo Restarting iPodplease type the password ${INFO}.....
ssh -t root@${podiplaunchctl stop com.apple.SpringBoard
echo Uninstall successful - your iPod has been restarted.
fi