#! /bin/bash
#
# (C) Copyright IBM 2002, 2003, 2004
#
# $Id: jBuildClasspathJar,v 1.55 2004/07/27 13:23:36 augart-oss Exp $
# @author Julian Dolby
# @modified Steven Augart
#   (Bash conversion plus more error checking.)
#

## This program is sensitive to the following environment variables:
## JIKES
## JAR
## ZIP_CMD
## AR
## CC
## FIND
## AR_FLAGS
## RVM_ROOT
## GNU_MAKE
## GREP
## XARGS


## CLASSPATH_ROOT		( optional )

ME="${0##*/}"

trap 'echo "$ME: Aborted due to a command exiting with error status ($?)"' ERR 2> /dev/null || :
set -e


##
## constants
##
TOP_LEVEL_PACKAGES="java javax gnu"

##
## defaults
##
export CHECK=0
export DEST_DIR=$RVM_ROOT/classpath
export OS_VER=$(uname -v)

JIKES_TOOL=$JIKES
JAR_TOOL=$JAR

declare -i AIX=0
[[ $OSTYPE == aix* ]] && AIX=1

CLASSPATH_URL=$(cat $RVM_ROOT/rvm/bin/classpath.stamp)

if [[ -e $CLASSPATH_ROOT/classpath/config.guess ]]; then
    export USE_LOCAL_CLASSPATH="yes"
    export DEST_DIR=$CLASSPATH_ROOT
else
    export USE_LOCAL_CLASSPATH="no"
fi

## What args do we give to classpath's "configure" command?
function classpath_configure_args () {
    if (( AIX )); then
	## AIX does not come with pkg-config or glib by default.  So, if 
	## someone is doing a default AIX build, we'll disable them.
	##
	## The GTk peers won't do any good without select hijacking, 
	## in any case. 
	echo "  --disable-gtk-peer CPPFLAGS='-D_POSIX_SOURCE -D_ALL_SOURCE -D__ppc__'"
	return;
    fi
    echo " --enable-portable-native-sync" # Needed for Classpath 0.09 and 0.08
    if ! [[ -e /usr/include/gtk-2.0/gtk/gtk.h ]]; then
	echo "  --disable-gtk-peer"
    else
	# We don't really support AWT yet; there are still some 
	# glitches needing resolution (AWT is still infernally slow for us).  
	# So, we can go ahead and just disable AWT for this default
	# Classpath build.  When this is enabled, as of this writing
	# (26 July 2004), the JMTk regressions fail due to an attempt
	# to contact the X server.  By setting --disable-gtk-peer 
	# in this situation, it should make it impossible for
	# Classpath to try to talk to X.
	# 
	# There is another reason for disabling the GTk peers in this
	# default GNU Classpath build.  As Steve Blackburn writes, the
	# GTk peers depend on GTk devel stuff, which in turn draws in
	# a large chunk of GNOME-related devel stuff that is
	# often/typically not installed by default.
	# 
	# If we were to leave the GTk peers enabled, a lot of people
	# might find that system would no longer build out of the box
	# because they did not have the GTk devel tools installed.
	echo "  --disable-gtk-peer"
    fi
}


##
## process arguments
##
while [[ $# > 0 ]] ; do
    case $1 in
	--destination)
	    DEST_DIR=$2
	    shift ;;
	
	--check)
	    CHECK=1 ;;
    esac	
    shift
done

##
## Warn if timestamps do not match.
##
mismatch=0
if ! [[ ${BUILDING_CLASSPATH_FROM_CVS_HEAD-} ]] && [[ "$USE_LOCAL_CLASSPATH" = "no" ]]; then
    if [[ -e $DEST_DIR/stamp ]]; then
	declare -i stat=0
	$DIFF $RVM_ROOT/rvm/bin/classpath.stamp $DEST_DIR/stamp > /dev/null || stat=$?
	if (( stat == 1 )); then
	    echo >&2 "${ME}: I think you want me to automatically download and build GNU Classpath.  But, there's already some sort of Classpath download in $DEST_DIR."
	    echo >&2 "The timestamp in $DEST_DIR/stamp is: $(< $DEST_DIR/stamp)"
	    echo >&2 "The timestamp I expect is: $(< $RVM_ROOT/rvm/bin/classpath.stamp)"
	    let ++mismatch;
	elif (( stat != 0 )); then
	    echo >&2 "$ME: $DIFF failed with bad status $stat; aborting"
	    exit $stat
	fi
    elif [[ -e $DEST_DIR ]]; then
	echo >&2 "${ME}: $DEST_DIR contains what might be an unknown GNU Classpath download"
	echo >&2 "${ME}: I expected to find a file named $DEST_DIR/stamp"
	echo >&2 "${ME}: that contains the timestamp: $(<  $RVM_ROOT/rvm/bin/classpath.stamp )"
	let ++mismatch;
    fi
