#!/bin/sh
export JOURNAL_FILE=${LIBRARY_FILE:-~/.library/journals.rec}
export LIBRARY_DIRECTORY=${LIBRARY_DIRECTOR:-~/.library}
export LIBRARY_GIT=${LIBRARY_GIT:-detect}
export LIBRARY_DEFAULT_FIELDS=${LIBRARY_DEFAULT_FIELDS:-ID,Title,Author,LCCN,Location,Course}
PROGNAME=$(basename $0)

if [[ $# -eq 0 ]] ; then
    echo "$PROGNAME [ help | add_journal | add_issue | many_issues | git ]"
    exit
fi

function display_help {
    cat <<EOF
$PROGNAME [help | add_journal | add_issue | many_issues | git ]

help: show this help
add_journal: add entry for a journal
add_issue: add entry for an issue
many_issues: add many issues
git: perform git command
EOF
}

function do_git {
    if [[ $# -lt 1 ]] ; then
        echo "$PROGNAME git args*"
        exit 1
    fi
    FIRST=$1
    if [[ $FIRST == "init" ]] ; then
        OLD=`pwd`
        cd "${LIBRARY_DIRECTORY}"
        git "$@"
        cd ${OLD}
    else
        if [[ $LIBRARY_GIT == "detect" ]] ; then
            OLD=`pwd`
            cd "${LIBRARY_DIRECTORY}"
            git "$@"
            cd ${OLD}
        fi
    fi
}

function add_journal {
    if [[ $# -lt 2 ]] ; then
        echo -n "Title: "
        read TITLE
        echo -n "ISSN: "
        read ISSN
        echo -n "Publisher: "
        read PUBLISHER
    else
        TITLE=$1
        shift
        ISSN=$1
        shift
        PUBLISHER=$1
        shift
    fi
    TMPDIR=. recins -t Journal \
          -f Title -v "${TITLE}" \
          -f ISSN -v "${ISSN}" \
          -f Publisher -v "${PUBLISHER}" \
          "${JOURNAL_FILE}"
    do_git add `basename "${JOURNAL_FILE}"`
    do_git commit -m "Added entry for journal ${TITLE}."
}

function add_issue {
    if [[ $# -lt 3 ]] ; then
        echo -n "Journal: "
        read JOURNAL
        echo -n "Volume: "
        read VOLUME
        echo -n "Issue: "
        read ISSUE
        echo -n "Location: "
        read LOCATION
    else
        JOURNAL=$1
        shift
        VOLUME=$1
        shift
        ISSUE=$1
        shift
        LOCATION=$1
        shift
    fi
    TMPDIR=. recins -t Issue \
          -f Journal -v "${JOURNAL}" \
          -f Volume -v "${VOLUME}" \
          -f Issue -v "${ISSUE}" \
          -f Location -v "${Location}" \
          "${JOURNAL_FILE}"
    do_git add `basename "${JOURNAL_FILE}"`
    do_git commit -m "Added entry for Journal Issue"
}

function many_issues {
    if [[ $@ -lt 1 ]] ; then
        echo "$PROGNAME many_issues number"
        exit 1
    fi
    echo $1
    GITOLD="${LIBRARY_GIT}"
    LIBRARY_GIT=FALSE
    for i in {1..$1} ; do
        add_issue
    done
    LIBRARY_GIT="${GITOLD}"
    do_git add `basename "${JOURNAL_FILE}"`
    do_git commit -m "Added ${1} Issues"
}


COMMAND=$1
shift

case "${COMMAND}" in
    help)
        display_help
        exit
        ;;
    add_journal)
        add_journal "$@"
        exit
        ;;
    add_issue)
        add_issue "$@"
        exit
        ;;
    git)
        do_git "$@"
        exit
        ;;
    many_issues)
        many_issues "$@"
        exit
        ;;
    *)
        display_help
        exit
esac

journal
