library 8.3 KB

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