library 6.8 KB

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