#!/bin/bash

check_root(){


STARTPATH=`pwd`
if [[ -d $ROOT_DIR ]] ; then 
	echo "[ WARNING  ] ---- Directory $ROOT_DIR already exist..."         
else
	mkdir   $ROOT_DIR ||  return 1;
	echo "[ SUCCESS ] --- Create Directory $ROOT_DIR "
fi
cd $ROOT_DIR
export AROOT_DIR=`pwd`;

#echo Absolute Path $AROOT_DIR...
mkdir -p $AROOT_DIR/package
mkdir -p $AROOT_DIR/build
mkdir -p $AROOT_DIR/log

output=$AROOT_DIR/log
export BUILD_DIR=$AROOT_DIR/build
export ACE_ROOT=$AROOT_DIR/build/ACE_wrappers/
export ASTCC_ROOT=$AROOT_DIR/build/astCC1_3/
export LOADER_ROOT=$ASTCC_ROOT/Loader
export LOADER=$LOADER_ROOT
export HOC_ROOT=$ASTCC_ROOT/ADHOC
export LD_LIBRARY_PATH=$LD_LIBRAY_PATH:$ACE_ROOT/ace
export PATH=$JAVA_HOME/bin/:$BUILD_DIR/swig/bin:$BUILD_DIR/apache-ant-1.5.4/bin:$PATH
export SWIGHOME=$BUILD_DIR/swig
export HOCHOME=$BUILD_DIR/ADHOC
export APACHE_ANT_ROOT=$BUILD_DIR/apache-ant-1.5.4
export QTDIR=$BUILD_DIR/qt
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$ACE_ROOT/ace/:$QTDIR/lib
export XERCESCROOT=$BUILD_DIR/xerces-c-src_2_6_0/
export XP_HOME=$ASTCC_ROOT/xp/
export XASSIST_HOME=$ASTCC_ROOT/xassist2/
export STREAMLIB_ROOT=$ASTCC_ROOT/streamlib3


}


download(){
	cd $AROOT_DIR/package;
	#if wget  wfdsaww.google.com ; then
	#        echo "Impossible to connect to the network..."
        #	echo
	#	echo "You must download packages manually in $AROOT_DIR/package/"
         #        for i in  ${link[*]};  do
          #             echo  $i;
           #      done;
	#	echo
	#	echo "Then you can execute  : `basename $0` -a 5.4   $AROOT_DIR/"
	#	echo
#	fi



	echo "Download in $AROOT_DIR/package/." 
	echo
	if  [[ $l_opt == 1 ]]; then WGET=false ; else WGET=wget; fi
	if  $WGET --help &>/dev/null  ; then 
	 	for i in  ${link[*]};  do 
			echo "Download package :      $i " 
			if [[ $v_opt == 0 ]] ; then   wget -q -c $i  ;fi
			if [[ $v_opt == 1 ]] ; then   wget  -c $i ; fi
		done;
	else
		 echo
                 echo "wget not found ... You must download packages manually in $AROOT_DIR/package/"
		 for i in  ${link[*]};  do
                       echo  $i;
        	 done;
		 exit -1;

	fi
	cd -
}


build_XERCES(){

output_xerces=$output/build_XERCES
rm -f $output_xerces;

if [[ $v_opt == 1 ]] ; then output_xerces=/dev/fd/0 ;fi
if [[ $f_xerces == 1 ]] ; then rm -fr $XERCESCROOT ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR


if !(test -e  $XERCESCROOT/ &>/dev/null ) ; then
        tar zxvf ../package/xerces-c-src_2_6_0.tar.gz &> $output_xerces
        cd $XERCESCROOT/src/xercesc
	autoconf 2>>$output_xerces  >>$output_xerces || return 1;
        ./runConfigure -plinux -cgcc -xg++ -minmem -nsocket -tnative -rpthread 2>> $output_xerces >> $output_xerces
        (make 2>>$output_xerces  >>$output_xerces   && echo "[ SUCCESS  ] --- Xerces 2.6  installed in $XERCESCROOT" )|| return 1;
else
        echo "[ SUCCESS  (CACHED)] --- Xerces 2.6  installed in $XERCESCROOT";
fi


cd $AROOT_DIR;

}

