#!/bin/sh

# $Id: patch-o-matic.sh,v 1.1 1999/05/31 20:26:02 sbb Exp $

# Given the JDK 1.2 Solaris sources in a zip file, and an existing JDK binary
# release directory, (optionally) create and unpack the Solaris diffs into
# a target directory, and apply the Blackdown JDK 1.2 diffs to the sources,
# resulting in a buildable Linux JDK 1.2 source tree.

diffs="${JDK_DIFFS_FILE:-/a5/sbb/java/jdk1.2.diffs.gz}"
bootJDKDir="${JDK_BOOT_DIR:-/a7/jdk1.2}"
targDir="${JDK_TARGET_DIR:-.}"
virginSrcZip="${JDK_SRC_ZIP_FILE:-/a5/sbb/java/JDK1_2FCS.solwin32.03_Dec.zip}"

#############################################################################
#
# No user serviceable parts below this line 
#
#############################################################################

unpackZip() {
    unzip "${virginSrcZip}"
}

renameDirectories() {
    mv build/solaris build/linux
    mv src/solaris src/linux
    mv ext/i18n/build/solaris ext/i18n/build/linux
    mv ext/i18n/src/solaris ext/i18n/src/linux
    mv ext/iiimp/build/solaris ext/iiimp/build/linux
}

getExtraFiles () {
    cp "${bootJDKDir}/jre/lib/i386/libsunwjit.so" \
	src/linux/lib/i386/linux.libsunwjit.so
# hack!	
    cp "${bootJDKDir}/jre/lib/i386/libsunwjit.so" \
	src/linux/lib/i386/linux.libsunwjit_g.so
}

sanityCheckFiles () {
    local error=
    for file
    do
	if [ ! -f "${file}" ]
	then
	    echo "Error: Required file ${file} not found or does not name a file" >&2
	    error=1
	fi
    done

    if [ "${error}" ]
    then
	exit 1
    fi
}

sanityCheckDirs () {
    local error=
    for dir
    do
	if [ ! -d "${dir}" ]
	then
	    echo "Error: Required directory ${dir} not found or does not name a directory" >&2
	    error=1
	fi
    done

    if [ "${error}" ]
    then
	exit 1
    fi
}


sanityCheckOptDirs () {
    local error=
    for dir
    do
	if [ ! -d "${dir}" -a -e "${dir}" ]
	then
	    echo "Error: Required directory ${dir} does not name a directory" >&2
	    error=1
	fi
    done

    if [ "${error}" ]
    then
	exit 1
    fi
}


sanityCheckFiles "${diffs}" "${virginSrcZip}"
sanityCheckDirs "${bootJDKDir}"
sanityCheckOptDirs "${targDir}"

if [ ! -e "${targDir}" ]
then
    mkdir -p "${targDir}"
fi

cd "${targDir}"

unpackZip

renameDirectories

getExtraFiles

zcat "${diffs}" | patch -p1

