#!/bin/bash

# exit with message and error code
function doExit {
    echo "$1"
    exit $2
}

# Test if a executable of name $1 exists
function testForTool {
    which $1 > /dev/null || doExit "Tool $1 is missing. Exit..." 1
}

# download from-url to-file
function download {
    echo "Downloading $1 ..."
    curl "-#" $1 -o $2 || doExit "Error while downloading. Exit..." 1
}

testForTool curl
testForTool tar
testForTool unzip
testForTool sed
testForTool patch

EXE=""
PLATFORM_PORT="dwt-linux"

DMD_PLATFORM="linux"

DSSS_PLATFORM="gnuWlinux-x86"
DSSS_PROFILE="dmd-posix-tango"
DSSS_VERSION="0.75"
DSSS_ARCHIVE_EXT=".tar.gz"
DSSS_ARCHIVE_BASE="dsss-$DSSS_VERSION-dmd-$DSSS_PLATFORM"
DSSS_ARCHIVE="$DSSS_ARCHIVE_BASE$DSSS_ARCHIVE_EXT"
DSSS_DIR="$DSSS_ARCHIVE_BASE"

TANGO_PLATFORM="linux"
TANGO_RELEASE="0.99.6"
TANGO_DMD_RELEASE="1.029"
TANGO_ARCHIVE_EXT=".tar.gz"
TANGO_ARCHIVE_BASE="tango-$TANGO_RELEASE-bin-$TANGO_PLATFORM-dmd.$TANGO_DMD_RELEASE"
TANGO_ARCHIVE="$TANGO_ARCHIVE_BASE$TANGO_ARCHIVE_EXT"
TANGO_DIR="$TANGO_ARCHIVE_BASE"

HG_EXISTS=0

echo "**************************************************"
echo "* This downloads and configures DWT"
echo "* Run this in a new created directory"

if which hg > /dev/null
then
    echo "* "
    echo "* Mercurial was found, so instead of downloading snapshots,"
    echo "* this script will create HG working directories"
    echo "* "
    HG_EXISTS=1
fi

echo "* <Enter> to proceed or cancel with Ctrl-C"
read ok

# create the archives directory, no error if already exists
mkdir -p archives

# *** DSSS ***
if [ ! -f archives/$DSSS_ARCHIVE ]
then
    echo "* download DSSS"
    download http://svn.dsource.org/projects/dsss/downloads/$DSSS_VERSION/$DSSS_ARCHIVE archives/$DSSS_ARCHIVE
fi

echo "* removing old DSSS version, extract the archive, rename it to simple name"
rm -rf dsss
tar xzf archives/$DSSS_ARCHIVE
mv $DSSS_DIR dsss

# *** Tango ***
if [ ! -f archives/$TANGO_ARCHIVE ]
then
    echo "* download Tango"
    download http://downloads.dsource.org/projects/tango/$TANGO_RELEASE/$TANGO_ARCHIVE archives/$TANGO_ARCHIVE
fi

echo "* remove old tango version, extract the archive, rename it to simple name"
rm -rf tango
tar xzf archives/$TANGO_ARCHIVE
mv $TANGO_DIR tango

# *** Jive ****
if [ ! -f archives/jive.zip ]
then
    echo "* download Jive"
    download http://downloads.dsource.org/projects/dwt/jive.zip archives/jive.zip
fi

echo "* remove old Jive version, extract the archive"
rm -rf jive
unzip -q archives/jive.zip

# Fixes and workarounds
DMD_FIXUP_VERSION="1.028"
echo "* Fix: Replace the DMD binary with $DMD_FIXUP_VERSION because of bug #2067"
if [ ! -f ./archives/dmd.$DMD_FIXUP_VERSION.zip ]
then
    download http://ftp.digitalmars.com/dmd.$DMD_FIXUP_VERSION.zip archives/dmd.$DMD_FIXUP_VERSION.zip
fi

# extract compiler executable, copy it into the tango/bin folder and remove the extraction leftover dirs
unzip -q archives/dmd.$DMD_FIXUP_VERSION.zip dmd/bin/dmd$EXE
cp dmd/bin/dmd$EXE tango/bin/dmd$EXE
chmod +x tango/bin/dmd$EXE
rm -rf dmd

