Bläddra i källkod

Rebuilt library management script

Samuel W. Flint 8 år sedan
förälder
incheckning
62b5497d36
1 ändrade filer med 118 tillägg och 62 borttagningar
  1. 118 62
      library

+ 118 - 62
library

@@ -1,18 +1,83 @@
 #!/bin/zsh -f
-
+# packaging, library.org
 LIBRARYFILE=~/.library/library.rec
 LIBRARYDIRECTORY=~/.library
-GIT=true
+GIT=detect
 
+# help-message, /home/swflint/.library/library.org
 if [[ $# -eq 0 ]] ; then
-    echo "library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field ]"
+    echo "library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field [ value ] | init ]"
     exit
 fi
 
+function display-help {
+    cat <<EOF
+library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] | edit id field [ value ] | init ]
+
+help:     Display this help message.
+query:    Query Library Database.
+add:      Add a singular book record.
+emacs:    View records in emacs.
+edit:     Edit the value of a specified field in a specified record.
+git:      Run a git command.
+report:   Run a report.
+init:     Initialize the database.
+bulk-add: Add a specified number of records.
+EOF
+}
+# help-message ends here
+
+# handle-reports, /home/swflint/.library/library.org
+function do-report {
+    NAME=$1
+    shift
+    case ${NAME} in
+        list)
+            for report in ${LIBRARYDIRECTORY}/*.report ;
+            do
+                echo " - $(basename -- ${report} .report)"
+            done
+        ;;
+        new)
+            REPORT=$1
+            shift
+            echo "# -*- mode: sh-script -*-" > ${LIBRARYDIRECTORY}/${REPORT}.report
+            emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report
+        ;;
+        *)
+            if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then
+               sh ${LIBRARYDIRECTORY}/${NAME}.report $@
+            fi
+    esac
+}
+# handle-reports ends here
+
+# handle-git, /home/swflint/.library/library.org
+function do-git {
+    FIRST=$1
+    if [[ $FIRST == "init" ]] ; then
+        OLD=`pwd`
+        cd ${LIBRARYDIRECTORY}
+        git $@
+        cd ${OLD}
+    else
+        if [[ $GIT == "detect" ]] ; then
+            OLD=`pwd`
+            cd ${LIBRARYDIRECTORY}
+            git $@
+            cd ${OLD}
+        fi
+    fi
+}
+# handle-git ends here
+
+# handle-query, /home/swflint/.library/library.org
 function run-query {
     recsel -t Book $@ ${LIBRARYFILE}
 }
+# handle-query ends here
 
+# add-book, /home/swflint/.library/library.org
 function add-single {
     echo -n "Title: "
     read TITLE
@@ -37,15 +102,12 @@ function add-single {
            -f ISBN -v "${ISBN}" \
            -f Location -v "${LOCATION}" \
            ${LIBRARYFILE}
-    if [[ $GIT == "true" ]] ; then
-        OLD=`pwd`
-        cd ${LIBRARYDIRECTORY}
-        git add `basename ${LIBRARYFILE}`
-        git commit -m "Added record for \"${TITLE}\""
-        cd ${OLD}
-    fi
+    do-git add `basename ${LIBRARYFILE}`
+    do-git commit -m "Added record for \"${TITLE}\""
 }
+# add-book ends here
 
+# add-in-bulk, /home/swflint/.library/library.org
 function bulk-add {
     GITOLD=${GIT}
     GIT=FALSE
@@ -54,59 +116,12 @@ function bulk-add {
         add-single
     done
     GIT=${GITOLD}
-    if [[ $GIT == "true" ]] ; then
-        OLD=`pwd`
-        cd ${LIBRARYDIRECTORY}
-        git add `basename ${LIBRARYFILE}`
-        git commit -m "Added ${1} records"
-        cd ${OLD}
-    fi
-}
-
-function do-report {
-    NAME=$1
-    shift
-    case ${NAME} in
-        list)
-            for report in ${LIBRARYDIRECTORY}/*.report ;
-            do
-                echo " - $(basename -- ${report} .report)"
-            done
-        ;;
-        new)
-            REPORT=$1
-            shift
-            echo "# -*- mode: shell-script -*-" > ${LIBRARYDIRECTORY}/${REPORT}.report
-            emacsclient --alternate-editor="" -n ${LIBRARYDIRECTORY}/${REPORT}.report
-        ;;
-        *)
-            if [[ -e ${LIBRARYDIRECTORY}/${NAME}.report ]] ; then
-               sh ${LIBRARYDIRECTORY}/${NAME}.report $@
-            fi
-    esac
-}
-
-function do-git {
-    OLD=`pwd`
-    cd ${LIBRARYDIRECTORY}
-    git $@
-    cd ${OLD}
-}
-
-function display-help {
-    cat <<EOF
-library [ help | query query-expressions | add | emacs | git [ other-args ] | bulk-add number | report [ name | list | new name ] ]
-
-help:     Display this help message.
-query:    Query Library Database.
-add:      Add a singular book record.
-emacs:    View records in emacs.
-git:      Run a git command.
-report:   Run a report.
-bulk-add: Add a specified number of records.
-EOF
+    do-git add `basename ${LIBRARYFILE}`
+    do-git commit -m "Added ${1} records"
 }
+# add-in-bulk ends here
 
+# edit-field, /home/swflint/.library/library.org
 function do-edit {
     ID=$1
     shift
@@ -124,8 +139,43 @@ function do-edit {
                -f "${FIELD}" -v "${VALUE}" \
                ${LIBRARYFILE}
     fi
+    do-git add `basename ${LIBRARYFILE}`
+    do-git commit -m "Edited record id ${ID}"
+}
+# edit-field ends here
+
+# initialize-database, /home/swflint/.library/library.org
+function initialize {
+    OLD=`pwd`
+    mkdir -p ${LIBRARYDIRECTORY}
+    cd ${LIBRARYDIRECTORY}
+    [[ -ne `basename ${LIBRARYFILE}` ]] && \
+        cat <<EOF > `basename ${LIBRARYFILE}`
+# file-format, /home/swflint/.library/library.org
+# -*- mode: rec -*-
+
+%rec: Book
+%doc: Foo
+%key: ID
+%unique: Title
+%type: ID int
+%type: Title line
+%type: Author line
+%type: LCCN line
+%type: ISBN regexp /[0-9]*X?/
+%type: Publisher line
+%type: Copyright int
+%type: Location line
+%type: Inserted date
+%mandatory: Title Author LCCN Inserted
+%allowed: ISBN Publisher Copyright Location
+%auto: ID Inserted
+# file-format ends here
+EOF
 }
+# initialize-database ends here
 
+# process-commands, /home/swflint/.library/library.org
 COMMAND=$1
 shift
 
@@ -155,14 +205,20 @@ case ${COMMAND} in
         exit
         ;;
     emacs)
-        emacsclient --alternate-editor="" -n ${LIBRARYFILE}
+        view-in-emacs
         exit
         ;;
     edit)
         do-edit $@
         exit
         ;;
+    init)
+        initialize
+        exit
+        ;;
     *)
         display-help
         exit
 esac
+# process-commands ends here
+# packaging ends here