#!/bin/sh
#
# Copyright (C) 1999, 2000, 2001, 2002, 2003 Czo <Olivier.Sirol@lip6.fr>
# License: GPL (http://www.gnu.org/copyleft/gpl.html)
#
# $Id: mkdistrib,v 1.17 2004/09/27 16:36:57 alliance Exp $
#
# /------------------------------------------------------------------\
# |                                                                  |
# |        A l l i a n c e   C A D   S y s t e m                     |
# |   W e b   D i s t r i b u t i o n   B u i l d e r                |
# |                                                                  |
# |  Author    :                         Olivier Sirol               |
# |  E-mail    :           alliance-users@asim.lip6.fr               |
# | ================================================================ |
# |  sh script :         "./mkdistrib"                               |
# | **************************************************************** |
# |  U p d a t e s                                                   |
# |                                                                  |
# |  16 Jun 2004 :                                                   |
# |    Major update by Jean-Paul Chaput.                             |
# |                                                                  |
# \------------------------------------------------------------------/
#


#set -x
#set -v




# --------------------------------------------------------------------
# Internal variables.


      DISTRIBDIR=/users/outil/alliance/distrib
   FTPDISTRIBDIR=/users/largo2/ftp/pub/alliance/distribution/5.0
         CVSROOT=/users/outil/alliance/cvsroot

         Solaris=sparc-solaris-2.9
           Linux=i386-linux-2.6.6

   MKDISTRIB_PID="$$"
#  MKDISTRIB_PID="26882"

         TARGETS="tsunami funk"
#        TARGETS="funk"
   TARGETLOGFILE=/tmp/alliance-build.$MKDISTRIB_PID
         LOGFILE="$DISTRIBDIR/mkdistrib-run.log"

         RELEASE=`date +%Y%m%d`
#        RELEASE=20040715
         VERSION=5.0

            NAME="alliance-$VERSION"
            PACK="$NAME-$RELEASE"

          TMPDIR=tmp-$MKDISTRIB_PID
     TMPBUILDDIR=build-$MKDISTRIB_PID
   TMPINSTALLDIR=install-$MKDISTRIB_PID
     TMPALLIANCE=alliance-$MKDISTRIB_PID
      TMPDESTDIR=dest-$MKDISTRIB_PID

      RPM_MACROS="$HOME/.rpmmacros"
      RPM_TOPDIR="$DISTRIBDIR/rpm"

 export CVSROOT




# /------------------------------------------------------------------\
# |                      Functions Definitions                       |
# \------------------------------------------------------------------/


# --------------------------------------------------------------------
# Function  :  "fail()".

 fail ()
 {
   echo -e "\
                                                             \n\
                                                             \n\
                   @@@@@@@@@        @      @@@@@@ @@@@@@     \n\
                     @@    @        @        @@     @@       \n\
                     @@     @      @@@       @@     @@       \n\
                     @@            @@@       @@     @@       \n\
                     @@   @       @  @@      @@     @@       \n\
                     @@@@@@       @  @@      @@     @@       \n\
                     @@   @      @    @@     @@     @@       \n\
                     @@          @@@@@@@     @@     @@       \n\
                     @@         @      @@    @@     @@      @\n\
                     @@         @      @@    @@     @@     @ \n\
                   @@@@@@     @@@@    @@@@ @@@@@@ @@@@@@@@@@ "
   echo ""
 }




# --------------------------------------------------------------------
# Function  :  "pass()".

 pass ()
 {
   echo -e "\
                                                              \n\
                                                              \n\
                   @@@@@@@          @        @@@@ @    @@@@ @ \n\
                     @@   @@        @       @    @@   @    @@ \n\
                     @@    @@      @@@     @@     @  @@     @ \n\
                     @@    @@      @@@     @@@       @@@      \n\
                     @@   @@      @  @@     @@@@      @@@@    \n\
                     @@@@@        @  @@       @@@@      @@@@  \n\
                     @@          @    @@        @@@       @@@ \n\
                     @@          @@@@@@@   @      @@ @      @@\n\
                     @@         @      @@  @@     @@ @@     @@\n\
                     @@         @      @@  @@@    @  @@@    @ \n\
                   @@@@@@     @@@@    @@@@ @  @@@@   @  @@@@  "
   echo ""
 }




# --------------------------------------------------------------------
# Function  :  "part()".

 part ()
 {
   echo ""
   echo "----------------------------------------------------------------------"
   echo "$*"
   echo ""
 }




# --------------------------------------------------------------------
# Function  :  "try()".

 try ()
 {
   echo "Running Command :"
   echo "  $*"

   $* 
   EXITCODE=$?
   if [ $EXITCODE != 0 ]; then 
     echo "[ERROR] Command failed :"
     echo "  $*"
     fail
     exit $EXITCODE
   fi
 }




# /------------------------------------------------------------------\
# |                  Main Part of the Shell Script                   |
# \------------------------------------------------------------------/



 part "mkdistrib Started."
 try cd $DISTRIBDIR