build_ACE(){



output_ace=$output/build_ACE
rm -f $output_ace;

if [[ $v_opt == 1 ]] ; then output_ace=/dev/fd/0 ;fi
if [[ $f_ace == 1 ]] ; then rm -fr $ACE_ROOT ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

case $a_arg in
    5.4 )  
	
	test -e $AROOT_DIR/package/ACE-5.4.4.tar.gz  &> $output_ace || return  1 ;
	test -e $BUILD_DIR/ACE_wrappers/ACE-INSTALL  || tar zxvf  ../package/ACE-5.4.4.tar.gz &> $output_ace
	;;
    5.3 ) 
	 test -e  $AROOT_DIR/package/ACE-5.3+TAO-1.3.tar.gz  &> $output_ace || return  1 ;
	  if !(test -e $BUILD_DIR/ACE_wrappers/ACE-INSTALL &>/dev/null) ; then
                  tar zvxf ../package/ACE-5.3+TAO-1.3.tar.gz >>$output_ace
                  sed -e "s/\/\/ \#define ACE_ENABLE_SWAP_ON_WRITE/\#define ACE_ENABLE_SWAP_ON_WRITE/g" $ACE_ROOT/ace/OS.h > $ACE_ROOT/ace/OS.h.tmp
                  cp -f  $ACE_ROOT/ace/OS.h.tmp $ACE_ROOT/ace/OS.h;
        fi
	;;
    * ) echo "Invalid option : You must specify 5.3 or 5.4 " ; return 1 ;;
  esac
cd $ACE_ROOT
if !(test -e  $BUILD_DIR/ACE_wrappers/ace/config.h &>/dev/null ) ; then
#	ln -sf $BUILD_DIR/ACE_wrappers/ace/config-macosx.h $BUILD_DIR/ACE_wrappers/ace/config.h
#	ln -sf $BUILD_DIR/ACE_wrappers/include/makeinclude/platform_macosx.GNU  $BUILD_DIR/ACE_wrappers/include/makeinclude/platform_macros.GNU


	ln -sf $BUILD_DIR/ACE_wrappers/ace/config-linux.h $BUILD_DIR/ACE_wrappers/ace/config.h
	ln -sf $BUILD_DIR/ACE_wrappers/include/makeinclude/platform_linux.GNU  $BUILD_DIR/ACE_wrappers/include/makeinclude/platform_macros.GNU
	cd ace
	(make 2>>$output_ace >> $output_ace && echo "[ SUCCESS  ] --- ACE $a_arg installed in $ACE_ROOT" ) || return 1;
else
	echo "[ SUCCESS  (CACHED)] --- ACE $a_arg installed in $ACE_ROOT"
fi

cd $AROOT_DIR

}
##LIBXML2

build_libxml2(){

output_xml2=$output/build_xml2

rm -f $output_xml2;

if [[ $v_opt == 1 ]] ; then output_xml2=/dev/fd/0 ;fi
if [[ $f_xml2 == 1 ]] ; then rm -fr $AROOT_DIR/build/libxml2 ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR


if !(test -e   $BUILD_DIR/libxml2/ &>/dev/null ) ; then 
	tar jxvf ../package/libxml2-2.6.17.tar.bz2 &>$output_xml2
	cd $BUILD_DIR/libxml2-2.6.17
	./configure --prefix=$BUILD_DIR/libxml2 2>> $output_xml2 >> $output_xml2
	(make 2>>$output_xml2  >>$output_xml2  )|| return 1;
	(make install 2>> $output_xml2 >>$output_xml2 && echo "[ SUCCESS  ] --- Libxml 2.6  installed in $AROOT/build/libxml2" )|| return 1;
	rm -fr $BUILD_DIR/libxml2-2.6.17;
else
	echo "[ SUCCESS  (CACHED)] --- Libxml 2.6  installed in $AROOT/build/libxml2";
fi


cd $AROOT_DIR;

}