echo "* Configure: make dsss use the tango profile"
echo "profile=$DSSS_PROFILE" > dsss/etc/rebuild/default

echo "* Configure: make dsss use oneatatime=off"
sed -i s/oneatatime=yes/oneatatime=no/ dsss/etc/rebuild/$DSSS_PROFILE

echo "* Configure: create a dsss starter which prepends dsss/bin and tango/bin to PATH"
DSSS_START=dsss.sh
echo "#!/bin/sh" > $DSSS_START
echo "PATH=`pwd`/tango/bin:`pwd`/dsss/bin:\$PATH `pwd`/dsss/bin/dsss \$* -full -version=Jive -I`pwd`/jive" >> $DSSS_START
chmod +x $DSSS_START

# download the dwt archives
function doDwtRepos {
    if [ -d $1 ]
    then
        echo "* removing old version of $1"
        rm -rf ./$1
    fi
    if [ $HG_EXISTS -eq 0 ]
    then
        echo "* download snapshot and extract $1"
        mkdir -p ./$1
        rm -f archives/$1.tar.gz
        download http://hg.dsource.org/projects/$1/archive/tip.tar.gz archives/$1.tar.gz
        tar xz -C $1 --strip 1 --file archives/$1.tar.gz
        rm -f archive/$1.tar.gz
    else
        echo -n "* clone Mercurial repository for $1 ..."
        hg clone -q http://hg.dsource.org/projects/$1 || doExit "Error while cloning $1" 1
        echo " OK"
    fi
}

doDwtRepos $PLATFORM_PORT
doDwtRepos dwt-samples
doDwtRepos dwt-addons

echo "* Fix: patch the tango collections to work around the dmd 1.028 bug #1978"
echo "*  (See also http://www.dsource.org/projects/dwt/wiki/JFace)"

patch -s -p1 << PATCHEND
diff -Naur old/tango/import/tango/util/collection/model/Seq.d new/tango/import/tango/util/collection/model/Seq.d
--- old/tango/import/tango/util/collection/model/Seq.d  2008-05-12 14:35:11.000000000 +0200
+++ new/tango/import/tango/util/collection/model/Seq.d  2008-05-12 14:37:25.000000000 +0200
@@ -32,6 +32,8 @@

 public interface Seq(T) : SeqView!(T), Dispenser!(T)
 {
+        public Seq!(T) duplicate();
+        public alias duplicate dup;
         /**
          * Insert all elements of enumeration e at a given index, preserving
          * their order. The index can range from
diff -Naur old/tango/import/tango/util/collection/model/Set.d new/tango/import/tango/util/collection/model/Set.d
--- old/tango/import/tango/util/collection/model/Set.d  2008-05-12 14:35:11.000000000 +0200
+++ new/tango/import/tango/util/collection/model/Set.d  2008-05-12 14:36:57.000000000 +0200
@@ -34,6 +34,8 @@

 public interface Set(T) : SetView!(T), Dispenser!(T)
 {
+        public Set!(T) duplicate();
+        public alias duplicate dup;
         /**
          * Include the indicated element in the collection.
          * No effect if the element is already present.
diff -Naur old/tango/import/tango/util/collection/model/View.d new/tango/import/tango/util/collection/model/View.d
--- old/tango/import/tango/util/collection/model/View.d 2008-05-12 14:35:11.000000000 +0200
+++ new/tango/import/tango/util/collection/model/View.d 2008-05-12 14:36:14.000000000 +0200
@@ -36,7 +36,7 @@
          * All Views implement duplicate
         **/

-        public Dispenser!(T) duplicate ();
+        public View!(T) duplicate ();
         public alias duplicate dup;

         /**
PATCHEND

echo "* Building+installing $PLATFORM_PORT"
cd $PLATFORM_PORT
../dsss.sh build >/dev/null || doExit "Compile failed" 1
../dsss.sh install >/dev/null || doExit "DSSS $PLATFORM_PORT install failed" 1
cd ..

echo "* "
echo "* "
echo "**************************************************"
echo "* The DWT Setup finished successfully"
echo "* now goto dwt-samples directory and run:"
echo "*   ../dsss.sh build"
echo "* to build the JFace examples goto dwt-samples/jface and start the build with  "
echo "*   ../../dsss.sh build -I../../dwt-addons -J../../dwt-addons/res"
echo "*   "
echo "**************************************************"

