library 8.0 KB

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