##SWIG
build_swig(){

output_swig=$output/build_swig


rm -f $output_swig;

if [[ $v_opt == 1 ]] ; then output_swig=/dev/fd/0 ;fi
if [[ $f_swig == 1 ]] ; then  rm -fr $AROOT_DIR/build/swig ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

if !(test -e   $BUILD_DIR/swig/bin/swig &>/dev/null ) ; then
	test -e  $BUILD_DIR/SWIG-1.3.24/INSTALL &>/dev/null ||  tar zvxf ../package/swig-1.3.24.tar.gz >>$output_swig
	cd $BUILD_DIR/SWIG-1.3.24
	./configure --prefix=$BUILD_DIR/swig  2>>$output_swig >>$output_swig;
	make 2>>$output_swig>>$output_swig  || return 1;
	(make install 2>>$output_swig >>$output_swig && echo "[ SUCCESS  ] --- SWIG  installed  in $BUILD_DIR/swig")|| return 1;
	rm -fr $BUILD_DIR/SWIG-1.3.24
	cd $AROOT_DIR
else
	 echo "[ SUCCESS  (CACHED)] --- SWIG  installed  in $BUILD_DIR/swig";
fi
}
##PBIO
build_pbio(){


output_pbio=$output/build_pbio
test -e  $AROOT_DIR/package/pbio.tgz  &> $output_pbio || return  1 ;
rm -f $output_pbio;
if [[ $v_opt == 1 ]] ; then output_pbio=/dev/fd/0 ;fi
if [[ $f_pbio == 1 ]] ; then  rm -fr $AROOT_DIR/build/pbio_root ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR
if !(test -e  $BUILD_DIR/pbio_root/include/io.h &>/dev/null) ; then
	test -e  $BUILD_DIR/pbio/include/io.h  &>/dev/null ||  tar zvxf ../package/pbio.tgz >> $output_pbio ;
	cd $BUILD_DIR/pbio
		./configure --prefix=$BUILD_DIR/pbio_root 2>>$output_pbio  >>$output_pbio
		(make 2>>$output_pbio >>$output_pbio) || return 1;
	(make install 2>>$output_pbio>>$output_pbio && echo "[ SUCCESS  ] --- PBIO  installed in $BUILD_DIR/pbio_root" ) || return 1;
	rm -fr $BUILD_DIR/pbio;
else
	echo "[ SUCCESS  (CACHED)] --- PBIO  installed in $BUILD_DIR/pbio_root"
fi

cd $AROOT_DIR

}
#build_ADHOC(){}

###STREAMLIB3
build_streamlib3(){

output_streamlib=$output/build_stream

test -e $AROOT_DIR/package/streamlib3.tgz  &> $output_streamlib || return  1 ;

rm -f $output_streamlib;
if [[ $v_opt == 1 ]] ; then output_streamlib=/dev/fd/0 ;fi
if [[ $f_stream == 1 ]] ; then   rm -fr $AROOT_DIR/build/streamlib3 ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR
if !(test -e  $BUILD_DIR/streamlib3/autogen.sh &>/dev/null) ; then
	 tar zvxf ../package/streamlib3.tgz >> $output_streamlib;
	 cd $BUILD_DIR/streamlib3
	##./autogen.sh &> $output/autogen_streamlib3
	#./configure LDFLAGS="-L$BUILD_DIR/pbio_root/lib " CPPFLAGS="-I $BUILD_DIR/pbio_root/include/" 2>> $output_streamlib>> $output_streamlib
	./configure  2>> $output_streamlib>> $output_streamlib
	(make 2>> $output_streamlib >> $output_streamlib && echo "[ SUCCESS  ] --- Streamlib3  installed  in $BUILD_DIR/stremlib3" )|| return 1;
	cd $BUILD_DIR/ADHOC &>$output_hoc;
        (make 2>>$output_hoc >>$output_hoc && echo "[ SUCCESS  ] --- HOC  installed in $BUILD_DIR/ADHOC" )|| return 1;
        cd $BUILD_DIR;

else
	echo "[ SUCCESS  (CACHED)] --- Streamlib3  installed  in $BUILD_DIR/stremlib3"
fi



}

