library 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/bin/zsh -f
  2. # packaging, library.org
  3. LIBRARYFILE=~/.library/library.rec
  4. LIBRARYDIRECTORY=~/.library
  5. GIT=detect
  6. # help-message, /home/swflint/org/library.org
  7. if [[ $# -eq 0 ]] ; then
  8. echo "library [ help | query | add | emacs | git | bulk-add | report | edit | loan | do-return | init ]"
  9. exit
  10. fi
  11. function display-help {
  12. cat <<EOF
  13. library [ help | query | add | emacs | git | bulk-add | report | edit | loan | do-return | init ]
  14. help: Display this help message.
  15. query: Query Library Database.
  16. add: Add a singular book record.
  17. emacs: View records in emacs.
  18. git: Run a git command.
  19. bulk-add: Add a specified number of records.
  20. report: Run a report.
  21. edit: Edit the value of a specified field in a specified record.
  22. loan: Loan a book out.
  23. do-return: Process a book return
  24. init: Initialize the database.
  25. EOF
  26. }
  27. # help-message ends here
  28. # handle-reports, /home/swflint/org/library.org
  29. function do-report {
  30. if [[ $# -lt 1 ]] ; then
  31. echo "library report name args*"
  32. exit 1
  33. fi
  34. NAME=$1
  35. shift
  36. case ${NAME} in
  37. list)
  38. for report in ${LIBRARYDIRECTORY}/*.report ;
  39. do
  40. echo " - $(basename -- ${report} .report)"
  41. done
  42. ;;
  43. new)
  44. if [[ $# -lt 1 ]] ; then
  45. echo "library report new name"
  46. exit 1
  47. fi
  48. REPORT=$1
  49. shift
  50. echo "# -*- mode: shell-script -*-" > ${LIBRARYDIRECTORY}/${REPORT}.report
  51. emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report
  52. ;;
  53. *)
  54. if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then
  55. sh ${LIBRARYDIRECTORY}/${NAME}.report $@
  56. fi
  57. esac
  58. }
  59. # handle-reports ends here
  60. # handle-git, /home/swflint/org/library.org
  61. function do-git {
  62. if [[ $# -lt 1 ]] ; then
  63. echo "library git args*"
  64. exit 1
  65. fi
  66. FIRST=$1
  67. if [[ $FIRST == "init" ]] ; then
  68. OLD=`pwd`
  69. cd ${LIBRARYDIRECTORY}
  70. git $@
  71. cd ${OLD}
  72. else
  73. if [[ $GIT == "detect" ]] ; then
  74. OLD=`pwd`
  75. cd ${LIBRARYDIRECTORY}
  76. git $@
  77. cd ${OLD}
  78. fi
  79. fi
  80. }
  81. # handle-git ends here
  82. # handle-query, /home/swflint/org/library.org
  83. function run-query {
  84. recsel -t Book $@ ${LIBRARYFILE}
  85. }
  86. # handle-query ends here
  87. # add-book, /home/swflint/org/library.org
  88. function add-single {
  89. echo -n "Title: "
  90. read TITLE
  91. echo -n "Author: "
  92. read AUTHOR
  93. echo -n "LCCN: "
  94. read LCCN
  95. echo -n "Copyright: "
  96. read COPYRIGHT
  97. echo -n "Publisher: "
  98. read PUBLISHER
  99. echo -n "ISBN: "
  100. read ISBN
  101. echo -n "Location: "
  102. read LOCATION
  103. recins -t Book \
  104. -f Title -v "${TITLE}" \
  105. -f Author -v "${AUTHOR}" \
  106. -f LCCN -v "${LCCN}" \
  107. -f Copyright -v "${COPYRIGHT}" \
  108. -f Publisher -v "${PUBLISHER}" \
  109. -f ISBN -v "${ISBN}" \
  110. -f Location -v "${LOCATION}" \
  111. ${LIBRARYFILE}
  112. do-git add `basename ${LIBRARYFILE}`
  113. do-git commit -m "Added record for \"${TITLE}\""
  114. }
  115. # add-book ends here
  116. # add-in-bulk, /home/swflint/org/library.org
  117. function bulk-add {
  118. if [[ $@ -lt 1 ]] ; then
  119. echo "library bulk-add number"
  120. exit 1
  121. fi
  122. GITOLD=${GIT}
  123. GIT=FALSE
  124. for i in {1..$1} ; do
  125. echo "Adding book number ${i}"
  126. add-single
  127. done
  128. GIT=${GITOLD}
  129. do-git add `basename ${LIBRARYFILE}`
  130. do-git commit -m "Added ${1} records"
  131. }
  132. # add-in-bulk ends here
  133. # edit-field, /home/swflint/org/library.org
  134. function do-edit {
  135. if [[ $# -lt 2 ]] ; then
  136. echo "ledger edit id field [ value ]"
  137. exit 1
  138. fi
  139. ID=$1
  140. shift
  141. FIELD=$1
  142. shift
  143. if [[ $FIELD = "Withdrawn" ]] ; then
  144. recset -e "ID = ${ID}" \
  145. -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  146. ${LIBRARYFILE}
  147. else
  148. VALUE=$1
  149. shift
  150. recset -e "ID = ${ID}" \
  151. -f "${FIELD}" -s "${VALUE}" \
  152. ${LIBRARYFILE}
  153. fi
  154. do-git add `basename ${LIBRARYFILE}`
  155. do-git commit -m "Edited record id ${ID}"
  156. }
  157. # edit-field ends here
  158. # initialize-database, /home/swflint/org/library.org
  159. function initialize {
  160. OLD=`pwd`
  161. mkdir -p ${LIBRARYDIRECTORY}
  162. cd ${LIBRARYDIRECTORY}
  163. [[ -ne `basename ${LIBRARYFILE}` ]] && \
  164. cat <<EOF > `basename ${LIBRARYFILE}`
  165. # file-format, /home/swflint/org/library.org
  166. # -*- mode: rec -*-
  167. %rec: Book
  168. %doc: Foo
  169. %key: ID
  170. %unique: Title
  171. %type: ID int
  172. %type: Title line
  173. %type: Author line
  174. %type: LCCN line
  175. %type: ISBN regexp /[0-9]*X?/
  176. %type: Publisher line
  177. %type: Copyright int
  178. %type: Location line
  179. %type: Withdrawn date
  180. %type: Inserted date
  181. %type: LoanTo line
  182. %type: LoanOn date
  183. %mandatory: Title Author LCCN Inserted
  184. %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn
  185. %auto: ID Inserted
  186. # file-format ends here
  187. EOF
  188. }
  189. # initialize-database ends here
  190. # view-in-emacs, /home/swflint/org/library.org
  191. function view-in-emacs {
  192. emacsclient --alternate-editor="" -n ${LIBRARYFILE}
  193. }
  194. # view-in-emacs ends here
  195. # loan, /home/swflint/org/library.org
  196. function do-loan {
  197. if [[ $# -lt 2 ]] ; then
  198. echo "library loan id name"
  199. exit 1
  200. fi
  201. ID=$1
  202. shift
  203. NAME=$1
  204. shift
  205. recset -e "ID = ${ID}" \
  206. -f "LoanTo" -S "${NAME}" \
  207. ${LIBRARYFILE}
  208. recset -e "ID = ${ID}" \
  209. -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  210. ${LIBRARYFILE}
  211. do-git add `basename ${LIBRARYFILE}`
  212. do-git commit -m "Loaned Book ${ID} to ${NAME}"
  213. }
  214. function do-return {
  215. if [[ $# -lt 1 ]] ; then
  216. echo "library return-book id"
  217. exit 1
  218. fi
  219. ID=$1
  220. shift
  221. recset -e "ID = ${ID}" \
  222. -f "LoanTo" -d \
  223. ${LIBRARYFILE}
  224. recset -e "ID = ${ID}" \
  225. -f "LoanOn" -d \
  226. ${LIBRARYFILE}
  227. do-git add `basename ${LIBRARYFILE}`
  228. do-git commit -m "Returned Book ${ID}"
  229. }
  230. # loan ends here
  231. # process-commands, /home/swflint/org/library.org
  232. COMMAND=$1
  233. shift
  234. case ${COMMAND} in
  235. help)
  236. display-help
  237. exit
  238. ;;
  239. query)
  240. run-query $@
  241. exit
  242. ;;
  243. add)
  244. add-single
  245. exit
  246. ;;
  247. git)
  248. do-git $@
  249. exit
  250. ;;
  251. bulk-add)
  252. bulk-add $@
  253. exit
  254. ;;
  255. report)
  256. do-report $@
  257. exit
  258. ;;
  259. emacs)
  260. view-in-emacs
  261. exit
  262. ;;
  263. edit)
  264. do-edit $@
  265. exit
  266. ;;
  267. loan)
  268. do-loan $@
  269. exit
  270. ;;
  271. return-book)
  272. do-return $@
  273. exit
  274. ;;
  275. init)
  276. initialize
  277. exit
  278. ;;
  279. *)
  280. display-help
  281. exit
  282. esac
  283. # process-commands ends here
  284. # packaging ends here