#!/bin/sh

# Calling Pashua GUI maker
isync_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# isync_run()

# Checking Mac's IP address
IP=`ifconfig en1 | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'`

# 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 FSTORE is in temporary memory
if [ -f "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.fstore" ]
then
read MFILE < "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.fstore"
fi

# Checking if ipod pass 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

RO="Fresh Install"
RT="Add IP's for New Network"

# Setting up GUI
conf="
*.transparency=0.94
*.title = iSync by Francisois
#tb.type = text
#tb.default = Make sure you have installed the BSD Subsystem and OpenSSH on your jailbroken iPhone/iPod Touch from Installer. Fill in the required fields on the left.[return][return]Your iPhone/iPod Touch MUST be powered on and unlocked, with Wi-Fi connected to the same network as your Mac, otherwise the utility will fail. If the above conditions are met but the utility still fails, please ensure that your router allows connections on port 22.[return][return]When you press OK, this window will be replaced by a terminal screen. You will be prompted for the device root password 3 times, type alpine and hit enter. You will then get a message saying 'Generating public/private dsa key pair.' Around 10 seconds later, you will be prompted twice for a passphrase, just hit enter. Next, you will be asked 'Are you sure you want to continue connecting yes/no', type yes and hit enter. You will then be prompted once for your Mac's password, type your password and hit enter. Now fill up your Mac's sync dir with stuff and you're set to go!
#tb.height = 276
#tb.width = 238
#tb.x = 340
#tb.y = 90

rb1.type = radiobutton
rb1.label = Choose Installation Type:
rb1.option = $RO
rb1.option = $RT
rb1.default = $RO

ipaddr.type = textfield
ipaddr.label = Local Mac IP Address:
ipaddr.default = $IP
ipaddr.width = 180
ipaddr.tooltip = Please do not enter your external internet IP address hereread the readme.txt for more information

podip.type = textfield
podip.label = Local iPod IP Address:
podip.default = $IPOD
podip.width = 180
podip.tooltip = Please do not enter your external internet IP address hereread the readme.txt for more information

istore.type = textfield
istore.label = Name your iPhone/iPod sync folder:
istore.width = 180
istore.default = Sync
istore.tooltip = This folder will be in /private/var/root on your iPhone/iPod

fstore.type = openbrowser
fstore.label = Select your Mac's sync folder:
fstore.width310
fstore.default = $MFILE
fstore.tooltip = Sync Folder

rb.type = radiobutton
rb.label = Sync every ... minutes:
rb.option = 20
rb.option = 30
rb.option = 60
rb.default = 30

chk.type = checkbox
chk.label = ExtraInstall MobileFinder on your device?
chk.default = 0

chk2.type = checkbox
chk2.label = Enhance battery life by managing ssh server?
chk2.default = 0

chk3.type = checkbox
chk3.label = Change default iPod password?
chk3.default = 0

pstore.type = textfield
pstore.label = If sowhat will you change your password to?
pstore.width = 180
pstore.default = $INFO
pstore.tooltip = This to simplify the instructions only and has no effect on the actual procedure

cb.type=cancelbutton

";

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

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

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

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

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

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

icon4=$(dirname "$0")'/iPod.png';

if [ -e "$icon4" ]
then
    conf="$conf
         img4.type = image
         img4.x = 253
         img4.y = 580
         img4.path = $icon4"
fi

isync_run "$conf"

# Did the user cancel?
    if [ "$cb" == 1 ]
    then
    echo No changes have been made to your device. Please run iSync again.
    exit
    fi

# Installation type
if [ "$rb1" == "$RO" ]
then

rm -rf "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts"

echo ${podip} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.podip"
echo ${fstore} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.fstore"
echo ${istore} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.istore"


mkdir "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/"

# Creating rsync shell script
echo "Setting up Synchronization Script ......."
echo rsync -avz ${macuser}@${ipaddr}:${fstore}/ /private/var/root/${istore} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync.sh"
echo rsync -avz /private/var/root/${istore}/ ${macuser}@${ipaddr}:${fstore} >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync.sh"

# Battery Life Crontab

echo "0 * * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
echo "1 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
echo "10 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    
    if [ "$rb" == 20 ]
    then
    echo "19 * * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "20 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "40 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "50 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    fi
    
    if [ "$rb" == 30 ]
    then
    echo "29 * * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "30 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "40 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    fi

echo "0 * * * * /Applications/Scripts/rsync.sh" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    
    if [ "$rb" == 20 ]
    then
    echo "20 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    echo "40 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    fi
    
    if [ "$rb" == 30 ]
    then
    echo "30 * * * * /Applications/Scripts/rsync.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    fi

# Creating ssh_authkey shell script and installing MobileFinder
echo "Setting up SSH Authentication between this computer and your iPhone ......."
    
    if [ "$chk3" == 1 ]
    then
    echo "echo 'Type ${pstore} below ........'" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
    echo "passwd" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
    echo ${pstore} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/Temp/.pstore"
    fi

echo "echo 'Please keep the passphrase empty or setup will fail .......'" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
echo "ssh-keygen -t dsa -f ~/.ssh/id_dsa" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
echo "cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
echo "echo 'Please type yes and enter your Mac password below .......'" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
echo "cat ~/.ssh/id_dsa.pub | ssh ${LOGNAME}@${ipaddr} 'cat - >> ~/.ssh/authorized_keys'" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
    
    if [ "$chk2" == 1 ]
    then
    echo "crontab /Applications/Scripts/bltab.txt" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
    else
    echo "crontab /Applications/Scripts/crontab.txt" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
    fi
    
echo "echo 'iSync has been successfully installed!'" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
echo "launchctl stop com.apple.SpringBoard" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync.sh"

    if [ "$chk" == 1 ]
    then
    echo "Installing, please type ${pstore} in the password field below ......."
    rsync -avz "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/" root@${podip}:/Applications
    else
    echo "Copying Scripts, please type ${pstore} in the password field below ......."
    scp -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts" root@${podip}:/Applications/
    fi

# Running scripts on iPod via ssh

echo "Installing, please type ${pstore} in the password field below ......."

echo "ssh -t root@${podip} sh -i /Applications/Scripts/isync.sh" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"
sh -i "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"

fi

if [ "$rb1" == "$RT" ]
then

# Creating rsync shell script
echo "Setting up Synchronization Script ......."
echo rsync -avz ${macuser}@${ipaddr}:${fstore}/ /private/var/root/${istore} > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync2.sh"
echo rsync -avz /private/var/root/${istore}/ ${macuser}@${ipaddr}:${fstore} >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync2.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/rsync2.sh"

# Battery Life Crontab

echo "2 * * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
echo "3 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
echo "11 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    
    if [ "$rb" == 20 ]
    then
    echo "20* * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "21 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "41 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "51 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    fi
    
    if [ "$rb" == 30 ]
    then
    echo "30 * * * * launchctl load /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "31 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    echo "41 * * * * launchctl unload /Library/LaunchDaemons/com.openssh.sshd.plist" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/bltab.txt"
    fi

echo "2 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    
    if [ "$rb" == 20 ]
    then
    echo "22 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    echo "42 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    fi
    
    if [ "$rb" == 30 ]
    then
    echo "32 * * * * /Applications/Scripts/rsync2.sh" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/crontab.txt"
    fi

    if [ "$chk2" == 1 ]
    then
    echo "crontab /Applications/Scripts/bltab.txt" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync2.sh"
    else
    echo "crontab /Applications/Scripts/crontab.txt" >> "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync2.sh"
    fi
    
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/isync2.sh"

echo "Installing updated script, please type ${pstore} in the password field below ......."
scp -r "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts" root@${podip}:/Applications/

# Running scripts on iPod via ssh
echo "Finalizing, please type ${pstore} in the password field below ......."
echo "ssh -t root@${podip} sh -i /Applications/Scripts/isync2.sh" > "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"
chmod +x "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"
sh -i "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts/setup.sh"
rm -rf "/Applications/iSync/AppFiles/AppFiles.app/Contents/MacOS/SendFiles/Scripts"
fi