install-osrf.sh: set ejabberd host correctly during registration on prod
[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 export DIR=$(readlink -f $(dirname $0))
9 NAME=outlook
10 if [ -z "${MAKEOPTS}" ]; then
11         echo "MAKEOPTS env variable is not set"
12         echo "If you really want this to be serialized, export MAKEOPTS=-j1"
13         echo "Otherwise set it about 1.5x your number of CPUs!"
14         exit 1
15 fi
16 [ -z "${T}" ] && export T=$(date -u +%Y%m%dT%H%M%SZ)
17 [ -z "${OUTDIR}" ] && export OUTDIR=$(mktemp --tmpdir -d ${NAME}_export.$T.XXXXXXX)
18 cd $OUTDIR
19 echo Prep $(date -uR)/@$(date +%s)
20 $DIR/fetch-shortcodes.sh shortcodes-$T.csv
21 cd $DIR
22 echo Exporting $(date -uR)/@$(date +%s)
23 export PERLOPT=-X
24 time ./sitka-full-export-parallel.sh >$OUTDIR/script.log 2>&1
25 rc=$?
26 cd $OUTDIR
27 mkdir split
28 mv *-split* split/
29 echo Compressing $(date -uR)/@$(date +%s)
30 pbzip2 -9nk -p4 *{marc,log}
31 pbzip2 -9n -p4 split/*{marc,log}
32 echo Stats $(date -uR)/@$(date +%s)
33 DELETE=1 $DIR/stats.sh >$OUTDIR/stats.log 2>&1
34 # Look for zeros in the output marc
35 echo Sanity $(date -uR)/@$(date +%s)
36 egrep -vsq '^([[:space:]]*0){2}.*marc$' *stats
37 grep_result=$?
38 # halt if found
39 if [ $rc -ne 0 -o $grep_result -ne 0 ]; then
40         echo "Failed to run export! Human needed! export=$rc grep_sanity=$grep_result" 1>&2
41         cat *stats 1>&2
42         exit 1
43 fi
44
45 # upload fails if curl username/password not provided
46 if [ -z "${CURL_PASSWORD}" ]; then
47     echo "Missing curl auth parameter(s), cannot upload to Outlook" 1>&2
48     exit 1
49 fi
50
51 # individually upload all required output files with curl
52 for CURL_FILE in `ls $OUTDIR | egrep '(.bz2|.stats|.csv)$' | grep -v script.log.bz2` ; do
53     $DIR/outlook-curl.sh $CURL_FILE $CURL_USERNAME $CURL_PASSWORD
54     echo $CURL_FILE >> $OUTDIR/Manifest
55 done
56
57 cp $DIR/email-template-${NAME}.sh $OUTDIR/
58 chmod +x email-template-${NAME}.sh
59 ./email-template-${NAME}.sh "$T" "@$(date +%s)" >email.txt
60 /usr/lib/sendmail -t -f sitka-tier2@bc.libraries.coop <email.txt