#!/bin/sh

if [ ! "$STUDIO_VERSION_MAJOR_MINOR" ]; then
	STUDIO_VERSION_MAJOR_MINOR=18.11
fi

# File in which the status of the installation generation is logged
INSTALL_LOG=`pwd`/install.log
# File in which the status of the installation compilation is logged
COMPILE_LOG=`pwd`/compile.log
# Where the new delivery is set up. Warning: this directory is erased!
INSTALL_DIR=`pwd`/Eiffel_$STUDIO_VERSION_MAJOR_MINOR
# Where the final delivery is set up (local machine). Warning: directory erased too!
FINAL_INSTALL_DIR=`pwd`/Eiffel_$STUDIO_VERSION_MAJOR_MINOR
# Where documentation will be generated
DOCUMENT_DIR=`pwd`
# Where all.tar.bz2 files should be stored
EXPORT_DIR=`pwd`
# What tag to use to CVS export everything
if [ ! "$DEFAULT_ISE_SVN" ]; then
	DEFAULT_ISE_SVN=svn://$LOGNAME@svn.ise/ise_svn/tags/Eiffel_18.11
fi
if [ ! "$DEFAULT_ORIGO_SVN_ROOT" ]; then
	DEFAULT_ORIGO_SVN_ROOT=https://svn.eiffel.com/eiffelstudio
fi
DEFAULT_ORIGO_SVN=$DEFAULT_ORIGO_SVN_ROOT/tags/Eiffel_18.11/R1
# This section is commented out be cause it won't work on non-bash shell
# we use the other one while waiting for a better solution
#if [ ! "$ORIGO_SVN_REVISION" ]; then
#	mkdir -p /tmp/eiffelstudio_revision
#	svn co $DEFAULT_ORIGO_SVN_ROOT/revision /tmp/eiffelstudio_revision	
#	ORIGO_SVN_REVISION=$(svnversion /tmp/eiffelstudio_revision)
#fi
if [ ! "$ORIGO_SVN_REVISION" ]; then
	ORIGO_SVN_REVISION=HEAD
fi

# Used to create the revision on the package.
FILE_SVN_REVISION=102592
# Version of GTK
GTK_DIR=gtk2

# Used only when making PorterPackage to checkout the sources
NEW_EIFFEL_SRC=`pwd`/tmpdev
NEW_ISE_SRC=`pwd`/tmpdev
NEW_ISE_LIBRARY=`pwd`/tmpdev
# The following directory is going to be used to compile the compiler
FINALIZATION_DIR=`pwd`/finalized

echo > $INSTALL_LOG

# OK, this is all for user-defined data. Leave me alone now, I'll do my magic :)

# File management
# Copy all files
copy ()
{
cp -r $*
}
move ()
{
mv $*
}
md ()
{
mkdir -p $*
}
# Compress a file and move it to $EXPORT_DIR
tgz ()
{
pax -w -f $1.tar $1
bzip2 $1.tar
}
# Completely remove a directory
fullrd ()
{
rm -rf $*
}
# Remove a file
fullrf ()
{
rm -f $*
}
# Retrieve a file from the old delivery and put it in the new one
quick_move ()
{
if [ $# -eq 1 ]; then
copy $ISE_EIFFEL/$1 $INSTALL_DIR/$1 >> $INSTALL_LOG
else
copy $1 $ISE_EIFFEL/$2 $INSTALL_DIR/$2 >> $INSTALL_LOG
fi
}
# Create a directory if it doesn't exist already
safe_md ()
{
if [ ! -d $1 ]; then
if [ -f $1 ]; then
fullrf $1
fi
md $1
fi
}
# Save current path into TMP_PATH
save_path ()
{
TMP_PATH=`pwd`
}
 
# Helpers for the script
# Give some info on what's happening, both in the log and on the screen
remtrace ()
{
echo $*
echo -------------------------------- >> $INSTALL_LOG
echo $* >> $INSTALL_LOG
}
# Clean exit
TERMINATE ()
{
echo Exiting...
remtrace final time:
remtrace `date +%c`
PATH=$OLD_PATH
cd $INIT_DIR
}
# Exit with an error
CANCEL()
{
TERMINATE
exit 1
}                                                                                   
 
# SVN Commands: checkout, export
co ()
{
until  svn co $* >> $INSTALL_LOG; do echo "svn co $* failed, retrying..."; done
}
exprt ()
{
until  svn --force export $* >> $INSTALL_LOG; do echo "svn export $* failed, retrying..."; done
}

# C Compilation
default_make ()
{
save_path
$INSTALL_DIR/studio/spec/$ISE_PLATFORM/bin/finish_freezing -library >> $COMPILE_LOG
cd $TMP_PATH
}
all_makes ()
{
save_path
# What should we do??? Compile for all platforms???
cd $TMP_PATH
}
#On MacOS X you need to run `ranlib' each time you move a library.
mac_ranlib ()
{
if [ ! -d $1 ]; then
if echo "$ISE_PLATFORM" | grep "macosx" >/dev/null 2>&1; then ranlib $1; fi
fi
}
# Eiffel Compilation
# Remove a project from the current directory, if any
clean_project ()
{
if [ -d EIFGENs ]; then fullrd EIFGENs; fi
}
# Create a portable compressed archive of the C code. Parameter: name of the compressed archive file
tgz_ccode ()
{
if [ -d EIFGENs/$1/F_code ]; then
cd EIFGENs/$1
pax -w -f F_code.tar F_code >> $INSTALL_LOG
bzip2 F_code.tar
mv F_code.tar.bz2 ../../$2
cd ../..
fi
}
# Unzip the F_code of $1, compile it, clean up
untgz_ccode ()
{
if [ -d F_code ]; then
	fullrd F_code
fi
if [ ! -f $1.tar.bz2 ]; then
	remtrace "Couldnt find $1.tar.bz2"
	CANCEL
fi
bunzip2 -c $1.tar.bz2 | tar -xf -
if [ ! -d F_code ]; then
	remtrace "No F_code for $1"
	CANCEL
fi
cd F_code
$INSTALL_DIR/studio/spec/$ISE_PLATFORM/bin/finish_freezing -silent >> $COMPILE_LOG
if [ ! -f $1 ]; then
	remtrace Couldnt generate $1
	# Do not cancel if requested by providing a 2nd argument.
	if [ -z "$2" ]; then
		CANCEL
	fi
fi
mv $1 ..
cd ..
fullrf $1.tar
fullrd F_code
}
# Finalize at the Eiffel level only
finalize ()
{
$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin/ecb -batch -finalize -config $1 -target $2 >> $INSTALL_LOG 2>&1
if [ ! -f EIFGENs/$2/F_code/Makefile.SH ]; then
echo "Couldn't finalize $1"
CANCEL
fi
}

# Tests....
testpar ()
{
	ls $*
}                            

