
tarball=openjdk-6-src-b40-22_aug_2016.tar.xz
version=6b40-1.13.12
cacaotb=cacao-68fe50ac34ec.tar.gz
jamvmtb=jamvm-2.0.0.tar.gz
base=openjdk-6
pkgdir=$base-$version
origtar=${base}_${version}.orig.tar.gz
tarballdir=6b40

icedtea_checkout=icedtea6-1.13.12
debian_checkout=openjdk6

if [ -d $pkgdir ]; then
    echo directory $pkgdir already exists
    exit 1
fi

if [ -d $pkgdir.orig ]; then
    echo directory $pkgdir.orig already exists
    exit 1
fi

if ! which automake-1.11 >/dev/null 2>&1; then
    echo should use automake1.11 to generate the autotools files
    exit 1
fi

if [ -f $origtar ]; then
    tar xf $origtar
    if [ -d $pkgdir.orig ]; then
	mv $pkgdir.orig $pkgdir
    fi
    tar -c -f - -C $icedtea_checkout . | tar -x -f - -C $pkgdir
    cp -a $debian_checkout $pkgdir/debian
else
    rm -rf $pkgdir.orig
    mkdir -p $pkgdir.orig
    case "$base" in
      openjdk*)
	cp -p $tarballdir/$tarball $pkgdir.orig/
	cp -p $tarballdir/$cacaotb $pkgdir.orig/
	cp -p $tarballdir/$jamvmtb $pkgdir.orig/
	;;
    esac
    tar -c -f - -C $icedtea_checkout . | tar -x -f - -C $pkgdir.orig
    (
	cd $pkgdir.orig
	sh autogen.sh
	rm -rf autom4te.cache
    )
    cp -a $pkgdir.orig $pkgdir
    rm -rf $pkgdir.orig/.hg
fi

echo "Build debian diff in $pkgdir/"
cp -a $debian_checkout $pkgdir/debian
(
  cd $pkgdir
  ls
  sh autogen.sh
  rm -rf autom4te.cache
)
