#! /bin/sh
#
# Startup script for Usb  
# Author: Andre Rodrigues / Walter Guerra

pcsuite_disable () {
    /sbin/lsmod | grep g_nokia > /dev/null
    if [ $? = 0 ]; then
        logger "$0: removing g_nokia"
    
        initctl emit G_NOKIA_REMOVE
    
        PNATD_PID=`pidof pnatd`
        if [ $? = 0 ]; then
            kill $PNATD_PID
        else
            logger "$0: pnatd is not running"
        fi
        OBEXD_PID=`pidof obexd`
        if [ $? = 0 ]; then
            kill -HUP $OBEXD_PID
        else
            logger "$0: obexd is not running"
        fi
        SYNCD_PID=`pidof syncd`
        if [ $? = 0 ]; then
            kill $SYNCD_PID
        else
            logger "$0: syncd is not running"
        fi
    
        sleep 2
        /sbin/rmmod g_nokia
        if [ $? != 0 ]; then
            logger "$0: failed to rmmod g_nokia!"
            exit 1
        fi
    fi
}

mass_storage_disable() {
    /sbin/lsmod | grep g_file_storage > /dev/null
    if [ $? = 0 ]; then
        logger "$0: removing g_file_storage"
        initctl emit G_FILE_STORAGE_REMOVE
        /sbin/rmmod g_file_storage
    fi
}

switch_to_usb_network() {
    pcsuite_disable
    mass_storage_disable

    osso-mmc-mount.sh /dev/mmcblk0p1 /home/user/MyDocs/

    /sbin/lsmod | grep g_ether > /dev/null
    if [ $? != 0 ]; then
        /sbin/insmod /lib/modules/2.6.28-omap1/drivers/usb/gadget/g_ether.ko
        RC=$?
    fi

    if [ $RC != 0 ]; then
        logger "$0: failed to install g_ether"
        exit 1
    else
        sleep 2
    fi

    initctl emit --no-wait G_ETHER

    /sbin/ifup usb0
}

switch_to_file_storage() {
    /sbin/ifdown usb0
    /sbin/rmmod g_ether
    
    pcsuite_disable
    osso-mmc-umount.sh /dev/mmcblk0p1
    osso-usb-mass-storage-enable.sh /dev/mmcblk0p1
}

network_status() {
    ifconfig | grep usb0
    exit $?
}

pc_suite_status() {
    lsmod | grep g_nokia
    exit $?
}

charging_only_status () {
    RET=`cat /sys/devices/platform/musb_hdrc/gadget/gadget-lun0/file`
    if [ "x$RET" =  "x" ] # is not in mass storage
    then
        lsmod | grep g_nokia 
        if [ $? -eq 1 ] # g_nokia is not loaded
        then
            exit 0
        fi 
    fi 
    
    exit 1
}

switch_to_pc_suite () {
    /sbin/ifdown usb0
    /sbin/rmmod g_ether
    mass_storage_disable
    osso-mmc-mount.sh /dev/mmcblk0p1 /home/user/MyDocs/
    /usr/sbin/pcsuite-enable.sh
}

switch_to_charging_only () {
    /sbin/ifdown usb0
    /sbin/rmmod g_ether
    mass_storage_disable
    osso-mmc-mount.sh /dev/mmcblk0p1 /home/user/MyDocs/
    pcsuite_disable 
}

case "$1" in
start)
	echo "switching to network mode" >> /tmp/usbnet
    switch_to_usb_network
	;;
stop)
	echo "switching back to mass storage mode" >> /tmp/usbnet
    switch_to_file_storage
	;;
status)
    echo "checking network status" > /tmp/usbnet
    network_status
    ;;
suite)
    echo "changing usb to pc suite" >> /tmp/usbnet
    switch_to_pc_suite
    ;;	
suite_status)
    echo "checking pc_suite status" > /tmp/usbnet
    pc_suite_status
    ;;
charging_only)
    echo "changing mode to charging only" > /tmp/usbnet
    switch_to_charging_only
    ;;
charging_only_status)
    echo "checking charging only status" > /tmp/usbnet
    charging_only_status
    ;;
*)
	echo "usbnet syntax error" >> /tmp/usbnet
	printf "Usage: nm-usb-setup {start|stop|status}\n" >&2
	exit 1
	;;
esac

exit 0