build_astCC(){

output_astcc=$output/build_astcc

rm -f $output_astcc;

if [[ $v_opt == 1 ]] ; then output_astcc=/dev/fd/0 ;fi
if [[ $f_astCC == 1 ]] ; then  rm -fr $AROOT_DIR/build/astCC1_3 ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR


test -e  $BUILD_DIR/astCC1_3/INSTALL &>/dev/null ||  tar zxvf ../package/astCC1_3.tgz >>$output_astcc
if !(test -e  $BUILD_DIR/astCC1_3/assist_arch &>/dev/null ) ; then
	cd $BUILD_DIR/astCC1_3/
	ln -sf $BUILD_DIR/streamlib3 .
	ln -sf $BUILD_DIR/ADHOC .
	ln -sf $BUILD_DIR/xp .
	ln -sf $BUILD_DIR/CompComp . 
	ln -sf $BUILD_DIR/xassist2 . 
	echo  'SYSINCLUDE   = $(ASTCC_ROOT)/../libxml2/include/libxml2' >> $ASTCC_ROOT/directory.mk
	echo  'SYSLIB   = $(ASTCC_ROOT)/../libxml2/lib' >> $ASTCC_ROOT/directory.mk
        which ant >> $output_astcc
	which java >> $output_astcc
 	(make 2>>$output_astcc  >>$output_astcc && echo "[ SUCCESS ] --- Assit 1.3 installed in $ASTCC_ROOT" )|| return 1;
else
	echo	"[ SUCCESS  (CACHED)] --- Assit 1.3 installed in $ASTCC_ROOT";
fi


}


###ADHOC
build_adhoc(){
output_hoc=$output/build_hoc

rm -f $output_hoc;

if [[ $v_opt == 1 ]] ; then output_hoc=/dev/fd/0 ;fi
if [[ $f_hoc == 1 ]] ; then  rm -fr $AROOT_DIR/build/ADHOC ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

test -e  $BUILD_DIR/astCC1_3/INSTALL &>/dev/null ||  tar zvxf ../package/astCC1_3.tgz >> $output_hoc;


echo  'SYSINCLUDE   = ${ASTCC_ROOT}/../libxml2/include/libxml2' >> $ASTCC_ROOT/directory.mk
echo  'SYSLIB   = ${ASTCC_ROOT}/../libxml2/lib' >> $ASTCC_ROOT/directory.mk

cd   $ASTCC_ROOT 
ln -sf $BUILD_DIR/streamlib3 .
ln -sf $BUILD_DIR/ADHOC .
ln -sf $BUILD_DIR/xp .
ln -sf $BUILD_DIR/CompComp .

cd $BUILD_DIR/astCC1_3/Loader/ClientLoaderC/
make 2>>$output_hoc >> $output_hoc || return 1;
cd $BUILD_DIR
if !(test -e   $BUILD_DIR/ADHOC/configure &>/dev/null ) ; then
	test -e  $BUILD_DIR/ADHOC/configure &>/dev/null || tar zvxf ../package/ADHOC.tgz >> $output_hoc;
	cd $BUILD_DIR/ADHOC/
	./configure --enable-assist-setup --with-ext-execute-object=$BUILD_DIR/astCC1_3/streamlib3/libsrc/Execute.o  LDFLAGS="-L$BUILD_DIR/libxml2/lib " CPPFLAGS="-I $BUILD_DIR/libxml2/include/libxml2" 2>>$output_hoc >>$output_hoc
	cd hoc
        (make libhocutil.a 2>>$output_hoc >>$output_hoc )|| return 1;

#	cd -
#	(make 2>>$output_hoc >>$output_hoc && echo "[ SUCCESS  ] --- HOC  installed in $BUILD_DIR/ADHOC" )|| return 1;
#	cd $BUILD_DIR;
else
	echo "[ SUCCESS  (CACHED)] --- HOC  installed in $BUILD_DIR/ADHOC" ;
fi





}
##QT
build_qt(){
output_qt=$output/build_qt

output_qt_n=$output_qt
rm -f $output_qt;

if [[ $v_opt == 1 ]] ; then output_qt=/dev/fd/0 ;output_qt_n=/dev/null ;fi
if [[ $f_qt == 1 ]] ; then  rm -fr $AROOT_DIR/build/qt;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

if !(test -e  $BUILD_DIR/qt/configure &>/dev/null);then
	tar zxf ../package/qt-x11-free-3.2.2.tar.gz >>$output_qt
        mv   $BUILD_DIR/qt-x11-free-3.2.2 $BUILD_DIR/qt
        cd $BUILD_DIR/qt
        echo yes | ./configure  -thread -fast 2>> $output_qt_n  >> $output_qt_n
        (make  sub-tools 2>>$output_qt >>$output_qt && echo " [ SUCCESS ] --- QT 3.2.2 installed in $BUILD_DIR/qt" )|| return 1;
else
  echo "[ SUCCESS  (CACHED)] --- QT 3.2.2 installed in $BUILD_DIR/qt"
fi
cd $AROOT_DIR

}

