library 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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}/*.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}/${REPORT}.report
  51. emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report
  52. ;;
  53. *)
  54. if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then
  55. sh ${LIBRARYDIRECTORY}/${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. 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. ${LIBRARYFILE}
  129. do_git add `basename ${LIBRARYFILE}`
  130. do_git commit -m "Added record for \"${TITLE}\""
  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=${GIT}
  140. GIT=FALSE
  141. for i in {1..$1} ; do
  142. echo "Adding book number ${i}"
  143. add_single
  144. done
  145. GIT=${GITOLD}
  146. do_git add `basename ${LIBRARYFILE}`
  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 "ledger edit id field [ value ]"
  154. exit 1
  155. fi
  156. ID=$1
  157. shift
  158. FIELD=$1
  159. shift
  160. if [[ $FIELD = "Withdrawn" ]] ; then
  161. recset -e "ID = ${ID}" \
  162. -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  163. ${LIBRARYFILE}
  164. else
  165. VALUE=$1
  166. shift
  167. recset -e "ID = ${ID}" \
  168. -f "${FIELD}" -s "${VALUE}" \
  169. ${LIBRARYFILE}
  170. fi
  171. do_git add `basename ${LIBRARYFILE}`
  172. do_git commit -m "Edited record id ${ID}"
  173. }
  174. function do_edit_exp {
  175. if [[ $# -lt 3 ]] ; then
  176. echo "ledger edit-matching match-exp field value"
  177. exit 1
  178. fi
  179. MATCHEXPRESSION=$1
  180. shift
  181. FIELDNAME=$1
  182. shift
  183. VALUE=$1
  184. shift
  185. recset -e "${MATCHEXPRESSION}" \
  186. -f "${FIELDNAME}" -S "${VALUE}" \
  187. ${LIBRARYFILE}
  188. do_git add $(basename ${LIBRARYFILE})
  189. do_git commit -m "Edited records"
  190. }
  191. # edit-field ends here
  192. # initialize-database, /home/swflint/Projects/library/library.org
  193. function initialize {
  194. OLD=`pwd`
  195. mkdir -p ${LIBRARYDIRECTORY}
  196. cd ${LIBRARYDIRECTORY}
  197. [[ ! -e `basename ${LIBRARYFILE}` ]] && \
  198. cat <<EOF > `basename ${LIBRARYFILE}`
  199. # file-format, /home/swflint/Projects/library/library.org
  200. # -*- mode: rec -*-
  201. %rec: Book
  202. %doc: Foo
  203. %key: ID
  204. %unique: Title
  205. %type: ID int
  206. %type: Title line
  207. %type: Author line
  208. %type: LCCN line
  209. %type: ISBN regexp /[0-9]*X?/
  210. %type: Publisher line
  211. %type: Copyright int
  212. %type: Location line
  213. %type: Withdrawn date
  214. %type: Inserted date
  215. %type: LoanTo line
  216. %type: LoanOn date
  217. %mandatory: Title Author LCCN Inserted
  218. %allowed: ISBN Publisher Copyright Location Withdrawn LoanTo LoanOn
  219. %auto: ID Inserted
  220. # file-format ends here
  221. EOF
  222. }
  223. # initialize-database ends here
  224. # loan, /home/swflint/Projects/library/library.org
  225. function do_loan {
  226. if [[ $# -lt 2 ]] ; then
  227. echo "library loan id name"
  228. exit 1
  229. fi
  230. ID=$1
  231. shift
  232. NAME=$1
  233. shift
  234. recset -e "ID = ${ID}" \
  235. -f "LoanTo" -S "${NAME}" \
  236. ${LIBRARYFILE}
  237. recset -e "ID = ${ID}" \
  238. -f "LoanOn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
  239. ${LIBRARYFILE}
  240. do_git add `basename ${LIBRARYFILE}`
  241. do_git commit -m "Loaned Book ${ID} to ${NAME}"
  242. }
  243. function do_return {
  244. if [[ $# -lt 1 ]] ; then
  245. echo "library return-book id"
  246. exit 1
  247. fi
  248. ID=$1
  249. shift
  250. recset -e "ID = ${ID}" \
  251. -f "LoanTo" -d \
  252. ${LIBRARYFILE}
  253. recset -e "ID = ${ID}" \
  254. -f "LoanOn" -d \
  255. ${LIBRARYFILE}
  256. do_git add `basename ${LIBRARYFILE}`
  257. do_git commit -m "Returned Book ${ID}"
  258. }
  259. # loan ends here
  260. # process-commands, /home/swflint/Projects/library/library.org
  261. COMMAND=$1
  262. shift
  263. case ${COMMAND} in
  264. help)
  265. display_help
  266. exit
  267. ;;
  268. query)
  269. run_query $@
  270. exit
  271. ;;
  272. add)
  273. add_single $@
  274. exit
  275. ;;
  276. git)
  277. do_git $@
  278. exit
  279. ;;
  280. bulk-add)
  281. bulk_add $@
  282. exit
  283. ;;
  284. report)
  285. do_report $@
  286. exit
  287. ;;
  288. edit)
  289. do_edit $@
  290. exit
  291. ;;
  292. edit-matching)
  293. do_edit_exp "$@"
  294. exit
  295. ;;
  296. loan)
  297. do_loan $@
  298. exit
  299. ;;
  300. return-book)
  301. do_return $@
  302. exit
  303. ;;
  304. init)
  305. initialize
  306. exit
  307. ;;
  308. *)
  309. display_help
  310. exit
  311. esac
  312. # process-commands ends here
  313. # packaging ends here