(((

 part "Removing Any Previous Run."
 mkdir $TMPDIR
 mv alliance* dest-* build-* rpm/BUILD/* rpm/tmp/* rpm/SOURCES/* $TMPDIR
 rm -rf $TMPDIR &


 part "Checkout Sources From CVS."
 try cvs co -P alliance/src

# Remove undistributed tools.
 try ./alliance/src/.asim

 perl -pi -e "s§AC_DEFINE_UNQUOTED.*ALLIANCE_VERSION.*§AC_DEFINE_UNQUOTED(ALLIANCE_VERSION, \"$VERSION \[$RELEASE\]\")§" alliance/src/alliance.m4


 part "Building alliance-$VERSION.tar.gz [make dist]."
 try cd alliance/src

 try ./autostuff
 cd ..
 mkdir $TMPBUILDDIR
 mkdir $TMPINSTALLDIR
 try cd $TMPBUILDDIR

 ALLIANCE_TOP=/opt/alliance-$VERSION
 export ALLIANCE_TOP
 CPPFLAGS=-I$DISTRIBDIR/alliance/$TMPINSTALLDIR/$ALLIANCE_TOP/include
 export CPPFLAGS
 CFLAGS=-I$DISTRIBDIR/alliance/$TMPINSTALLDIR/$ALLIANCE_TOP/include
 export CFLAGS
 LIBS=-L$DISTRIBDIR/alliance/$TMPINSTALLDIR/$ALLIANCE_TOP/lib
 export LIBS

 try ../src/configure --prefix=$ALLIANCE_TOP
 try make DESTDIR="$DISTRIBDIR/alliance/$TMPINSTALLDIR" install
 #try make distcheck
 try make dist

 mv "$NAME.tar.gz" "$PACK.tar.gz"
 mv -f "$PACK.tar.gz" ../..

 cd ../..

 mv alliance $TMPALLIANCE
 (chmod -R 777 $TMPALLIANCE ; rm -fr $TMPALLIANCE) &

 try tar -xvzf "$PACK.tar.gz"


 for TARGET in $TARGETS
 do
   part "Building Package on Target $TARGET."
   try mkdir -p $TMPDESTDIR/$TARGET $TMPBUILDDIR/$TARGET

   ssh -n $TARGET \
     "cd $DISTRIBDIR/$TMPBUILDDIR/$TARGET; \
      ALLIANCE_TOP=$ALLIANCE_TOP; export ALLIANCE_TOP; \
      CPPFLAGS=-I$DISTRIBDIR/$TMPDESTDIR/$TARGET$ALLIANCE_TOP/include; export CPPFLAGS; \
      CFLAGS=-I$DISTRIBDIR/$TMPDESTDIR/$TARGET$ALLIANCE_TOP/include; export CFLAGS; \
      LIBS=-L$DISTRIBDIR/$TMPDESTDIR/$TARGET$ALLIANCE_TOP/lib; export LIBS; \
      MAKE=gmake; export MAKE; \
      PATH=${PATH}:/usr/ccs/bin; export PATH; \
      ../../$NAME/configure --prefix=$ALLIANCE_TOP --enable-alc-shared; \
      gmake DESTDIR=$DISTRIBDIR/$TMPDESTDIR/$TARGET install \
        && echo Alliance_compilation_OK"  2>&1 | tee $TARGETLOGFILE
   try grep Alliance_compilation_OK $TARGETLOGFILE
   try cd $DISTRIBDIR/$TMPDESTDIR/$TARGET
   try tar chozf $DISTRIBDIR/$PACK-$TARGET.tar.gz opt/alliance-$VERSION
   try cd $DISTRIBDIR
 done

 try mv $PACK-tsunami.tar.gz $PACK-$Linux.tar.gz
 try mv $PACK-funk.tar.gz $PACK-$Solaris.tar.gz


 part "Building RPM packages."

 rm -f $RPM_MACROS
 echo "%_topdir   $RPM_TOPDIR"     >> $RPM_MACROS
 echo "%_tmppath  %{_topdir}/tmp"  >> $RPM_MACROS
 for dir in $RPM_TOPDIR             \
            $RPM_TOPDIR/SPECS       \
            $RPM_TOPDIR/SOURCES     \
            $RPM_TOPDIR/BUILD       \
            $RPM_TOPDIR/SRPMS       \
            $RPM_TOPDIR/RPMS/noarch \
            $RPM_TOPDIR/RPMS/i386   \
            $RPM_TOPDIR/tmp
 do
   if [ ! -d "$dir" ]; then try mkdir -p $dir; fi
 done
 try cd rpm/SOURCES
 try ln -f ../../$PACK.tar.gz alliance-$VERSION.tar.gz
 try cd ../SPECS
 try cp -f ../../$NAME/distrib/alliance.spec .
 try cd ..
 rpmbuild -ba --clean --define "release $RELEASE" SPECS/alliance.spec
 EXITCODE=$?
 if [ $EXITCODE -ne 0 ]; then
   echo "[ERROR] Command failed :"
   echo "  rpmbuild -ba --clean --define "release $RELEASE" SPECS/alliance.spec"
   fail
   exit $EXITCODE
 fi
 try cd ../..


 try cp -f "$PACK.tar.gz"          $FTPDISTRIBDIR
 try cp -f "$PACK-$Linux.tar.gz"   $FTPDISTRIBDIR
 try cp -f "$PACK-$Solaris.tar.gz" $FTPDISTRIBDIR

 try cp -fp $RPM_TOPDIR/RPMS/i386/alliance*.rpm $FTPDISTRIBDIR
 try cp -fp $RPM_TOPDIR/SRPMS/alliance*.rpm     $FTPDISTRIBDIR


)      | perl -ne  '$|=1; printf("%s%s",   "[m", $_)' \
) 2>&1 | perl -ne  '$|=1; printf("%s%s%s", "[1;31;40m", $_, "[m")'
) 2>&1 > $LOGFILE

