#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
el_make_environment
. gettext.sh
TEXTDOMAIN="elive-tools"
export TEXTDOMAIN



main(){
    # pre {{{
    local file where mode
    local message_unabletofound

    # Usage
    if [[ -z "${3}" ]] ; then
        echo -e "Usage: $(basename $BASH_SOURCE) where mode file[s]"
        echo -e "Small tool helper for send files to specific locations using a fuzzy matching system, for example:"
        echo -e "$ $(basename $BASH_SOURCE) dropbox link $HOME/photo.jpg"
        echo -r "it will search for approximate matching of your dropbox dir in your home and link the file to this directory, use with caution"
        echo -e "Modes: copy | link"
        exit 1
    fi

    where="$1"
    shift
    mode="$1"
    shift

    # }}}
    if [[ ! -e "$where" ]] ; then
        # approximate matching finder if is not absolute location
        el_dependencies_check "find-approximate-matching-homedir"

        where="$( find-approximate-matching-homedir "$where" )"
        el_warning "Using approximate match: $where"
    fi

    # check and report if target is not found
    if [[ ! -d "$where" ]] ; then
        if echo "$where" | grep -Fqs "dropbox" ; then
            message_unabletofound="$( printf "$( eval_gettext "Unable to find the location of %s. Have you configured your Dropbox account? You can find it in your application's menus." )" "$where" )"
        else
            message_unabletofound="$( printf "$( eval_gettext "Unable to find the location of %s." )" "$where" )"
        fi

        zenity --error --text="$message_unabletofound"
        exit
    fi

    for file in "$@"
    do
        case "$mode" in
            link|ln)
                ln -fs "$file" "$where"
                ;;
            copy|cp)
                cp -f "$file" "$where"
                ;;
            move|mv)
                mv -f "$file" "$where"
                ;;
        esac
    done

}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
