#!/bin/bash
-# a script to generate an SQL file containing reports templates that can be
-# imported into another Evergreen server
+# Copyright (C) 2015, Georgia Public Library Service
+# Chris Sharp <csharp@georgialibraries.org>
-DB_USER="evergreen"
-DB_HOST="localhost"
-DB_NAME="evergreen"
-OUTFILE="templates_to_import_`date +%F`.sql"
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
-read -p "Please enter a comma-and-space-separated list of template ids for the reports you're copying (e.g., 12345, 12346, 12347 - No comma necessary if just one template): " TEMPLATES
-read -p "Please enter the id for the destination template owner on the destination server: " OWNER
-read -p "Please enter the id for the destination template folder on the destination server for the copied templates: " FOLDER
+# A script to copy reports templates from one user to another. If you're copying
+# templates to a different server, both users must exist on both servers with
+# the same barcode. At least that's as much as the script will do initially.
+# The script generates an SQL file to be run on the target server.
+PSQL="/usr/bin/psql"
+PG_USER="evergreen"
+PG_HOST="localhost"
+PG_DB="evergreen"
+
+read -p "Please enter the barcode for the user who currently owns the report templates you'd like to copy: " OLD_REPORT_USER
+read -p "Please enter the barcode for the user who will be receiving the report templates: " NEW_REPORT_USER
+read -p "Please enter the IDs for the report templates you would like to copy, separated by a comma and space (e.g., 1234, 5678, 9012): " TEMPLATE_IDS
+OUTFILE="copy_report_templates_${OLD_REPORT_USER}_to_${NEW_REPORT_USER}.sql"
+TEMPLATE_FOLDER_NAME="Reports from $OLD_REPORT_USER `date +%F`"
read -d '' SQL <<EOF
-select 'insert into reporter.template (owner, name, description, data, folder) values ($OWNER, ''' || name || ''', ''' || description || ''', ''' || data || ''', $FOLDER);'
-from reporter.template
-where id in ($TEMPLATES);
+select 'insert into reporter.template (
+ owner,
+ name,
+ description,
+ data,
+ folder
+ )
+ values (
+ (select usr from actor.card where barcode = ''$NEW_REPORT_USER''), '''
+ || name || ''', '''
+ || description || ''', '''
+ || data || ''',
+ (
+ select id
+ from reporter.template_folder
+ where owner in (
+ select usr
+ from actor.card
+ where barcode = ''$NEW_REPORT_USER''
+ )
+ and name = ''$TEMPLATE_FOLDER_NAME''
+ )
+ );'
+from reporter.template where id in ($TEMPLATE_IDS);
EOF
+echo "-- copying reports templates from $OLD_REPORT_USER to $NEW_REPORT_USER" > $OUTFILE
echo "begin;" >> $OUTFILE
-psql -A -t -U "$DB_USER" -h "$DB_HOST" -c "$SQL" "$DB_NAME" >> "$OUTFILE"
+echo "insert into reporter.template_folder (
+ owner,
+ name
+ )
+ values (
+ (select usr from actor.card where barcode = '$NEW_REPORT_USER'),
+ '$TEMPLATE_FOLDER_NAME'
+ );" >> $OUTFILE
+$PSQL -A -t -h $PG_HOST -U $PG_USER -c "$SQL" $PG_DB >> $OUTFILE
echo "commit;" >> $OUTFILE
-
+echo "Complete. Please run psql on the file $OUTFILE on the server to which you want to copy these templates."