Modify Outlook export profile for Radium, per RT#74886
[sitka/sitka-tools.git] / marc_export_custom / sitka-full-export-parallel-outlook-wrapper.sh
1 #!/bin/bash
2 # This is meant to be fire-and-forget
3 # Only use it if you trust the output!
4
5 CURL_USERNAME=$1
6 CURL_PASSWORD=$2
7
8 EXPORTDIR="/marcexport"
9
10 export DIR=$(readlink -f $(dirname $0))
11 NAME=outlook
12 if [ -z "${MAKEOPTS}" ]; then
13         echo "MAKEOPTS env variable is not set"
14         echo "If you really want this to be serialized, export MAKEOPTS=-j1"
15         echo "Otherwise set it about 1.5x your number of CPUs!"
16         exit 1
17 fi
18 [ -z "${T}" ] && export T=$(date -u +%Y%m%dT%H%M%SZ)
19 [ -z "${OUTDIR}" ] && export OUTDIR=$(mktemp --tmpdir=${EXPORTDIR} -d ${NAME}_export.$T.XXXXXXX)
20 cd $OUTDIR
21 echo Prep $(date -uR)/@$(date +%s)
22 $DIR/fetch-shortcodes.sh shortcodes-$T.csv
23 cd $DIR
24 echo Exporting $(date -uR)/@$(date +%s)
25 export PERLOPT=-X
26 time ./sitka-full-export-parallel.sh >$OUTDIR/script.log 2>&1
27 rc=$?
28 cd $OUTDIR
29 mkdir split
30 mv *-split* split/
31 echo Compressing $(date -uR)/@$(date +%s)
32 pbzip2 -9nk -p4 *{marc,log}
33 pbzip2 -9n -p4 split/*{marc,log}
34 echo Stats $(date -uR)/@$(date +%s)
35 DELETE=1 $DIR/stats.sh >$OUTDIR/stats.log 2>&1
36 # Look for zeros in the output marc
37 echo Sanity $(date -uR)/@$(date +%s)
38 egrep -vsq '^([[:space:]]*0){2}.*marc$' *stats
39 grep_result=$?
40 # halt if found
41 if [ $rc -ne 0 -o $grep_result -ne 0 ]; then
42         echo "Failed to run export! Human needed! export=$rc grep_sanity=$grep_result" 1>&2
43         cat *stats 1>&2
44         exit 1
45 fi
46
47 # upload fails if curl username/password not provided
48 if [ -z "${CURL_PASSWORD}" ]; then
49     echo "Missing curl auth parameter(s), cannot upload to Outlook" 1>&2
50     exit 1
51 fi
52
53 # individually upload all required output files with curl
54 for CURL_FILE in `ls $OUTDIR | egrep '(.bz2|.stats|.csv)$' | grep -v script.log.bz2` ; do
55     $DIR/outlook-curl.sh $CURL_FILE $CURL_USERNAME $CURL_PASSWORD
56     echo $CURL_FILE >> $OUTDIR/Manifest
57 done
58
59 cp $DIR/email-template-${NAME}.sh $OUTDIR/
60 chmod +x email-template-${NAME}.sh
61 ./email-template-${NAME}.sh "$T" "@$(date +%s)" >email.txt
62 /usr/lib/sendmail -t -f sitka-tier2@bc.libraries.coop <email.txt
63
64 # log successful export and cleanup old exports, saving this one and the previous one
65 echo "$OUTDIR" >> $DIR/sitka_full_outlook.log
66 head -n -2 $DIR/sitka_full_outlook.log | while read OLDEXPORT ; do
67   [ -d "${OLDEXPORT}" ] && rm -rf "${OLDEXPORT}"
68 done