Skip to content

Instantly share code, notes, and snippets.

@jasmas
Last active August 1, 2017 19:14
Show Gist options
  • Save jasmas/9f420de22fd52f2ca8f99ce9d8f01197 to your computer and use it in GitHub Desktop.
Save jasmas/9f420de22fd52f2ca8f99ce9d8f01197 to your computer and use it in GitHub Desktop.
Script to update North Central templates
#!/usr/bin/env bash
USERNAME=jmasker
LOGFILE=${HOME}/Library/Logs/`basename $0`.log
TEMPLATE_DIR="${HOME}/Library/Group Containers/UBF8T346G9.Office/User Content.localized/Templates.localized"
ARCHIVE=".old-nc-templates"
TEMPLATE_EXTS='*.dotm,*.dotx,*.xltm'
TMP=
function exit_fail {
case $1 in
2)
echo "`date` Parse error [2]" >> $LOGFILE
;;
3)
echo "`date` File I/O error [3]" >> $LOGFILE
;;
4)
echo "`date` Network failure [4]" >> $LOGFILE
;;
5)
echo "`date` SSL verification failure [5]" >> $LOGFILE
;;
6)
echo "`date` Username/password authentication failure [6]" >> $LOGFILE
;;
7)
echo "`date` Protocol errors [7]" >> $LOGFILE
;;
8)
echo "`date` Server issued an error response [8]" >> $LOGFILE
;;
9)
echo "`date` Error trashing old template archive [9]" >> $LOGFILE
;;
10)
echo "`date` Error moving updated templates into place [10]" >> $LOGFILE
echo "`date` Removed templates may be recovered from trash" >> $LOGFILE
;;
11)
echo "`date` Could not create temporary files [11]" >> $LOGFILE
;;
12)
echo "`date` Could not archive old templates [12]" >> $LOGFILE
;;
*)
echo "`date` ERROR [1]" >> $LOGFILE
;;
esac
popd > /dev/null 2>&1
rm -rf $TMP > /dev/null 2>&1
echo "Error. Dumping contents of $LOGFILE:"
cat $LOGFILE
echo
echo 'Update failed'
if [[ $1 -eq 0 ]]; then
exit 1
else
exit $1
fi
}
echo Template refresh initiated
echo "`date` Template refresh initiated" > $LOGFILE
echo "`date` Requesting credentials" >> $LOGFILE
echo -n Password for $USERNAME:
read -s PASSWORD
echo
echo "`date` Validating credentials" >> $LOGFILE
TMP=`mktemp -d`
pushd $TMP > /dev/null 2>&1 || exit_fail 11
wget -q --user $USERNAME --password $PASSWORD --delete-after \
https://iconnect.presidio.com/employees/NorthCentral/SA%20Templates/
TEST=$?
if [[ $TEST -eq 0 ]]; then
echo "`date` Credentials successfully validated" >> $LOGFILE
else
exit_fail $TEST
fi
echo 'Retrieving current templates'
echo "`date` Retrieving current templates" >> $LOGFILE
wget -q --show-progress --progress=bar:force:noscroll --user $USERNAME \
--password $PASSWORD -nH -r -np -nd -A "*.aspx,$TEMPLATE_EXTS" \
https://iconnect.presidio.com/employees/NorthCentral/SA%20Templates/ \
|| exit_fail $?
echo 'Trashing old template archive'
echo "`date` Trashing old template archive" >> $LOGFILE
mkdir -p "${TEMPLATE_DIR}/${ARCHIVE}"
trash -v "${TEMPLATE_DIR}/${ARCHIVE}" || exit_fail 9
echo 'Archiving old templates'
echo "`date` Archiving old templates" >> $LOGFILE
pushd "${TEMPLATE_DIR}" > /dev/null 2>&1 || exit_fail 6
mkdir "${ARCHIVE}"
mv *NC_* "${ARCHIVE}/" || exit_fail 12
popd > /dev/null 2>&1 || exit_fail 6
echo 'Moving updated templates into place'
echo "`date` Moving updated templates into place" >> $LOGFILE
mv -v ${TEMPLATE_EXTS//,/ } "${TEMPLATE_DIR}/" || exit_fail 10
echo 'Success!'
echo "`date` Success!" >> $LOGFILE
popd > /dev/null 2>&1
rm -rf $TMP > /dev/null 2>&1
exit 0
@jasmas
Copy link
Author

jasmas commented Aug 1, 2017

Requires wget
brew install wget
https://brew.sh/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment