#!/bin/bash

# patch-panthertz.sh
# ==================
# Ian Ward Comfort, 27 January 2007
#
# This script attempts to patch a machine running version 10.3 of OS X or OS X
# Server to use updated timezone information from the Olson database hosted at
# ftp://elsie.nci.nih.gov. It uses zic(8) to compile new zoneinfo files (for
# POSIX-like applications), and builds a new ICU archive file using the same
# Olson data (for applications that use libicucore).
#
# NOTE: Although Darwin's zic(8) is capable of incorporating leap second data
# into its zone files, this script does not take advantage of this capability.
# The stock zoneinfo directory appears to omit this information, so for maximal
# compatibility we do the same.

OLSON=2007a
DARWIN_ICU=ICU-3.13
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

die () { 
  retval=$?
  echo Error $retval
  [ -n "$1" ] && echo $1
  exit $retval
}

[ $EUID -ne 0 ] && echo "You must be root to run this script." && exit 77
[ ! -e /usr/bin/gnumake ] && echo "Can't find gnumake. Have you installed Xcode?" && exit 78

echo -n "---> Checking operating system and architecture ... "
uname -srp | grep 'Darwin 7.[0-9].[0-9] powerpc' || die "This doesn't look like an OS X 10.3 install."

echo -n "---> Backing up current files ... "
mkdir -m0755 /usr/share/icu.orig.$$ /usr/share/zoneinfo.orig.$$ || die "Couldn't create backup directories."
ditto /usr/share/icu      /usr/share/icu.orig.$$      || die "Couldn't backup icu archive."
ditto /usr/share/zoneinfo /usr/share/zoneinfo.orig.$$ || die "Couldn't backup zoneinfo files."
echo /usr/share/{icu,zoneinfo}.orig.$$

echo -n "---> Creating working directory ... "
WORKDIR="${TMPDIR:=/tmp}/panthertz.$$"
mkdir -m0755 "$WORKDIR" && cd "$WORKDIR" || die "Couldn't make working directory in $TMPDIR."
echo "$WORKDIR"

echo
echo "---> Patching zoneinfo files"
echo

echo -n "---> Downloading ftp://elsie.nci.nih.gov/pub/tzdata$OLSON.tar.gz ... "
curl -sO ftp://elsie.nci.nih.gov/pub/tzdata$OLSON.tar.gz || die "Couldn't download tzdata."
echo OK

echo -n "---> Untarring tzdata$OLSON.tar.gz ... "
mkdir tzdata$OLSON && tar Cxzf tzdata$OLSON tzdata$OLSON.tar.gz || die "Couldn't untar tzdata."
echo OK

echo -n "---> Moving non-zonefiles out of the way ... "
mv tzdata$OLSON/*.tab tzdata$OLSON/leapseconds tzdata$OLSON/yearistype.sh . || die "Couldn't move files."
echo OK

echo -n "---> Compiling new zoneinfo ... "
/usr/sbin/zic -y ./yearistype.sh tzdata$OLSON/* || die "zic(8) failed."
echo OK

echo
echo "---> Patching ICU archive"
echo

echo -n "---> Downloading http://www.opensource.apple.com/darwinsource/10.3.9/$DARWIN_ICU.tar.gz ... "
curl -sO http://www.opensource.apple.com/darwinsource/10.3.9/$DARWIN_ICU.tar.gz || die "Couldn't download ICU tarball."
echo OK

echo -n "---> Untarring $DARWIN_ICU.tar.gz ... "
tar xzf $DARWIN_ICU.tar.gz || die "Couldn't untar ICU source."
cd $DARWIN_ICU/icuSources/tools/gentz || die "Couldn't chdir."
echo OK

echo -n "---> Copying tzdata$OLSON.tar.gz ... "
cp ../../../../tzdata$OLSON.tar.gz . || die "Couldn't copy tzdata."
echo OK

echo -n "---> Untarring tzdata$OLSON.tar.gz ... "
mkdir tzdata$OLSON && tar Cxzf tzdata$OLSON tzdata$OLSON.tar.gz || die "Couldn't untar tzdata."
echo OK

echo -n "---> Patching tz.alias and tz.default ... "
sed -i.bak1 -e 's/Buenos_Aires/Argentina\/&/' tz.alias tz.default && \
  sed -i.bak2 -e 's/Indianapolis/Indiana\/&/' tz.alias && \
  sed -i.bak3 -e 's/^[EHM]ST/#&/' tz.alias || die "Patching failed."
echo OK

echo -n "---> Backing up old timezone.txt ... "
cp ../../data/misc/timezone.txt ../../data/misc/timezone.txt.bak || die "Couldn't copy timezone.txt."
echo OK

echo "---> Generating new timezone.txt ... "
perl ./tz.pl tzdata$OLSON ../../data/misc/timezone.txt || die "tz.pl failed."

echo "---> Configuring ICU for build ..."
cd ../.. && ./runConfigureICU MacOSX --with-data-packaging=archive || die "---> runConfigureICU failed."

echo "---> Building new ICU library ..."
gnumake || die "---> gnumake failed."

echo -n "---> Installing new icudt26b.dat ... "
install -o root -g wheel -m 0644 -Sp data/out/icudt26b.dat /usr/share/icu/icudt26b.dat || die "Couldn't install new icudt26b.dat."
echo OK

echo
echo "---> Done. Working directory $WORKDIR will be left in place."
echo "---> A reboot is highly recommended to ensure all processes are using the updated timezone data."
echo
