library 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/zsh -f
  2. # packaging, library.org
  3. LIBRARYFILE=~/.library/library.rec
  4. LIBRARYDIRECTORY=~/.library
  5. GIT=detect
  6. # help-message, /home/swflint/.library/library.org
  7. if [[ $# -eq 0 ]] ; then
  8. echo "library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field [ value ] | init ]"
  9. exit
  10. fi
  11. function display-help {
  12. cat <<EOF
  13. library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field [ value ] | init ]
  14. help: Display this help message.
  15. query: Query Library Database.
  16. add: Add a singular book record.
  17. emacs: View records in emacs.
  18. edit: Edit the value of a specified field in a specified record.
  19. git: Run a git command.
  20. report: Run a report.
  21. init: Initialize the database.
  22. bulk-add: Add a specified number of records.
  23. EOF
  24. }
  25. # help-message ends here
  26. # handle-reports, /home/swflint/.library/library.org
  27. function do-report {
  28. NAME=$1
  29. shift
  30. case ${NAME} in
  31. list)
  32. for report in ${LIBRARYDIRECTORY}/*.report ;
  33. do
  34. echo " - $(basename -- ${report} .report)"
  35. done
  36. ;;
  37. new)
  38. REPORT=$1
  39. shift
  40. echo "# -*- mode: sh-script -*-" > ${LIBRARYDIRECTORY}/${REPORT}.report
  41. emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report
  42. ;;
  43. *)
  44. if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then
  45. sh ${LIBRARYDIRECTORY}/${NAME}.report $@
  46. fi
  47. esac
  48. }
  49. # handle-reports ends here
  50. # handle-git, /home/swflint/.library/library.org
  51. function do-git {
  52. FIRST=$1
  53. if [[ $FIRST == "init" ]] ; then
  54. OLD=`pwd`
  55. cd ${LIBRARYDIRECTORY}
  56. git $@
  57. cd ${OLD}
  58. else
  59. if [[ $GIT == "detect" ]] ; then
  60. OLD=`pwd`
  61. cd ${LIBRARYDIRECTORY}
  62. git $@
  63. cd ${OLD}
  64. fi
  65. fi
  66. }
  67. # handle-git ends here
  68. # handle-query, /home/swflint/.library/library.org
  69. function run-query {
  70. recsel -t Book $@ ${LIBRARYFILE}
  71. }
  72. # handle-query ends here
  73. # add-book, /home/swflint/.library/library.org
  74. function add-single {
  75. echo -n "Title: "
  76. read TITLE
  77. echo -n "Author: "
  78. read AUTHOR
  79. echo -n "LCCN: "
  80. read LCCN
  81. echo -n "Copyright: "
  82. read COPYRIGHT
  83. echo -n "Publisher: "
  84. read PUBLISHER
  85. echo -n "ISBN: "
  86. read ISBN
  87. echo -n "Location: "
  88. read LOCATION
  89. recins -t Book \
  90. -f Title -v "${TITLE}" \
  91. -f Author -v "${AUTHOR}" \
  92. -f LCCN -v "${LCCN}" \
  93. -f Copyright -v "${COPYRIGHT}" \
  94. -f Publisher -v "${PUBLISHER}" \
  95. -f ISBN -v "${ISBN}" \
  96. -f Location -v "${LOCATION}" \
  97. ${LIBRARYFILE}
  98. do-git add `basename ${LIBRARYFILE}`
  99. do-git commit -m "Added record for \"${TITLE}\""
  100. }
  101. # add-book ends here
  102. # add-in-bulk, /home/swflint/.library/library.org
  103. function bulk-add {
  104. GITOLD=${GIT}
  105. GIT=FALSE
  106. for i in {1..$1} ; do
  107. echo "Adding book number ${i}"
  108. add-single
  109. done
  110. GIT=${GITOLD}
  111. do-git add `basename ${LIBRARYFILE}`
  112. do-git commit -m "Added ${1} records"
  113. }
  114. # add-in-bulk ends here
  115. # edit-field, /home/swflint/.library/library.org
  116. function do-edit {
  117. ID=$1
  118. shift
  119. FIELD=$1
  120. shift
  121. if [[ $FIELD = "Withdrawn" ]] ; then
  122. recins -e "ID = ${ID}" \
  123. -f "Withdrawn" -v `date "%a, %d %b %Y %H:%M:%S %z"` \
  124. ${LIBRARYFILE}
  125. else
  126. VALUE=$1
  127. shift
  128. recins -e "ID = ${ID}" \
  129. -f "${FIELD}" -v "${VALUE}" \
  130. ${LIBRARYFILE}
  131. fi
  132. do-git add `basename ${LIBRARYFILE}`
  133. do-git commit -m "Edited record id ${ID}"
  134. }
  135. # edit-field ends here
  136. # initialize-database, /home/swflint/.library/library.org
  137. function initialize {
  138. OLD=`pwd`
  139. mkdir -p ${LIBRARYDIRECTORY}
  140. cd ${LIBRARYDIRECTORY}
  141. [[ -ne `basename ${LIBRARYFILE}` ]] && \
  142. cat <<EOF > `basename ${LIBRARYFILE}`
  143. # file-format, /home/swflint/.library/library.org
  144. # -*- mode: rec -*-
  145. %rec: Book
  146. %doc: Foo
  147. %key: ID
  148. %unique: Title
  149. %type: ID int
  150. %type: Title line
  151. %type: Author line
  152. %type: LCCN line
  153. %type: ISBN regexp /[0-9]*X?/
  154. %type: Publisher line
  155. %type: Copyright int
  156. %type: Location line
  157. %type: Inserted date
  158. %mandatory: Title Author LCCN Inserted
  159. %allowed: ISBN Publisher Copyright Location
  160. %auto: ID Inserted
  161. # file-format ends here
  162. EOF
  163. }
  164. # initialize-database ends here
  165. # view-in-emacs, /home/swflint/.library/library.org
  166. function view-in-emacs {
  167. emacsclient --alternate-editor="" -n ${LIBRARYFILE}
  168. }
  169. # view-in-emacs ends here
  170. # process-commands, /home/swflint/.library/library.org
  171. COMMAND=$1
  172. shift
  173. case ${COMMAND} in
  174. help)
  175. display-help
  176. exit
  177. ;;
  178. query)
  179. run-query $@
  180. exit
  181. ;;
  182. add)
  183. add-single
  184. exit
  185. ;;
  186. git)
  187. do-git $@
  188. exit
  189. ;;
  190. bulk-add)
  191. bulk-add $@
  192. exit
  193. ;;
  194. report)
  195. do-report $@
  196. exit
  197. ;;
  198. emacs)
  199. view-in-emacs
  200. exit
  201. ;;
  202. edit)
  203. do-edit $@
  204. exit
  205. ;;
  206. init)
  207. initialize
  208. exit
  209. ;;
  210. *)
  211. display-help
  212. exit
  213. esac
  214. # process-commands ends here
  215. # packaging ends here