Wednesday, February 23, 2011

Script for automatically update InstaDMG images

Just finished this script, which has room for improvements, but it works as is. This script uses InstaUp2Date. What this script does is that it searches a given folder for catalog files and if the checksum since last run is different, it launches instaUp2Date.

Set up a LaunchDaemon and let it run every night to check if you have done any changes to any instaUp2Date catalog files, and if so, it builds the new image.

Given as is...

#!/bin/bash

# First of all, make sure this is run as root
if [[ "$(/usr/bin/whoami)" != "root" ]]; then
printf '\nMust be run as root!\n\n'
exit 1
fi

usage()
{
cat << EOF ######################################## Put this script in /AddOns/InstaUp2Date
Usage: $0
########################################
EOF
}

#if [[ -z $1 ]]
#then
# usage
# exit 1
#fi

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

SCRIPTDIR=$(dirname $0)
SHADIR="$SCRIPTDIR/shasums"
OUTPUTDIR="$SCRIPTDIR/../../OutputFiles" # Path to default InstaDMG Output path
TIMESTAMP=$(date "+%y%m%d_%H.%M.%S")
CATALOGDIR="$SCRIPTDIR/CatalogFiles"

if [ ! -d $SHADIR ];
then
mkdir "$SHADIR"
fi

for i in $(find "$CATALOGDIR" -type f -print | egrep -i '.catalog$')
do
FILENAME=$(basename "$i" .catalog)
if [ ! -f "$SHADIR/$FILENAME.sha" ];
then
echo "Old shasum for \"$i\" does not exist. Assuming it's a new catalog file. Running InstaUp2Date for \"$i\""
/usr/bin/python "$SCRIPTDIR/instaUp2Date.py" --process $FILENAME
/usr/bin/shasum "$i" | awk '{print $1}' > "$SHADIR/$FILENAME.sha"
else
echo "Old shasum for \"$i\" exists"
fi

NEWSHA=$(/usr/bin/shasum "$i" | awk '{print $1}')
OLDSHA=$(cat "$SHADIR/$FILENAME.sha")

if [ $NEWSHA = $OLDSHA ];
then
echo "No change done to the catalogfile: $i"
else
echo "Found changes in catalog file, renaming old image and running instaUp2Date for catalog file: $i"
OUTFILENAME=$(cat $i | grep "Output File Name" | awk -F" = " '{print $2}')
for image in $(find "$OUTPUTDIR" -name "$OUTFILENAME*");
do
mv $image $image-$TIMESTAMP
done
/usr/bin/python "$SCRIPTDIR/instaUp2Date.py" --process $FILENAME
echo "Checksumming: $i"
/usr/bin/shasum "$i" | awk '{print $1}' > "$SHADIR/$FILENAME.sha"
fi

done

IFS=$SAVEIFS

exit 0

Can also be downloaded here (with logging added): http://db.tt/95GzhdC