#!/bin/sh
# ----------------------------------- #
# iPod Touch & iPhone
# mounting / unmounting script
# by: Mario Limonciello, November 2007
# ----------------------------------- #

usage()
{
    echo " This package is useful for issuing an unmount and unmount commands in "
    echo " and application such as Amarok.  It gets around problems that are normally "
    echo " encountered with things such as ArtworkDB mmap problems and annoyances "
    echo " with having to type commands in order to just sync your iPod. "
    echo "*************************************************************************"
    echo " It is *HIGHLY* recommended that you set up public/private "
    echo " SSH keys before you use this script "
    echo "*************************************************************************"
}

#Load configurable items
. /etc/default/ipod-convenience

PROGRAM=`basename $0`

#Check our permissions and directory structure
if [ ! -d "$MOUNTPOINT" ]; then
    mkdir -p $MOUNTPOINT
    if [ ! -d "$MOUNTPOINT" ]; then
        echo "We tried to make your directory $MOUNTPOINT, but couldn't.  Please make it yourself and try again."
        exit 1
    fi
fi
if touch $MOUNTPOINT/test 2>&1 > /dev/null; then
    rm $MOUNTPOINT/test
else
    echo "Unable to write to $MOUNTPOINT.  Please adjust permissions and try again."
    exit 2
fi

#Make sure we have sshfs installed
if ! which sshfs 2>&1 > /dev/null; then
    echo "We don't have sshfs installed.  Please install and try again."
    exit 3
fi

#Make sure we are in the fuse group
if ! groups | grep fuse 2>&1 > /dev/null; then
    echo "Please add yourself to the fuse group, logout/in and try again."
    exit 4
fi

#Check that we can ping the ipod
if ! ping $IPADDRESS -c 1 2>&1 > /dev/null; then
    echo "iPod is not responding to pings at $IPADDRESS."
    echo "Please set the environment variable IGNOREPING if you want to ignore this."
    if [ -z "$IGNOREPING" ]; then
        exit 5
    fi
fi

if [ "$PROGRAM" = "iphone-mount" ] || [ "$PROGRAM" = "ipod-touch-mount" ]; then

    #Mount our Music Directory
    #work around if its 1.1.3+
    if ssh root@$IPADDRESS test -d /var/mobile/Media; then
        sshfs root@$IPADDRESS:/var/mobile/Media $MOUNTPOINT/ -o workaround=rename
    else
        sshfs root@$IPADDRESS:/var/root/Media $MOUNTPOINT/ -o workaround=rename
    fi

    if [ $? != 0 ]; then
        echo "Failed to mount $MOUNTPOINT"
        exit 6
    fi

    mkdir -p $MOUNTPOINT/iTunes_Control/Music

    #Check that we have iPhone/iPod touch symbolic links in place
    if [ ! -e $MOUNTPOINT/iPod_Control ]; then
        ln -s iTunes_Control $MOUNTPOINT/iPod_Control
    fi
    if [ ! -e $MOUNTPOINT/iTunes ]; then
        ln -s . $MOUNTPOINT/iTunes
    fi

    #Check if we have a FirewireGUID
    if [ ! -e $MOUNTPOINT/iTunes_Control/Device/SysInfo ] || ! grep FirewireGuid $MOUNTPOINT/iTunes_Control/Device/SysInfo 2>&1 > /dev/null; then
        echo "You don't have a Firewire GUID, so we will create one"

        # Make the directory and file
        mkdir -p $MOUNTPOINT/iTunes_Control/Device
        # try regular user
        lsusb=$(/usr/sbin/lsusb -v -d 05ac: 2>/dev/null)
        if [ $? != 0 ]; then
            # try sudo
            lsusb=$(sudo lsusb -v -d 05ac: 2>/dev/null)
        fi
        guid=$(echo "$lsusb" | awk '/iSerial/{printf("FirewireGuid: 0x%s\n", substr($3, 1, 16))}')
        if [ -z "$guid" ]; then
            echo "Failed to retrieve Firewire GUID, is it connected via USB?"
            echo "See https://help.ubuntu.com/community/PortableDevices/iPhone for more information."
            exit 7
        fi

        echo "$guid"
        echo "$guid" >> $MOUNTPOINT/iTunes_Control/Device/SysInfo
    fi


elif [ "$PROGRAM" = "iphone-umount" ] || [ "$PROGRAM" = "ipod-touch-umount" ]; then
    fusermount -u $MOUNTPOINT
    if [ $? != 0 ]; then
        # umount failed, likely wasn't mounted at all
        exit 0
    fi
    # NOTE: `ps' is run on device, `awk' is run locally
    PROCESS=`ssh root@$IPADDRESS ps -e | awk '/MobileMusicPlayer/{print $1}'`
    if [ "$PROCESS" ]; then
        ssh root@$IPADDRESS kill $PROCESS
    fi
else
    usage
fi
