#!/bin/zsh -f # packaging, library.org LIBRARYFILE=~/.library/library.rec LIBRARYDIRECTORY=~/.library GIT=detect # help-message, /home/swflint/.library/library.org if [[ $# -eq 0 ]] ; then echo "library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field [ value ] | init ]" exit fi function display-help { cat < ${LIBRARYDIRECTORY}/${REPORT}.report emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report ;; *) if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then sh ${LIBRARYDIRECTORY}/${NAME}.report $@ fi esac } # handle-reports ends here # handle-git, /home/swflint/.library/library.org function do-git { FIRST=$1 if [[ $FIRST == "init" ]] ; then OLD=`pwd` cd ${LIBRARYDIRECTORY} git $@ cd ${OLD} else if [[ $GIT == "detect" ]] ; then OLD=`pwd` cd ${LIBRARYDIRECTORY} git $@ cd ${OLD} fi fi } # handle-git ends here # handle-query, /home/swflint/.library/library.org function run-query { recsel -t Book $@ ${LIBRARYFILE} } # handle-query ends here # add-book, /home/swflint/.library/library.org function add-single { echo -n "Title: " read TITLE echo -n "Author: " read AUTHOR echo -n "LCCN: " read LCCN echo -n "Copyright: " read COPYRIGHT echo -n "Publisher: " read PUBLISHER echo -n "ISBN: " read ISBN echo -n "Location: " read LOCATION recins -t Book \ -f Title -v "${TITLE}" \ -f Author -v "${AUTHOR}" \ -f LCCN -v "${LCCN}" \ -f Copyright -v "${COPYRIGHT}" \ -f Publisher -v "${PUBLISHER}" \ -f ISBN -v "${ISBN}" \ -f Location -v "${LOCATION}" \ ${LIBRARYFILE} do-git add `basename ${LIBRARYFILE}` do-git commit -m "Added record for \"${TITLE}\"" } # add-book ends here # add-in-bulk, /home/swflint/.library/library.org function bulk-add { GITOLD=${GIT} GIT=FALSE for i in {1..$1} ; do echo "Adding book number ${i}" add-single done GIT=${GITOLD} do-git add `basename ${LIBRARYFILE}` do-git commit -m "Added ${1} records" } # add-in-bulk ends here # edit-field, /home/swflint/.library/library.org function do-edit { ID=$1 shift FIELD=$1 shift if [[ $FIELD = "Withdrawn" ]] ; then recins -e "ID = ${ID}" \ -f "Withdrawn" -v `date "%a, %d %b %Y %H:%M:%S %z"` \ ${LIBRARYFILE} else VALUE=$1 shift recins -e "ID = ${ID}" \ -f "${FIELD}" -v "${VALUE}" \ ${LIBRARYFILE} fi do-git add `basename ${LIBRARYFILE}` do-git commit -m "Edited record id ${ID}" } # edit-field ends here # initialize-database, /home/swflint/.library/library.org function initialize { OLD=`pwd` mkdir -p ${LIBRARYDIRECTORY} cd ${LIBRARYDIRECTORY} [[ -ne `basename ${LIBRARYFILE}` ]] && \ cat < `basename ${LIBRARYFILE}` # file-format, /home/swflint/.library/library.org # -*- mode: rec -*- %rec: Book %doc: Foo %key: ID %unique: Title %type: ID int %type: Title line %type: Author line %type: LCCN line %type: ISBN regexp /[0-9]*X?/ %type: Publisher line %type: Copyright int %type: Location line %type: Inserted date %mandatory: Title Author LCCN Inserted %allowed: ISBN Publisher Copyright Location %auto: ID Inserted # file-format ends here EOF } # initialize-database ends here # view-in-emacs, /home/swflint/.library/library.org function view-in-emacs { emacsclient --alternate-editor="" -n ${LIBRARYFILE} } # view-in-emacs ends here # process-commands, /home/swflint/.library/library.org COMMAND=$1 shift case ${COMMAND} in help) display-help exit ;; query) run-query $@ exit ;; add) add-single exit ;; git) do-git $@ exit ;; bulk-add) bulk-add $@ exit ;; report) do-report $@ exit ;; emacs) view-in-emacs exit ;; edit) do-edit $@ exit ;; init) initialize exit ;; *) display-help exit esac # process-commands ends here # packaging ends here