build_xassist(){
output_xassist=$output/build_xassist

rm -f $output_xassist;

if [[ $v_opt == 1 ]] ; then output_xassist=/dev/fd/0 ;fi
if [[ $f_xassist == 1 ]] ; then  rm -fr $AROOT_DIR/build/xassist2 ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

if !(test -e  $BUILD_DIR/xassist2/Makefile  &>/dev/null);then
	 tar zvxf ../package/xassist2.tgz >> $output_xassist
        cd $BUILD_DIR/xassist2/
	cd astrc_xml
	(make -f Makefile_support 2>> $output_xassist >>$output_xassist  )|| return 1;
        cd - 
        (make 2>> $output_xassist >>$output_xassist && echo "[ SUCCESS  ] --- Xassist installed in $BUILD_DIR/xassist2" )|| return 1;
else
	echo "[ SUCCESS  (CACHED)] --- Xassist installed in $BUILD_DIR/xassist2";
fi
cd $AROOT_DIR

}


build_xp(){
output_xp=$output/build_xp

rm -f $output_xp;

if [[ $v_opt == 1 ]] ; then output_xp=/dev/fd/0 ;fi
if [[ $f_xp == 1 ]] ; then  rm -fr $AROOT_DIR/build/xp ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

if !(test -e  $BUILD_DIR/xp/GNUmakefile  &>/dev/null);then
	 tar zvxf ../package/xp.tgz >> $output_xp
        cd $BUILD_DIR/xp/
        (make 2>> $output_xp >>$output_xp )|| return 1;
	(g++ -g    -c -o pxml.o pxml.C -I. -I $BUILD_DIR/libxml2/include/libxml2/  -L $BUILD_DIR/libxml2/lib  2>> $output_xp >>$output_xp && echo "[ SUCCESS  ] --- Xp installed in $BUILD_DIR/xp" )|| return 1;

else
	echo "[ SUCCESS  (CACHED)] --- Xp installed in $BUILD_DIR/xp";
fi
cd $AROOT_DIR

}




