#!/bin/sh
# See LICENSE file for copyright and license details.

# uses SAFE_SECRET, SAFE_MASTER and SAFE_DIR from environment

get_master_key() {
    if [ -z "$SAFE_SECRET" ]; then >&2 echo "SAFE_SECRET is not defined"; exit 1; fi
    if [ -z "$SAFE_MASTER" ]; then >&2 echo "SAFE_MASTER is not defined"; exit 1; fi
    safe -K "$SAFE_SECRET" "$SAFE_MASTER"
}

get_files() {
    KEY=`get_master_key`
    if [ -z "$SAFE_DIR" ]; then >&2 echo "SAFE_DIR is not defined"; exit 1; fi
    find "$SAFE_DIR" -type f -name "*.safe" -maxdepth 1 \
        | while read -r file; do
            name=`basename "$file" | sed "s/\.safe$//" | safe -k "$KEY" -`
            echo "$file,$name"
          done
}

get_file_scripting() {
    if [ -z "$1" ]; then >&2 echo "name is not specified"; exit 1; fi
    get_files | while IFS="," read -r file name; do
        if [ "$1" == "$name" ]; then
            echo "$file"
            return
        fi
    done
}

get_file_interactive() {
    get_files | fzf --delimiter="," --with-nth=2 --accept-nth=1 --height=5 $@
}

get_file() {
    if [ "$SCRIPTING_MODE" -ne "0" ]; then
        file=`get_file_scripting "$1"`
        if [ -z "$file" ]; then >&2 echo "'$1' was not found"; exit 1; fi
        echo "$file"
        return
    fi
    if [ -n "$1" ]; then
        file=`get_file_interactive --filter "$1" | sed q`
        if [ -n "$file" ]; then
            echo "$file"
            return
        fi
    fi
    get_file_interactive
}

SCRIPTING_MODE="0"
if [ "$1" == "-s" ]; then
    SCRIPTING_MODE="1"
    shift
fi

case "$1" in
raw) shift;
    safe -k "`get_master_key`" $@;;
encode) shift;
    safe -k "`get_master_key`" encode $@;;
decode) shift;
    safe -k "`get_master_key`" decode $@;;
html) shift;
    case "$1" in
    pack) shift;
        template="`dirname $0`/../share/safe/template.html"
        if [ "`dirname $0`" == "." ]; then template="template.html"; fi
        sed "/{{ENTRIES}}/,\$d" "$template"
        find "$SAFE_DIR" -type f -name "*.safe" -maxdepth 1 | awk '{
            password=""; getline password < $0;
            username=""; getline username < $0;
            gsub(/.*\//,""); sub(/\.safe$/,"");
            print "<span class=\"safe_entry\">"
            print "<span class=\"safe_id\">" $0 "</span>"
            print "<span class=\"safe_password\">" password "</span>"
            print "<span class=\"safe_username\">" username "</span>"
            print "</span>"
        }'
        sed "1,/{{ENTRIES}}/d" "$template"
        ;;
    unpack) shift;
        mkdir -p "$SAFE_DIR"
        htmlq -f "${1:--}" -t "span.safe_entry > span" | paste -d "," - - - \
            | (cd "$SAFE_DIR" && awk -F , '{
                file=$1 ".safe";
                print $2 > file;
                if ($3 != "") print $3 >> file;
            }')
        ;;
    esac
    ;;

path) shift;
    get_file "$1";;
get) shift;
    FILE=`get_file "$1"`
    shift
    safe -k "`get_master_key`" $@ "$FILE"
    ;;

both) shift;
    KEY=`get_master_key`
    FILE=`get_file "$1"`
    safe -k "$KEY" -1 "$FILE" | xclip
    echo -n "Username copied to clipboard."
    read
    safe -k "$KEY" "$FILE" | xclip
    echo "Password copied to clipboard."
    ;;
*)
    QUERY=`echo "$1" | sed "s/^-.*//"`
    FILE=`get_file "$QUERY"`
    if [ -n "$QUERY" ]; then shift; fi
    safe -k "`get_master_key`" $@ "$FILE" | xclip
    ;;
esac