fi

if (( mismatch )); then
    echo >&2 "${ME}: Either (a) set one of the CLASSPATH_ variables in your config file and rerun \"jconfigure\" or (b) delete $DEST_DIR.  Then restart this build.  If you wipe out $DEST_DIR, I will automatically download GNU Classpath and build it for you.  If you set one of the CLASSPATH_ variables, I will use the already built or installed GNU Classpath that I find there."
    exit 1
fi

##
## check if requested
##
if [[ $CHECK = 1 ]] && [[ -e $DEST_DIR/classpath/config.guess ]]; then
    system_type="$($DEST_DIR/classpath/config.guess)"
    if [[ -e $DEST_DIR/$system_type/lib/glibj.zip ]]; then
	exit 0
    fi
fi

##
## download classpath if needed
##
if [[ ! -e $DEST_DIR/classpath/TODO ]]; then
    if [[ $USE_LOCAL_CLASSPATH != "no" ]]; then
	echo >&2 "Please download GNU Classpath and unpack it in $DEST_DIR"
	echo >&2 "(Classpath 0.08 or newer is required; 0.10 is recommended)"
	exit 1
    fi
    echo "Downloading GNU Classpath"
    rm -rf $DEST_DIR
    mkdir -p $DEST_DIR
    cd $DEST_DIR
    
    $WGET --passive-ftp $CLASSPATH_URL -Oclasspath.tar.gz
    $GNU_TAR xzf classpath.tar.gz
    rm classpath.tar.gz
    mv classpath-* classpath
    cp $RVM_ROOT/rvm/bin/classpath.stamp stamp
else
    cd $DEST_DIR
fi

# stuff untarred into this dir
cd classpath

# Set up the directory for building if it's not already done.
# This will only be needed if you are using a CVS release.
if [[ ! -e $DEST_DIR/classpath/configure ]]; then
    echo >&2 "Setting up source tree"
    if [[ `uname -m` = i686 ]]; then
      aclocal
    else
      aclocal -I $RVM_ROOT/rvm/config/m4
    fi
    autoheader
    automake
    autoconf
fi

# configure and build library
system_type=$($DEST_DIR/classpath/config.guess)
if [[ ! -e $DEST_DIR/$system_type/lib/glibj.zip ]]; then
    mkdir -p $DEST_DIR/$system_type
    echo "Configuring for $system_type"
    cd $DEST_DIR/$system_type
    FIND=$FIND ZIP=$ZIP_CMD AR=$AR AR_FLAGS=$AR_FLAGS eval ../classpath/configure --enable-jni --with-jikes="$JIKES_TOOL" $(classpath_configure_args)
    if (( AIX )); then
	echo "Cleaning up native/fdlibm/Makefile for AIX"
	sed -e '/^CPPFLAGS/s/-D_ALL_SOURCE//' native/fdlibm/Makefile \
		> native/fdlibm/Makefile.tmp
	mv -f native/fdlibm/Makefile.tmp native/fdlibm/Makefile
    fi
    echo "Building the library"
    $GNU_MAKE
fi

# build classpath source jar
cp_src_jar_dir=$RVM_ROOT/support/lib
cp_src_jar=${cp_src_jar_dir}/classpathsrc.jar
echo "Building ${cp_src_jar}"
rm -f ${cp_src_jar}
mkdir -p $cp_src_jar_dir
touch ${cp_src_jar}
cd $DEST_DIR/classpath
$FIND $TOP_LEVEL_PACKAGES -name '*.java' | $GREP -v -f lib/standard.omit | $XARGS $JAR_TOOL uf ${cp_src_jar}