build_Comp(){
output_Comp=$output/build_Comp

rm -f $output_Comp;

if [[ $v_opt == 1 ]] ; then output_Comp=/dev/fd/0 ;fi
if [[ $f_Comp == 1 ]] ; then  rm -fr $AROOT_DIR/build/CompComp ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR
if !(test -e   $BUILD_DIR/xassist2/Makefile  &>/dev/null);then
        tar zvxf ../package/xassist2.tgz >> $output_Comp
        cd $BUILD_DIR/xassist2/astrc_xml
        (make -f Makefile_support 2>> $output_Comp >>$output_Comp  )|| return 1;
        cd -

fi



if !(test -e  $BUILD_DIR/CompComp/Makefile  &>/dev/null);then
	 tar zvxf ../package/CompComp.tgz >> $output_Comp
        cd $BUILD_DIR/CompComp/
        (./configure --with-libxml2=$BUILD_DIR/libxml2/ --with-xerces=$XERCESCROOT/  2>> $output_Comp >>$output_Comp )|| return 1;
        (make 2>> $output_Comp >>$output_Comp && echo "[ SUCCESS  ] --- CompComp installed in $BUILD_DIR/CompComp" )|| return 1;
	cd $BUILD_DIR
	tar zvxf ../package/Framework.tgz >>  $output_Comp
        cd $BUILD_DIR/Framework
         (make 2>> $output_Comp >>$output_Comp && echo "[ SUCCESS  ] --- Framework installed in $BUILD_DIR/Framework" )|| return 1;       
        cd $BUILD_DIR
else
	echo "[ SUCCESS  (CACHED)] --- CompComp installed in $BUILD_DIR/CompComp";
fi
cd $AROOT_DIR

}




###ANT
build_ant(){

output_ant=$output/build_ant

rm -f $output_ant;

if [[ $v_opt == 1 ]] ; then output_ant=/dev/fd/0 ;fi
if [[ $f_ant == 1 ]] ; then  rm -fr $AROOT_DIR/build/apache-ant-1.5.4 ;fi

BUILD_DIR=$AROOT_DIR/build
cd $BUILD_DIR

if !(test -e  $BUILD_DIR/apache-ant-1.5.4/bin/ant &>/dev/null);then
	 unzip ../package/apache-ant-1.5.4-bin.zip >>$output_ant
	echo  "[ SUCCESS  ] --- Ant installed in $BUILD_DIR/apache-ant-1.5.4";
else
	echo  "[ SUCCESS  (CACHED)] --- Ant installed in $BUILD_DIR/apache-ant-1.5.4";
fi

cd $AROOT_DIR

}

NO_ARGS=0 
E_OPTERROR=65



if [ !  -n "$JAVA_HOME" ];then
        echo "You must set JAVA_HOME variable..."
	echo
        exit -1
fi

echo  "[ SUCCESS  ] --- Check JAVA_HOME ..."

