#!/bin/sh

# Uncomment the following line to get debug info
#set -x

SELF=${SELF:-$(basename $0)}
LOCK_FILE=/var/lock/share.lock

source /usr/local/lib/utils
source usb_gadget

# The USB network interface file
USB_IF=/etc/network/interfaces.d/usb0

# The USB mass storage backing store file or partition
BACKING_STORE_FILE=/sys/kernel/config/usb_gadget/FunKey/functions/mass_storage.mmcblk0p4/lun.0/file

# Unmount a local share
unmount_share () {

    umount /mnt || die 1 "cannot unmount the backing store partition"
    return 0
}

# Mount a share locally
mount_share () {

    mount | grep -q /dev/mmcblk0p4
    if [ $? -ne 0 ]; then

	# Check if the FAT32 partition has been unmounted cleanly
	fsck.fat -n /dev/mmcblk0p4 2>/dev/null | egrep -q "Dirty bit"
	if [ $? -eq 0 ]; then

	    # The FAT32 partition was not cleanly unmounted, try to
	    # clean it
	    warn "the backing store partition was not properly unmounted"
	    #warn "the backing store partition was not properly
	    #unmounted, clean it"
    	    # fsck.fat -a -t -w /dev/loop0 >/dev/null 2>&1
	    # if [ $? -gt 1 ]; then
	    #     die 3 "cannot clean backing store file"
	    # fi
	fi

	# Mount the backing store file locally
	mount /mnt/ || die 4 "cannot mount backing store partition"
    fi

    # Create the directory structure if required
    (cd /mnt; mkdir -p "Atari lynx" "Game Boy" "Game Boy Color" "Game Boy Advance" "Game Gear" "Neo Geo Pocket" "NES" "PS1" "Sega Genesis" "Sega Master System" "SNES" "WonderSwan")
    return 0
}

# Initialize the backing store if needed and mount it locally
init_share () {
    if [ -f ${LOCK_FILE} ]; then
	return 0
    fi
    touch ${LOCK_FILE}

    # Initialize the USB gadget
    init_usb_gadget

    # Mount the backing store partition locally
    mount_share
    return 0
}

# Stop sharing the backing store partition
stop_share () {

    # Stop sharing the backing store partition
    echo > $BACKING_STORE_FILE || die 11 "the backing store partition is still mounted on host"

    # 3) Mount the backing store partition locally
    mount_share

    info "stopped sharing the backing store partition"

    return 0
}

# Start sharing the backing store partition
start_share () {

    # 1) Check if the backing store partition is already shared
    if [ -e $BACKING_STORE_FILE ]; then
	cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4 && die 12 "the backing store partition is already shared"
    fi

    # 2) Check if USB data is connected
    is_usb_data_connected > /dev/null 2>&1 || die 13 "USB sharing impossible, not connected to a host"

    # 3) Unmount the backing store partition if it is locally mounted
    unmount_share

    # 4) Everything is now clear to start sharing the backing store partition
    info "start sharing the backing store partition"
    echo /dev/mmcblk0p4 > $BACKING_STORE_FILE || die 14 "cannot share the backing store partition"
}

# Return if currently sharing
is_share_started () {

    if [ ! -e $BACKING_STORE_FILE ]; then
	echo "no"
	return 1
    fi
    cat $BACKING_STORE_FILE | egrep -q ^/dev/mmcblk0p4
    local res=$?
    if [ "$res" == "0" ]; then
        echo "yes"
    else
        echo "no"
    fi
    return $res
}

case "$1" in

    init)
	init_share
	;;

    start)
	start_share
	;;
    
    stop)
	stop_share
	;;

    is_sharing)
        is_share_started
        ;;


    *)
	die 15 "Usage $0 {init|start|stop|is_sharing}"
	;;
esac
exit $?
