#!/live/bin/sh

### BEGIN INIT INFO
# Provides:          live-usb-storage
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description:
# Description:       Create symlink to storage space directly on live-usb
### END INIT INFO

STORAGE_BIND=Live-usb-storage
 STORAGE_DIR=Live-usb-storage

export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin

. /live/lib/live-init-utils.sh
start_init_logging
load_translation

main() {
    case $1 in
        start) do_start   ;;
         stop) do_stop    ;;
            *) echo "Usage: $0 {start|stop}" ;;
    esac
    exit 0
}

do_start() {

    test -e /live/config/remasterable || exit 0

    . $INITRD_OUT

    CRIPPLED_FS=
    case $BOOT_FSTYPE in
        vfat|ntfs*) CRIPPLED_FS=true ;;
    esac

    local bdir root_dir
    for bdir in $SQFILE_DIR $TORAM_MP; do
        [ -d $bdir ] || continue
        root_dir=$bdir
        break
    done
    [ "$root_dir" ] || DISABLE=true

    read_cmdline

    test -e $root_dir/state/nostore && DISABLE=true
    [ -n "$DID_TORAM" -a -z "$TORAM_STORE" ] && DISABLE=true

    echo_script "$_Configure_Live_usb_storage_" $0

    mountpoint -q "$BOOT_MP" || DISABLE=true

    if [ "$DISABLE" ]; then
        echo_live "$_Disabling_live_usb_storage_"
        rmdir /root/$STORAGE_BIND /home/*/$STORAGE_BIND 2>/dev/null
        return
    fi

    local STORE_DIR=$BOOT_MP/$STORAGE_DIR
    local user
    for user in root $(get_home_users); do
        create_user_storage "$user"
    done
}

do_stop() {

    test -e /live/config/remasterable || exit 0

    echo_script "$_Disabling_live_usb_storage_" $0

    read_cmdline

    local dir bind_dir
    for dir in /root $(get_home_users 6); do
        bind_dir=$dir/$STORAGE_BIND
        test -d "$bind_dir" || continue
        mountpoint -q $bind_dir && umount $bind_dir
        rmdir $bind_dir
    done
}

create_user_storage() {
    local user=$1

    ent=$(getent passwd $user)
    local home=$(echo $ent | cut -d: -f6)
    local  uid=$(echo $ent | cut -d: -f3)
    local  gid=$(echo $ent | cut -d: -f4)

    local from_dir="$home/$STORAGE_BIND"
    local store_dir="$STORE_DIR/$user"

    if ! test -d "$home"; then
        echo_live "$_Home_directory_for_user_X_not_found_" "$(pquote $user)"
        return
    fi

    if [ ${#uid} -le 0 -o ${#gid} -le 0 ]; then
        echo_live "$_Could_not_find_UID_or_GUID_for_user_X_" "$(pquote $user)"
        return
    fi

    if ! echo $uid$gid | egrep -q "^[0-9]+$"; then
        echo_live "$_Found_non_numeric_UID_or_GUID_for_user_X_Y_" "$(pquote $user)"  $uid:$gid
        return
    fi

    if [ -n "$CRIPPLED_FS" -a $uid -ne 0 -a $uid -ne $USER_UID ]; then
        echo_live "$_Can_only_use_X_storage_with_UID_of_Y_" "$(pquote vfat live-usb)" "$(pquote $USER_UID)"
        return
    fi

    test -e $from_dir || mkdir -p $from_dir

    if ! test -d $from_dir; then
        echo_live "$_The_file_X_is_not_a_directory_" "$(pquote $from_dir)"
        return
    fi

    chown $uid:$gid $from_dir

    mkdir -p $store_dir
    [  -z "$CRIPPLED_FS" ] && chown $uid:$gid $store_dir

    if mountpoint -q $from_dir; then
        echo_live "$_Directory_X_is_already_a_mountpoint_" "$(pquote $from_dir)"
        return
    fi
    echo_live "$_Enable_live_usb_storage_at_X_" "$(pquote $from_dir)"
    mount --bind $store_dir $from_dir
}

#------------------------------------------------------------------------------
# Return a list of user names with a shell like /bin/*sh with a home directory
# under /home/ and with a UID of 1000 or over.
#------------------------------------------------------------------------------
get_home_users() {
    local field=${1:-1}
    local ent uid
    while read ent; do
        [ -z "$ent" ]    && continue
        uid=$(echo $ent | cut -d: -f3)
        [ -z "$uid" ]     && continue
        [ $uid -lt 1000 ] && continue
        echo $ent | cut -d: -f$field
    done<<Home_Ents
$(grep  ":/home/.*:*/bin/.*sh$" /etc/passwd)
Home_Ents
}

read_cmdline() {
    : ${CMDLINE:=$(cat /live/config/proc-cmdline /live/config/cmdline2)}
    local param
    for param in $CMDLINE; do
        value=${param#*=}
        case $param in
              nostore)  DISABLE=true       ;;
        esac
    done
}


main "$@" 2>&1 | tee -a $INIT_LOG_FILE

sync

exit 0