if [ $# -eq "$NO_ARGS" ]  
then
  echo "Usage: `basename $0` -a <ACE_Version> [-xdf]  <ROOT_DIR> "
  echo
  echo " -a <ACE_Version>   Specify ACE_wrappers  version (You can choose 5.4 or 5.3). "
  echo " -x                 Enabled xassist compilation    "
  echo " -d                 Download ALL Packages need to compile Assist in  <ROOT_DIR>/packages. You can download the package manually"
  echo "                    and copy in <ROOT_DIR>/packages. To see the list of packages use -l option."
  echo " -l                 Display the list  of package of Assist Environment."
  echo " -f <Package>       Force to recompile  \"Package\". List Packages: ACE,libxml2,qt,swig,streamlib3,astCC,ant,adhoc,xassist,xerces" 
  echo " -v                 Verbose output (default output is written in <ROOT_DIR>/log)"
  echo " -c                 Compile CompComp & xp modules"
  echo " "
  echo "                     "
  echo " Example : "
  echo ""
  echo "Download all  packages that you need to compile assist in /usr/loca/Ast_ENV/packages"
  echo  "`basename $0` -d /usr/loca/Ast_ENV  " 
  echo
  echo "To compile Assist with ace 5.4 in /usr/loca/Ast_ENV/build"
  echo   "`basename $0` -a 5.4 /usr/loca/Ast_ENV  "
  echo ""
  echo "To compile  Assist with ace 5.3 and xassist "
  echo   "`basename $0` -x -a 5.3 /usr/loca/Ast_ENV   "
  echo
  echo "If you want to force Assist and streamlib3 recompilation (first you must update package in <ROOT_DIR>/packages)"
  echo   "`basename $0` -a 5.4 -f astCC -f streamlib3 /usr/loca/Ast_ENV   "
  echo
  echo "If you want to compile Assist Component "
  echo "`basename $0` -c  -a 5.4  /usr/loca/Ast_ENV   "
     
echo
  exit $E_OPTERROR        
fi  

declare -a link=(  ftp://ftp.trolltech.com/qt/source/qt-x11-free-3.2.2.tar.gz
       		   http://archive.apache.org/dist/ant/binaries/apache-ant-1.5.4-bin.zip 
                   http://ovh.dl.sourceforge.net/sourceforge/swig/swig-1.3.24.tar.gz 
                   http://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.17.tar.bz2
                   http://deuce.doc.wustl.edu/old_distribution/ACE-5.3+TAO-1.3.tar.gz
                   http://deuce.doc.wustl.edu/old_distribution/ACE-5.4.4.tar.gz
                   http://www.di.unipi.it/groups/architetture/package/astCC1_3.tgz
                   http://www.di.unipi.it/groups/architetture/package/streamlib3.tgz
                   http://www.di.unipi.it/~vitale/xerces-c-src_2_6_0.tar.gz
                   http://www.di.unipi.it/groups/architetture/package/CompComp.tgz
                   http://www.di.unipi.it/groups/architetture/package/xp.tgz
                   http://www.di.unipi.it/groups/architetture/package/ADHOC.tgz
                   http://www.di.unipi.it/groups/architetture/package/xassist2.tgz
                   http://www.di.unipi.it/groups/architetture/package/Framework.tgz
                        )
	

d_opt=0
x_opt=0
a_opt=0
f_opt=0
v_opt=0
c_opt=0
f_ace=0
f_xml2=0
f_ant=0
f_qt=0
f_astCC=0
f_hoc=0
f_pbio=0
f_swig=0
f_stream=0
f_xassist=0
f_Comp=0
f_xp=0
f_xerces=0
declare -a f_arg[20];


f_i=0;
while getopts "a:xvlcdf:" Option
do
  case $Option in
    a ) a_opt=1;a_arg=$OPTARG  ;;
    x ) x_opt=1 ;;
    d ) d_opt=1 ;;
    v ) v_opt=1 ;;
    c ) c_opt=1 ;;  
    l ) l_opt=1 ;;  
    f ) f_arg[f_i]=$OPTARG ; ((f_i++)) ;;
    * ) echo "Invalid option : $Option.";;  
  esac
done

shift $(($OPTIND - 1))
ROOT_DIR=$1;

if [[ $l_opt == 1 ]];  then
  for i in  ${link[*]};  do
                     echo "Download package :      $i "
  done;
  exit -1;
fi
if [ -z $ROOT_DIR  ] ;then 	
	echo
	echo "You must specify <ROOT_DIR>"
	echo
	exit $E_OPTERROR; 
fi;


for i in ${f_arg[*]}; do
	if [[ "$i" == "ACE" ]] ;      then f_ace=1    ;fi
	if [[ "$i" == "libxml2" ]] ;  then f_xml2=1   ;fi
	if [[ "$i" == "qt" ]] ;       then f_qt=1     ;fi 
	if [[ "$i" == "swig" ]] ;     then f_swig=1   ;fi
	if [[ "$i" == "streamlib3" ]];then f_stream=1 ;fi
	if [[ "$i" == "astCC" ]] ;    then f_astCC=1  ;fi
	if [[ "$i" == "ant" ]] ;      then f_ant=1    ;fi
	if [[ "$i" == "pbio" ]] ;     then f_pbio=1   ;fi
	if [[ "$i" == "adhoc" ]] ;    then f_hoc=1    ;fi
	if [[ "$i" == "xp" ]] ;       then f_xp=1;     fi
	if [[ "$i" == "Comp" ]] ;     then f_Comp=1;   fi
	if [[ "$i" == "xassist" ]] ;  then f_xassist=1;fi
	if [[ "$i" == "xerces" ]] ;   then f_xerces=1; fi
