library 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #!/usr/bin/env bash
  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. read -e -p "Title: " TITLE
  96. read -e -p "Author: " AUTHOR
  97. read -e -p "LCCN: " LCCN
  98. read -e -p "Copyright Year: " COPYRIGHT
  99. read -e -p "Publisher: " PUBLISHER
  100. read -e -p "ISBN: " ISBN
  101. read -e -p "Location: " LOCATION
  102. else
  103. TITLE=$1
  104. shift
  105. AUTHOR=$1
  106. shift
  107. LCCN=$1
  108. shift
  109. COPYRIGHT=$1
  110. shift
  111. PUBLISHER=$1
  112. shift
  113. ISBN=$1
  114. shift
  115. LOCATION=$1
  116. shift
  117. fi
  118. TMPDIR=. recins -t Book \
  119. -f Title -v "${TITLE}" \
  120. -f Author -v "${AUTHOR}" \
  121. -f LCCN -v "${LCCN}" \
  122. -f Copyright -v "${COPYRIGHT}" \
  123. -f Publisher -v "${PUBLISHER}" \
  124. -f ISBN -v "${ISBN}" \
  125. -f Location -v "${LOCATION}" \
  126. -f Card -v UNPRINTED \
  127. "${LIBRARY_FILE}"
  128. do_git add `basename "${LIBRARY_FILE}"`
  129. do_git commit -m "Added record for \"${TITLE}\""
  130. library query -i --uniq -e "Title ~ \"${TITLE}\"" | less
  131. }
  132. # add-book ends here
  133. # add-in-bulk, /home/swflint/Projects/library/library.org
  134. function bulk_add {
  135. if [[ $@ -lt 1 ]] ; then
  136. echo "library bulk-add number"
  137. exit 1
  138. fi
  139. GITOLD="${LIBRARY_GIT}"
  140. LIBRARY_GIT=FALSE
  141. for i in 1 .. $1 ; do
  142. echo "Adding book number ${i}"
  143. add_single
  144. done
  145. LIBRARY_GIT="${GITOLD}"
  146. do_git add `basename "${LIBRARY_FILE}"`
  147. do_git commit -m "Added ${1} records"
  148. }
  149. # add-in-bulk ends here
  150. # edit-field, /home/swflint/Projects/library/library.org
  151. function do_edit {
  152. if [[ $# -lt 2 ]] ; then
  153. echo "library edit id field [ value ]"
  154. exit 1
  155. fi
  156. ID=$1
  157. shift
  158. FIELD=$1
  159. shift
  160. VALUE=$1
  161. shift
  162. TMPDIR=. recset -e "ID = ${ID}" \
  163. -f "${FIELD}" -S "${VALUE}" \
  164. "${LIBRARY_FILE}"
  165. do_git add `basename "${LIBRARY_FILE}"`
  166. do_git commit -m "Edited record id ${ID}"
  167. }
  168. function do_edit_exp {
  169. if [[ $# -lt 3 ]] ; then
  170. echo "library edit-matching match-exp field value [ commit-message ]"
  171. exit 1
  172. fi
  173. MATCHEXPRESSION=$1
  174. shift
  175. FIELDNAME=$1
  176. shift
  177. VALUE=$1
  178. shift
  179. TMPDIR=. recset -e "${MATCHEXPRESSION}" \
  180. -f "${FIELDNAME}" -S "${VALUE}" \
  181. "${LIBRARY_FILE}"
  182. do_git add $(basename "${LIBRARY_FILE}")
  183. if [[ $1 != "" ]] ; then
  184. do_git commit -m "${1}"
  185. else
  186. do_git commit -m "Bulk edited records"
  187. fi
  188. }
  189. # edit-field ends here
  190. # withdraw-books, /home/swflint/Projects/library/library.org
  191. function do_withdraw {
  192. if [ $# -lt 1 ] ; then
  193. echo "library withdraw id"
  194. exit 1
  195. fi
  196. ID=$1
  197. TMPDIR=. recset -e "ID = ${ID}" \
  198. -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  199. -f "Location" -S "WITHDRAWN" \
  200. "${LIBRARY_FILE}"
  201. do_git add $(basename ${LIBRARY_FILE})
  202. do_git commit -m "Withdrew book ${ID}"
  203. }
  204. function remove_withdrawn {
  205. TMPDIR=. recdel -t Book \
  206. -e "Location ~ \"WITHDRAWN\"" \
  207. "${LIBRARY_FILE}"
  208. do_git add $(basename ${LIBRARY_FILE})
  209. do_git commit -m "Remove Withdrawn Books"
  210. }
  211. # withdraw-books ends here
  212. # initialize-database, /home/swflint/Projects/library/library.org
  213. function initialize {
  214. OLD=`pwd`
  215. mkdir -p "${LIBRARY_DIRECTORY}"
  216. cd "${LIBRARY_DIRECTORY}"
  217. [[ ! -e `basename "${LIBRARY_FILE}"` ]] && \
  218. cat <<EOF > `basename "${LIBRARY_FILE}"`
  219. # file-format, /home/swflint/Projects/library/library.org
  220. # -*- mode: rec -*-
  221. %rec: Book
  222. %doc: Foo
  223. %key: ID
  224. %unique: Title
  225. %type: ID int
  226. %type: Title line
  227. %type: Author line
  228. %type: LCCN line
  229. %type: ISBN regexp /[0-9]*X?/
  230. %type: Publisher line
  231. %type: Copyright int
  232. %type: Location line
  233. %type: Withdrawn date
  234. %type: Inserted date
  235. %type: Course line
  236. %type: LoanTo line
  237. %type: LoanOn date
  238. %typedef: CardPrint enum PRINTED REPRINT UNPRINTED
  239. %type: Card CardPrint
  240. %mandatory: Title Author LCCN Inserted
  241. %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn Course Card
  242. %auto: ID Inserted
  243. # file-format ends here
  244. EOF
  245. }
  246. # initialize-database ends here
  247. # loan, /home/swflint/Projects/library/library.org
  248. function do_loan {
  249. if [[ $# -lt 2 ]] ; then
  250. echo "library loan id name"
  251. exit 1
  252. fi
  253. ID=$1
  254. shift
  255. NAME=$1
  256. shift
  257. TMPDIR=. recset -e "ID = ${ID}" \
  258. -f "LoanTo" -S "${NAME}" \
  259. "${LIBRARY_FILE}"
  260. TMPDIR=. recset -e "ID = ${ID}" \
  261. -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  262. "${LIBRARY_FILE}"
  263. do_git add `basename "${LIBRARY_FILE}"`
  264. do_git commit -m "Loaned Book ${ID} to ${NAME}"
  265. }
  266. function do_return {
  267. if [[ $# -lt 1 ]] ; then
  268. echo "library return-book id"
  269. exit 1
  270. fi
  271. ID=$1
  272. shift
  273. TMPDIR=. recset -e "ID = ${ID}" \
  274. -f "LoanTo" -d \
  275. "${LIBRARY_FILE}"
  276. TMPDIR=. recset -e "ID = ${ID}" \
  277. -f "LoanOn" -d \
  278. "${LIBRARY_FILE}"
  279. do_git add `basename "${LIBRARY_FILE}"`
  280. do_git commit -m "Returned Book ${ID}"
  281. }
  282. # loan ends here
  283. # process-commands, /home/swflint/Projects/library/library.org
  284. COMMAND=$1
  285. shift
  286. case "${COMMAND}" in
  287. help)
  288. display_help
  289. exit
  290. ;;
  291. query)
  292. run_query "$@"
  293. exit
  294. ;;
  295. add)
  296. add_single "$@"
  297. exit
  298. ;;
  299. git)
  300. do_git "$@"
  301. exit
  302. ;;
  303. bulk-add)
  304. bulk_add "$@"
  305. exit
  306. ;;
  307. report)
  308. do_report "$@"
  309. exit
  310. ;;
  311. edit)
  312. do_edit "$@"
  313. exit
  314. ;;
  315. edit-matching)
  316. do_edit_exp "$@"
  317. exit
  318. ;;
  319. loan)
  320. do_loan "$@"
  321. exit
  322. ;;
  323. return-book)
  324. do_return "$@"
  325. exit
  326. ;;
  327. withdraw)
  328. do_withdraw "$@"
  329. exit
  330. ;;
  331. drop-withdrawn)
  332. remove_withdrawn "$@"
  333. exit
  334. ;;
  335. init)
  336. initialize
  337. exit
  338. ;;
  339. *)
  340. display_help
  341. exit
  342. esac
  343. # process-commands ends here
  344. # packaging ends here