#!/bin/bash

HELP_INFO='
Mount script utility (sshfs of curlftpfs) footer file

Example of your mount script:
    cd "$(dirname "$0")"
    MOUNT_TYPE="SSH" # or "FTP", optional, default is "SSH"
    LOGIN="user" # optional, default is current user
    SERVER="domain.org"
    REMOTE_PATH="/home/user/remote_dir"
    LOCAL_PATH="~/local_dir"
    source mount.bash # include this utility

Version: 0.3
Author: Viacheslav Lotsmanov (unclechu)
Contact: http://unclechumind.blogspot.ru/p/contact-me.html
'

check_required_fields () {
    echo Checking for required fields

    if [ -z "$SERVER" ]; then
        echo '"SERVER" is required field' 1>&2
        ERR=1
    fi

    if [ -z "$REMOTE_PATH" ]; then
        echo '"REMOTE_PATH" is required field' 1>&2
        ERR=1
    fi

    if [ -z "$LOCAL_PATH" ]; then
        echo '"LOCAL_PATH" is required field' 1>&2
        ERR=1
    fi

    if [ -n "$ERR" ]; then
        echo Some required fields is not set, terminating 1>&2
        exit 1
    fi

    if [ -z "$MOUNT_TYPE" ]; then
        MOUNT_TYPE="SSH"
        echo \"MOUNT_TYPE\" field is not set, will use default value: \"$MOUNT_TYPE\"
    fi

    if [ "$MOUNT_TYPE" != "SSH" ] && [ "$MOUNT_TYPE" != "FTP" ]; then
        echo 'Wrong value of "MOUNT_TYPE" field, you can use only "SSH" (for sshfs) or "FTP" (for curlftpfs)' 1>&2
        exit 1
    fi

    if [ -z "$LOGIN" ]; then
        LOGIN="$(whoami)"
        echo \"LOGIN\" field is not set, will use current user: \"$LOGIN\"
    fi

    ABSOLUTE_LOCAL_PATH="$(echo "$(pwd)/$LOCAL_PATH" | sed 's/\/\.\//\//g')"

    echo All required fields is correct
}

mount () {
    echo Start mounting process

    check_required_fields

    echo Check for already mounted point
    grep "$LOCAL_PATH" /etc/mtab > /dev/null
    if [ $? -eq 1 ]; then
        echo Mount type\: \""$MOUNT_TYPE"\"

        echo Connecting to server \""$LOGIN"@"$SERVER"\"
        
        if [ "$MOUNT_TYPE" == "SSH" ]; then
            echo sshfs -o workaround=rename ""$LOGIN"@"$SERVER":"$REMOTE_PATH"" "$LOCAL_PATH"
            sshfs -o workaround=rename ""$LOGIN"@"$SERVER":"$REMOTE_PATH"" "$LOCAL_PATH"
        else
            echo curlftpfs "ftp://"$SERVER"/"$REMOTE_PATH"" "$LOCAL_PATH" -o user=$LOGIN,uid=$(id -u $(whoami)),gid=$(id -g $(whoami))
            curlftpfs "ftp://"$SERVER"/"$REMOTE_PATH"" "$LOCAL_PATH" -o user=$LOGIN,uid=$(id -u $(whoami)),gid=$(id -g $(whoami))
        fi

        if [ $? -eq 0 ]; then
            echo Remote path \""$REMOTE_PATH"\" mounted as \""$ABSOLUTE_LOCAL_PATH"\"
        else
            echo Error 1>&2
            exit 1
        fi
    else
        echo Can\'t mount to \""$ABSOLUTE_LOCAL_PATH"\" because this point is already mounted 1>&2
        exit 1
    fi
}

unmount () {
    echo Start unmounting process

    check_required_fields
    
    echo Try to unmount point \""$LOCAL_PATH"\"
    fusermount -u "$LOCAL_PATH"
    if [ $? -eq 0 ]; then
        echo Mount point \""$ABSOLUTE_LOCAL_PATH"\" is unmounted
    else
        echo Can\'t unmount point \""$ABSOLUTE_LOCAL_PATH"\" 1>&2
        exit 1
    fi
}

ACTION="MOUNT"

for ARG in "$@"; do
    if [ "$ARG" == "-h" ] || [ "$ARG" == "--help" ]; then
        ACTION="HELP"
    fi
    if [ "$ARG" == "-u" ] || [ "$ARG" == "--unmount" ]; then
        if [ "$ACTION" != "HELP" ]; then
            ACTION="UNMOUNT"
        fi
    fi
done

case "$ACTION" in
    "HELP") echo "$HELP_INFO" ;;
    "MOUNT") mount ;;
    "UNMOUNT") unmount ;;
    *)
        echo Unknown action 1>&2
        exit 1
        ;;
esac

exit 0

