#!/bin/bash

# patch-java-zi-dir.sh
# ====================
# Ian Ward Comfort, 2 February 2007
#
# This script attempts to patch the 1.4.2 and 1.5.0 JREs on a 10.4 OS X or OS X
# Server machine to use updated timezone information from the Olson database
# hosted at ftp://elsie.nci.nih.gov. It uses the javazic tool to compile a new
# JAVA_HOME/lib/zi directory, and installs the new data in two places:
#
#   /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/zi
#   /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/zi
#
# The javazic tool is only available as part of the source distribution of
# Sun's JDK. Since the Sun code can't be arbitrarily redistributed, you must
# download the JDK source yourself from here:
#
#   http://java.sun.com/j2se/jrl_download.html
#
# and place the zip archive in the same directory as this script before running
# it. (The archive should be called jdk-1_5_0-src-jrl.zip; if it isn't, edit
# the JDK_SOURCE variable appropriately.)
#
# You should be able to copy the contents of the new lib/zi directory to any
# JRE (of version 1.4 or 1.5, anyway) including OS X 10.3's JRE.
#
# ... But I would't try this for the first time on a production box.

OLSON=2007a
JDK_SOURCE=jdk-1_5_0-src-jrl
JDK_SOURCE_ARCHIVE="$JDK_SOURCE.zip"
START_DIR=$(pwd)
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
[ ! -r "$JDK_SOURCE_ARCHIVE" ] && echo "Couldn't find $JDK_SOURCE_ARCHIVE in this directory." && exit 65

echo -n "---> Backing up current Java timezone info ... "
mkdir -m0755 /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/zi.orig.$$ \
             /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/zi.orig.$$ || die "Couldn't create backup directories."
ditto /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/zi \
      /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/zi.orig.$$ || die "Couldn't backup icu archive."
ditto /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/zi \
      /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/zi.orig.$$ || die "Couldn't backup icu archive."
echo /System/Library/Frameworks/JavaVM.framework/Versions/{1.4.2,1.5.0}/Home/lib/zi.orig.$$

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

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 and factory zone out of the way ... "
mv tzdata$OLSON/*.tab tzdata$OLSON/yearistype.sh tzdata$OLSON/leapseconds tzdata$OLSON/factory . || die "Couldn't move files."
echo OK

echo -n "---> Unzipping $JDK_SOURCE_ARCHIVE ... "
mkdir -m0755 $JDK_SOURCE && unzip -qd $JDK_SOURCE "$START_DIR/$JDK_SOURCE_ARCHIVE" || die "Couldn't unzip JDK source."
echo OK

echo "---> Compiling javazic tool ... "
javac $JDK_SOURCE/j2se/src/share/classes/sun/tools/javazic/*.java || die "Compilation failed."
echo "---> Compile succeeded."

echo -n "---> Building new lib/zi directory ... "
mkdir zi$OLSON || die "Couldn't mkdir zi$OLSON."
java -cp $JDK_SOURCE/j2se/src/share/classes sun.tools.javazic.Main -V tzdata$OLSON -d zi$OLSON tzdata$OLSON/* $JDK_SOURCE/j2se/make/sun/javazic/tzdata_jdk/{gmt,jdk11_backward} || die "javazic failed."
echo OK

echo -n "---> Installing new lib/zi directory ... "
ditto zi$OLSON /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/zi || die "1.4.2 installation failed."
ditto zi$OLSON /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/zi || die "1.5.0 installation failed."
echo OK

echo
echo "---> Done. Working directory $WORKDIR will be left in place."
echo "---> Any currently-running JVMs will likely have to be restarted to use the updated timezone data."
echo
