# 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"
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."
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"