done



	check_root ;
tmp=$?;

if [ "$tmp" -eq "1" ] ; then echo Impossible to create $ROOT_DIR; exit -1; fi

if [[ $d_opt == 1 || $l_opt == 1 ]]; then 
	download ;
	tmp=$?; 
	if [ "$tmp" -eq "1" ] ; then 
		echo "Download failed(try -v option to see the problem) ";
		echo "Es: `basename $0` -v -d <ROOT_DIR>"
		exit -1;
	fi;
fi

if [[ $a_opt == 1 ]]; then build_ACE ; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- ACE installation failed(see log in $output_ace)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_libxml2; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- Libxml2  installation failed(see log in $output_xml2)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_ant; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- ANT  installation failed(see log in $output_ant)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_swig tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- SWIG  installation failed(see log in $output_swig)"; exit -1; fi ;fi
##if [[ $a_opt == 1 ]]; then build_pbio; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- PBIO  installation failed(see log in $output_pbio)"; exit -1; fi ;fi
if [[ $c_opt == 1 ]]; then build_XERCES; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- XERCES  installation failed(see log in $output_xerces)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_adhoc; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- ADHOC  installation failed(see log in $output_adhoc)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_streamlib3; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- StreamLib3  installation failed(see log in $output_stream)"; exit -1; fi ;fi
if [[ $a_opt == 1 ]]; then build_astCC; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- AstCC 1.3 installation failed(see log in $output_astcc)"; exit -1; fi ;fi
if [[ $x_opt == 1 ]]; then build_qt; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- QT installation failed(see log in $output_qt)"; exit -1; fi ;fi
if [[ $x_opt == 1 ]]; then build_xassist; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- Xassist installation failed(see log in $output_xassist)"; exit -1; fi ;fi
if [[ $c_opt == 1 ]]; then build_xp; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- Xp installation failed(see log in $output_xp)"; exit -1; fi ;fi
if [[ $c_opt == 1 ]]; then build_Comp; tmp=$?; if [ "$tmp" -eq "1" ] ; then echo "[ ERROR  ] --- CompComp  installation failed(see log in $output_Comp)"; exit -1; fi ;fi


echo
echo 
echo If an error occurs during the installation send the $output dir to :    vitale@di.unipi.it
echo
echo
echo You must set these environment  variables to use Assist:
echo
echo export ASTCC_ROOT=$ASTCC_ROOT
echo export LOADER=\$ASTCC_ROOT/Loader
echo export HOC_ROOT=\$ASTCC_ROOT/ADHOC
echo export ACE_ROOT=\$ASTCC_ROOT/../ACE_wrappers


echo export LOADER_ROOT=\$ASTCC_ROOT/Loader
echo export LOADER=$LOADER_ROOT
echo export HOC_ROOT=$ASTCC_ROOT/ADHOC
echo export SWIGHOME=$BUILD_DIR/swig
echo export HOCHOME=$BUILD_DIR/ADHOC
echo export APACHE_ANT_ROOT=$BUILD_DIR/apache-ant-1.5.4
echo export QTDIR=$BUILD_DIR/qt
echo export XERCESCROOT=$BUILD_DIR/xerces-c-src_2_6_0/
echo export XP_HOME=\$ASTCC_ROOT/xp/
echo export XASSIST_HOME=\$ASTCC_ROOT/xassist2/



echo export LD_LIBRARY_PATH=\$LD_LIBRAY_PATH:\$ACE_ROOT/ace:\$ASTCC_ROOT/../qt/lib
echo export PATH=\$ASTCC_ROOT/bin/Linux/:\$ASTCC_ROOT/../xassist2:\$PATH

echo "To compile assist program  you must create a $HOME/.ast_rc configuration file using"
echo "these command:"
echo 
echo  "cd \$ASTCC_ROOT/"
echo  "user_conf.sh"
echo
echo 
cd $STARTPATH


exit 0






