library 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/sh
  2. # packaging, library.org
  3. # configuration, /home/swflint/Projects/library/library.org
  4. export LIBRARY_FILE=${LIBRARY_FILE:-~/.library/library.rec}
  5. export LIBRARY_DIRECTORY=${LIBRARY_DIRECTOR:-~/.library}
  6. export LIBRARY_GIT=${LIBRARY_GIT:-detect}
  7. export LIBRARY_DEFAULT_FIELDS=${LIBRARY_DEFAULT_FIELDS:-ID,Title,Author,LCCN,Location,Course}
  8. # configuration ends here
  9. # help-message, /home/swflint/Projects/library/library.org
  10. if [[ $# -eq 0 ]] ; then
  11. echo "library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | withdraw | drop-withdrawn | init ]"
  12. exit
  13. fi
  14. function display_help {
  15. cat <<EOF
  16. library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | init ]
  17. help: Display this help message.
  18. query: Query Library Database.
  19. add: Add a singular book record.
  20. git: Run a git command.
  21. bulk-add: Add a specified number of records.
  22. report: Run a report.
  23. edit: Edit the value of a specified field in a specified record.
  24. edit-matching: Edit records matching a give expression
  25. loan: Loan a book out.
  26. return-book:Process a book return
  27. withdraw: Withdraw a specified book
  28. drop-withdrawn: Drop withdrawn books
  29. init: Initialize the database.
  30. EOF
  31. }
  32. # help-message ends here
  33. # handle-reports, /home/swflint/Projects/library/library.org
  34. function do_report {
  35. if [[ $# -lt 1 ]] ; then
  36. echo "library report name args*"
  37. exit 1
  38. fi
  39. NAME=$1
  40. shift
  41. case "${NAME}" in
  42. list)
  43. for report in ${LIBRARY_DIRECTORY}/reports/*.report ;
  44. do
  45. echo " - $(basename -- "${report}" .report)"
  46. done
  47. ;;
  48. new)
  49. if [[ $# -lt 1 ]] ; then
  50. echo "library report new name"
  51. exit 1
  52. fi
  53. REPORT=$1
  54. shift
  55. echo "# -*- mode: shell-script -*-" > "${LIBRARY_DIRECTORY}/reports/${REPORT}.report"
  56. emacsclient --alternate-editor="" -n "${LIBRARY_DIRECTORY}/reports/${REPORT}.report"
  57. ;;
  58. *)
  59. if [[ -e "${LIBRARY_DIRECTORY}/reports/${NAME}.report" ]] ; then
  60. sh "${LIBRARY_DIRECTORY}/reports/${NAME}.report" "$@"
  61. fi
  62. esac
  63. }
  64. # handle-reports ends here
  65. # handle-git, /home/swflint/Projects/library/library.org
  66. function do_git {
  67. if [[ $# -lt 1 ]] ; then
  68. echo "library git args*"
  69. exit 1
  70. fi
  71. FIRST=$1
  72. if [[ $FIRST == "init" ]] ; then
  73. OLD=`pwd`
  74. cd "${LIBRARY_DIRECTORY}"
  75. git "$@"
  76. cd ${OLD}
  77. else
  78. if [[ $LIBRARY_GIT == "detect" ]] ; then
  79. OLD=`pwd`
  80. cd "${LIBRARY_DIRECTORY}"
  81. git "$@"
  82. cd ${OLD}
  83. fi
  84. fi
  85. }
  86. # handle-git ends here
  87. # handle-query, /home/swflint/Projects/library/library.org
  88. function run_query {
  89. recsel -t Book "$@" "${LIBRARY_FILE}"
  90. }
  91. # handle-query ends here
  92. # add-book, /home/swflint/Projects/library/library.org
  93. function add_single {
  94. if [[ $# -lt 7 ]] ; then
  95. echo -n "Title: "
  96. read TITLE
  97. echo -n "Author: "
  98. read AUTHOR
  99. echo -n "LCCN: "
  100. read LCCN
  101. echo -n "Copyright: "
  102. read COPYRIGHT
  103. echo -n "Publisher: "
  104. read PUBLISHER
  105. echo -n "ISBN: "
  106. read ISBN
  107. echo -n "Location: "
  108. read LOCATION
  109. else
  110. TITLE=$1
  111. shift
  112. AUTHOR=$1
  113. shift
  114. LCCN=$1
  115. shift
  116. COPYRIGHT=$1
  117. shift
  118. PUBLISHER=$1
  119. shift
  120. ISBN=$1
  121. shift
  122. LOCATION=$1
  123. shift
  124. fi
  125. TMPDIR=. recins -t Book \
  126. -f Title -v "${TITLE}" \
  127. -f Author -v "${AUTHOR}" \
  128. -f LCCN -v "${LCCN}" \
  129. -f Copyright -v "${COPYRIGHT}" \
  130. -f Publisher -v "${PUBLISHER}" \
  131. -f ISBN -v "${ISBN}" \
  132. -f Location -v "${LOCATION}" \
  133. -f Card -v UNPRINTED \
  134. "${LIBRARY_FILE}"
  135. do_git add `basename "${LIBRARY_FILE}"`
  136. do_git commit -m "Added record for \"${TITLE}\""
  137. }
  138. # add-book ends here
  139. # add-in-bulk, /home/swflint/Projects/library/library.org
  140. function bulk_add {
  141. if [[ $@ -lt 1 ]] ; then
  142. echo "library bulk-add number"
  143. exit 1
  144. fi
  145. GITOLD="${LIBRARY_GIT}"
  146. LIBRARY_GIT=FALSE
  147. for i in 1 .. $1 ; do
  148. echo "Adding book number ${i}"
  149. add_single
  150. done
  151. LIBRARY_GIT="${GITOLD}"
  152. do_git add `basename "${LIBRARY_FILE}"`
  153. do_git commit -m "Added ${1} records"
  154. }
  155. # add-in-bulk ends here
  156. # edit-field, /home/swflint/Projects/library/library.org
  157. function do_edit {
  158. if [[ $# -lt 2 ]] ; then
  159. echo "library edit id field [ value ]"
  160. exit 1
  161. fi
  162. ID=$1
  163. shift
  164. FIELD=$1
  165. shift
  166. VALUE=$1
  167. shift
  168. TMPDIR=. recset -e "ID = ${ID}" \
  169. -f "${FIELD}" -S "${VALUE}" \
  170. "${LIBRARY_FILE}"
  171. do_git add `basename "${LIBRARY_FILE}"`
  172. do_git commit -m "Edited record id ${ID}"
  173. }
  174. function do_edit_exp {
  175. if [[ $# -lt 3 ]] ; then
  176. echo "library edit-matching match-exp field value [ commit-message ]"
  177. exit 1
  178. fi
  179. MATCHEXPRESSION=$1
  180. shift
  181. FIELDNAME=$1
  182. shift
  183. VALUE=$1
  184. shift
  185. TMPDIR=. recset -e "${MATCHEXPRESSION}" \
  186. -f "${FIELDNAME}" -S "${VALUE}" \
  187. "${LIBRARY_FILE}"
  188. do_git add $(basename "${LIBRARY_FILE}")
  189. if [[ $1 != "" ]] ; then
  190. do_git commit -m "${1}"
  191. else
  192. do_git commit -m "Bulk edited records"
  193. fi
  194. }
  195. # edit-field ends here
  196. # withdraw-books, /home/swflint/Projects/library/library.org
  197. function do_withdraw {
  198. if [ $# -lt 1 ] ; then
  199. echo "library withdraw id"
  200. exit 1
  201. fi
  202. ID=$1
  203. TMPDIR=. recset -e "ID = ${ID}" \
  204. -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  205. -f "Location" -S "WITHDRAWN" \
  206. "${LIBRARY_FILE}"
  207. do_git add $(basename ${LIBRARY_FILE})
  208. do_git commit -m "Withdrew book ${ID}"
  209. }
  210. function remove_withdrawn {
  211. TMPDIR=. recdel -t Book \
  212. -e "Location ~ \"WITHDRAWN\"" \
  213. "${LIBRARY_FILE}"
  214. }
  215. # withdraw-books ends here
  216. # initialize-database, /home/swflint/Projects/library/library.org
  217. function initialize {
  218. OLD=`pwd`
  219. mkdir -p "${LIBRARY_DIRECTORY}"
  220. cd "${LIBRARY_DIRECTORY}"
  221. [[ ! -e `basename "${LIBRARY_FILE}"` ]] && \
  222. cat <<EOF > `basename "${LIBRARY_FILE}"`
  223. # file-format, /home/swflint/Projects/library/library.org
  224. # -*- mode: rec -*-
  225. %rec: Book
  226. %doc: Foo
  227. %key: ID
  228. %unique: Title
  229. %type: ID int
  230. %type: Title line
  231. %type: Author line
  232. %type: LCCN line
  233. %type: ISBN regexp /[0-9]*X?/
  234. %type: Publisher line
  235. %type: Copyright int
  236. %type: Location line
  237. %type: Withdrawn date
  238. %type: Inserted date
  239. %type: Course line
  240. %type: LoanTo line
  241. %type: LoanOn date
  242. %typedef: CardPrint enum PRINTED REPRINT UNPRINTED
  243. %type: Card CardPrint
  244. %mandatory: Title Author LCCN Inserted
  245. %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn Course Card
  246. %auto: ID Inserted
  247. # file-format ends here
  248. EOF
  249. }
  250. # initialize-database ends here
  251. # loan, /home/swflint/Projects/library/library.org
  252. function do_loan {
  253. if [[ $# -lt 2 ]] ; then
  254. echo "library loan id name"
  255. exit 1
  256. fi
  257. ID=$1
  258. shift
  259. NAME=$1
  260. shift
  261. TMPDIR=. recset -e "ID = ${ID}" \
  262. -f "LoanTo" -S "${NAME}" \
  263. "${LIBRARY_FILE}"
  264. TMPDIR=. recset -e "ID = ${ID}" \
  265. -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  266. "${LIBRARY_FILE}"
  267. do_git add `basename "${LIBRARY_FILE}"`
  268. do_git commit -m "Loaned Book ${ID} to ${NAME}"
  269. }
  270. function do_return {
  271. if [[ $# -lt 1 ]] ; then
  272. echo "library return-book id"
  273. exit 1
  274. fi
  275. ID=$1
  276. shift
  277. TMPDIR=. recset -e "ID = ${ID}" \
  278. -f "LoanTo" -d \
  279. "${LIBRARY_FILE}"
  280. TMPDIR=. recset -e "ID = ${ID}" \
  281. -f "LoanOn" -d \
  282. "${LIBRARY_FILE}"
  283. do_git add `basename "${LIBRARY_FILE}"`
  284. do_git commit -m "Returned Book ${ID}"
  285. }
  286. # loan ends here
  287. # process-commands, /home/swflint/Projects/library/library.org
  288. COMMAND=$1
  289. shift
  290. case "${COMMAND}" in
  291. help)
  292. display_help
  293. exit
  294. ;;
  295. query)
  296. run_query "$@"
  297. exit
  298. ;;
  299. add)
  300. add_single "$@"
  301. exit
  302. ;;
  303. git)
  304. do_git "$@"
  305. exit
  306. ;;
  307. bulk-add)
  308. bulk_add "$@"
  309. exit
  310. ;;
  311. report)
  312. do_report "$@"
  313. exit
  314. ;;
  315. edit)
  316. do_edit "$@"
  317. exit
  318. ;;
  319. edit-matching)
  320. do_edit_exp "$@"
  321. exit
  322. ;;
  323. loan)
  324. do_loan "$@"
  325. exit
  326. ;;
  327. return-book)
  328. do_return "$@"
  329. exit
  330. ;;
  331. withdraw)
  332. do_withdraw "$@"
  333. exit
  334. ;;
  335. drop-withdrawn)
  336. remove_withdrawn "$@"
  337. exit
  338. ;;
  339. init)
  340. initialize
  341. exit
  342. ;;
  343. *)
  344. display_help
  345. exit
  346. esac
  347. # process-commands ends here
  348. # packaging ends here