Adding ability to schedule IntraPINES reports via cron.
authorChris Sharp <csharp@georgialibraries.org>
Mon, 1 Dec 2014 12:30:02 +0000 (07:30 -0500)
committerChris Sharp <csharp@georgialibraries.org>
Mon, 1 Dec 2014 12:30:02 +0000 (07:30 -0500)
intrapines-loans/intrapines-loans-reports.sh

index fe40b2e..82fd49e 100755 (executable)
@@ -4,14 +4,26 @@
 # Chris Sharp <csharp@georgialibraries.org>
 #
 
+Usage () {
+echo -e "Usage:\n\t$0 [-s START_DATE] [-e END_DATE]\n\n\tDates are in YYYY-MM-DD format."
+exit;
+}
+
+while getopts s:e:h OPTIONS
+do     case "$OPTIONS" in
+       s)      START_DATE="$OPTARG" ;;
+       e)      END_DATE="$OPTARG" ;;
+       h)  Usage ;;
+       esac
+done
 
 WORK_DIR=$(pwd)
-PGUSERNAME="evergreen"
-DBHOST="localhost"
-DBNAME="evergreen"
+PGUSERNAME="mydbuser"
+DBHOST="mydbhost"
+DBNAME="mydbname"
 OUTBOX="$WORK_DIR/outbox"
 
-
+GetDates () {
 clear
 echo "This script will generate an SQL script to run IntraPINES loans reports"
 echo "(for loans between systems not including loans between branches within"
@@ -25,10 +37,13 @@ if [ $RESPONSE != "y" ]; then
        echo "Aborting."
        exit
 fi
+}
 
+GenerateReports () {
 SQL_TEMPLATE="intrapines_matrix_report-template.sql"
 TMP_TEMPLATE="/tmp/intrapines_matrix_report-$START_DATE-to-$END_DATE.sql"
 README="/tmp/README-intraPINES-loans-$START_DATE-to-$END_DATE.txt"
+EMAILS="recipient@example.org"
 ADMIN_EMAIL="admin-email@example.org"
 
 echo "Generating SQL template."
@@ -42,11 +57,28 @@ cp "$WORK_DIR"/README.template "$README"
 sed -i "s^FQDN^$(hostname -f)^g" "$README"
 sed -i "s^TODAYS_DATE^$(date)^g" "$README"
 sed -i "s^ADMIN_EMAIL^$ADMIN_EMAIL^g" "$README"
+}
+
+ZipReports () {
 echo "Zipping results."
 zip -jmT $OUTBOX/intrapines-reports-$START_DATE-to-$END_DATE.zip /tmp/*$START_DATE-to-$END_DATE.csv $README
+}
+
+CleanUp () {
 # clean up
 echo "Cleaning up."
 rm -f $README $TMP_TEMPLATE
-echo "Complete! Zipped results available at $OUTBOX/intrapines-reports-$START_DATE-to-$END_DATE.zip"
+}
+MailResults () {
+echo "IntraPINES loans report for $START_DATE to $END_DATE attached." | mutt -s "IntraPINES Loans report $START_DATE - $END_DATE" -a $OUTBOX/intrapines-reports-$START_DATE-to-$END_DATE.zip -- $EMAILS
+}
+
+if [ -z $START_DATE ] || [ -z $END_DATE ]; then
+       GetDates
+fi
+GenerateReports
+ZipReports
+CleanUp
+MailResults
 
-#TODO: automatically transfer these to the local PINES samba share
+echo "Complete! Zipped results available at $OUTBOX/intrapines-reports-$START_DATE-to-$END_DATE.zip"