library 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. do_git add $(basename ${LIBRARY_FILE})
  215. do_git commit -m "Remove Withdrawn Books"
  216. }
  217. # withdraw-books ends here
  218. # initialize-database, /home/swflint/Projects/library/library.org
  219. function initialize {
  220. OLD=`pwd`
  221. mkdir -p "${LIBRARY_DIRECTORY}"
  222. cd "${LIBRARY_DIRECTORY}"
  223. [[ ! -e `basename "${LIBRARY_FILE}"` ]] && \
  224. cat <<EOF > `basename "${LIBRARY_FILE}"`
  225. # file-format, /home/swflint/Projects/library/library.org
  226. # -*- mode: rec -*-
  227. %rec: Book
  228. %doc: Foo
  229. %key: ID
  230. %unique: Title
  231. %type: ID int
  232. %type: Title line
  233. %type: Author line
  234. %type: LCCN line
  235. %type: ISBN regexp /[0-9]*X?/
  236. %type: Publisher line
  237. %type: Copyright int
  238. %type: Location line
  239. %type: Withdrawn date
  240. %type: Inserted date
  241. %type: Course line
  242. %type: LoanTo line
  243. %type: LoanOn date
  244. %typedef: CardPrint enum PRINTED REPRINT UNPRINTED
  245. %type: Card CardPrint
  246. %mandatory: Title Author LCCN Inserted
  247. %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn Course Card
  248. %auto: ID Inserted
  249. # file-format ends here
  250. EOF
  251. }
  252. # initialize-database ends here
  253. # loan, /home/swflint/Projects/library/library.org
  254. function do_loan {
  255. if [[ $# -lt 2 ]] ; then
  256. echo "library loan id name"
  257. exit 1
  258. fi
  259. ID=$1
  260. shift
  261. NAME=$1
  262. shift
  263. TMPDIR=. recset -e "ID = ${ID}" \
  264. -f "LoanTo" -S "${NAME}" \
  265. "${LIBRARY_FILE}"
  266. TMPDIR=. recset -e "ID = ${ID}" \
  267. -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  268. "${LIBRARY_FILE}"
  269. do_git add `basename "${LIBRARY_FILE}"`
  270. do_git commit -m "Loaned Book ${ID} to ${NAME}"
  271. }
  272. function do_return {
  273. if [[ $# -lt 1 ]] ; then
  274. echo "library return-book id"
  275. exit 1
  276. fi
  277. ID=$1
  278. shift
  279. TMPDIR=. recset -e "ID = ${ID}" \
  280. -f "LoanTo" -d \
  281. "${LIBRARY_FILE}"
  282. TMPDIR=. recset -e "ID = ${ID}" \
  283. -f "LoanOn" -d \
  284. "${LIBRARY_FILE}"
  285. do_git add `basename "${LIBRARY_FILE}"`
  286. do_git commit -m "Returned Book ${ID}"
  287. }
  288. # loan ends here
  289. # process-commands, /home/swflint/Projects/library/library.org
  290. COMMAND=$1
  291. shift
  292. case "${COMMAND}" in
  293. help)
  294. display_help
  295. exit
  296. ;;
  297. query)
  298. run_query "$@"
  299. exit
  300. ;;
  301. add)
  302. add_single "$@"
  303. exit
  304. ;;
  305. git)
  306. do_git "$@"
  307. exit
  308. ;;
  309. bulk-add)
  310. bulk_add "$@"
  311. exit
  312. ;;
  313. report)
  314. do_report "$@"
  315. exit
  316. ;;
  317. edit)
  318. do_edit "$@"
  319. exit
  320. ;;
  321. edit-matching)
  322. do_edit_exp "$@"
  323. exit
  324. ;;
  325. loan)
  326. do_loan "$@"
  327. exit
  328. ;;
  329. return-book)
  330. do_return "$@"
  331. exit
  332. ;;
  333. withdraw)
  334. do_withdraw "$@"
  335. exit
  336. ;;
  337. drop-withdrawn)
  338. remove_withdrawn "$@"
  339. exit
  340. ;;
  341. init)
  342. initialize
  343. exit
  344. ;;
  345. *)
  346. display_help
  347. exit
  348. esac
  349. # process-commands ends here
  350. # packaging ends here