123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #!/bin/sh
- # packaging, library.org
- LIBRARYFILE=~/.library/library.rec
- LIBRARYDIRECTORY=~/.library
- GIT=detect
- # help-message, /home/swflint/Projects/library/library.org
- if [[ $# -eq 0 ]] ; then
- echo "library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | init ]"
- exit
- fi
- function display_help {
- cat <<EOF
- library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | init ]
- help: Display this help message.
- query: Query Library Database.
- add: Add a singular book record.
- git: Run a git command.
- bulk-add: Add a specified number of records.
- report: Run a report.
- edit: Edit the value of a specified field in a specified record.
- edit-matching: Edit records matching a give expression
- loan: Loan a book out.
- return-book:Process a book return
- init: Initialize the database.
- EOF
- }
- # help-message ends here
- # handle-reports, /home/swflint/Projects/library/library.org
- function do_report {
- if [[ $# -lt 1 ]] ; then
- echo "library report name args*"
- exit 1
- fi
- NAME=$1
- shift
- case ${NAME} in
- list)
- for report in ${LIBRARYDIRECTORY}/*.report ;
- do
- echo " - $(basename -- ${report} .report)"
- done
- ;;
- new)
- if [[ $# -lt 1 ]] ; then
- echo "library report new name"
- exit 1
- fi
- REPORT=$1
- shift
- echo "# -*- mode: shell-script -*-" > ${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/Projects/library/library.org
- function do_git {
- if [[ $# -lt 1 ]] ; then
- echo "library git args*"
- exit 1
- fi
- 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/Projects/library/library.org
- function run_query {
- recsel -t Book $@ ${LIBRARYFILE}
- }
- # handle-query ends here
- # add-book, /home/swflint/Projects/library/library.org
- function add_single {
- if [[ $# -lt 7 ]] ; then
- 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
- else
- TITLE=$1
- shift
- AUTHOR=$1
- shift
- LCCN=$1
- shift
- COPYRIGHT=$1
- shift
- PUBLISHER=$1
- shift
- ISBN=$1
- shift
- LOCATION=$1
- shift
- fi
- 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/Projects/library/library.org
- function bulk_add {
- if [[ $@ -lt 1 ]] ; then
- echo "library bulk-add number"
- exit 1
- fi
- 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/Projects/library/library.org
- function do_edit {
- if [[ $# -lt 2 ]] ; then
- echo "ledger edit id field [ value ]"
- exit 1
- fi
- ID=$1
- shift
- FIELD=$1
- shift
- if [[ $FIELD = "Withdrawn" ]] ; then
- recset -e "ID = ${ID}" \
- -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
- ${LIBRARYFILE}
- else
- VALUE=$1
- shift
- recset -e "ID = ${ID}" \
- -f "${FIELD}" -s "${VALUE}" \
- ${LIBRARYFILE}
- fi
- do_git add `basename ${LIBRARYFILE}`
- do_git commit -m "Edited record id ${ID}"
- }
- function do_edit_exp {
- if [[ $# -lt 3 ]] ; then
- echo "ledger edit-matching match-exp field value [ commit-message ]"
- exit 1
- fi
- MATCHEXPRESSION=$1
- shift
- FIELDNAME=$1
- shift
- VALUE=$1
- shift
- recset -e "${MATCHEXPRESSION}" \
- -f "${FIELDNAME}" -S "${VALUE}" \
- ${LIBRARYFILE}
- do_git add $(basename ${LIBRARYFILE})
- if [[ $1 != "" ]] ; then
- do_git commit -m "${1}"
- else
- do_git commit -m "Bulk edited records"
- fi
- }
- # edit-field ends here
- # initialize-database, /home/swflint/Projects/library/library.org
- function initialize {
- OLD=`pwd`
- mkdir -p ${LIBRARYDIRECTORY}
- cd ${LIBRARYDIRECTORY}
- [[ ! -e `basename ${LIBRARYFILE}` ]] && \
- cat <<EOF > `basename ${LIBRARYFILE}`
- # file-format, /home/swflint/Projects/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: Withdrawn date
- %type: Inserted date
- %type: LoanTo line
- %type: LoanOn date
- %mandatory: Title Author LCCN Inserted
- %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn
- %auto: ID Inserted
- # file-format ends here
- EOF
- }
- # initialize-database ends here
- # loan, /home/swflint/Projects/library/library.org
- function do_loan {
- if [[ $# -lt 2 ]] ; then
- echo "library loan id name"
- exit 1
- fi
- ID=$1
- shift
- NAME=$1
- shift
- recset -e "ID = ${ID}" \
- -f "LoanTo" -S "${NAME}" \
- ${LIBRARYFILE}
- recset -e "ID = ${ID}" \
- -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
- ${LIBRARYFILE}
- do_git add `basename ${LIBRARYFILE}`
- do_git commit -m "Loaned Book ${ID} to ${NAME}"
- }
- function do_return {
- if [[ $# -lt 1 ]] ; then
- echo "library return-book id"
- exit 1
- fi
- ID=$1
- shift
- recset -e "ID = ${ID}" \
- -f "LoanTo" -d \
- ${LIBRARYFILE}
- recset -e "ID = ${ID}" \
- -f "LoanOn" -d \
- ${LIBRARYFILE}
- do_git add `basename ${LIBRARYFILE}`
- do_git commit -m "Returned Book ${ID}"
- }
- # loan ends here
- # process-commands, /home/swflint/Projects/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
- ;;
- edit)
- do_edit "$@"
- exit
- ;;
- edit-matching)
- do_edit_exp "$@"
- exit
- ;;
- loan)
- do_loan "$@"
- exit
- ;;
- return-book)
- do_return "$@"
- exit
- ;;
- init)
- initialize
- exit
- ;;
- *)
- display_help
- exit
- esac
- # process-commands ends here
- # packaging ends here
|