library 8.9 KB

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