#! /bin/sh

###########################################################
#  $Id: configure,v 1.9 2006/05/04 18:09:39 saroun Exp $                                                 
#  Creates  'makefile' for RESTRAX under UNIX/Linux                          
#  (c) J. Saroun, 2004                       
#                                                        
#  Takes system-dependent information from the 'config.<sys>' files.                        
#                                                        
###########################################################

#------------  Define variables  ------------
PGMNAME="restrax"
VERSION="4.8.3"
#--------------------------------------------

PACKNUM=`echo $VERSION | sed -e s/[.]//g `
BUILDNUM=`echo $PACKNUM | awk '{print $1/100}'`
BDATE="`date -u +%Y-%m-%d/%T ` UTC"

# Name of the executable:
EXEC=$PGMNAME$PACKNUM

# Name of the EXCI shared library (without extension and 'lib' prefix):
ELIB=res_exci
# Path to the 'config.<sys> files:
CFGDIR=./config
# Path to the source files
SRC=src
# Path to EXCI source files
EXCSRC=exci
# Path to executable files 
BINDIR=bin
# Path to library files 
LIBDIR=lib
# Path to public include files 
INCLDIR=include
# Documented source files 
DOCFILES="include/*.inc src/*.inc src/ness/*.inc src/*.f src/ness/*.f src/exci/*.f  exci/*.f"
DOCEXCI="include/exci.inc include/const.inc include/reclat.inc exci/res_exci_osc.f src/exci/*.f"
# Target directories - will be created if they do not exist
TARDIR="$BINDIR $BINDIR/ness  $BINDIR/cfiles  doc doc/srcdoc"

# Check command and distribution consistency
if [ -z "$1" ] ; then
	echo "Usage: $0 <sys> [-g] [-all]"
	echo " <sys> ... defines the configuration file $CFGDIR/config.<sys> "
	echo " -g    ... compile with debug option "
	echo " -all  ... create scripts for installation, packaging etc. "
	exit
fi
if [ "a$2" = "a-g" -o "a$3" = "a-g" ] ; then
	DBG="-g"
else
	DBG=""
fi


# check source directories
for F in $SRC $SRC/ness $SRC/cfiles $INCLDIR $CFGDIR 
do
	if [ ! -d ./$F ] ; then
		echo "Inconsistent distribution, directory does not exist: "$F
		IERR=1
	fi
done

# check target directories, create when needed
for F in $TARDIR
do
	if test ! -d $F; then
		mkdir $F;
		chmod 755 $F
		echo "Created target directory: "$F
	fi
done

# check configuration file
CFG=$CFGDIR/config.$1
IERR=0
for F in $CFG
do
	if [ ! -f ./$F ] ; then
		echo "Configuration file does not exist: "$F
		IERR=1
	fi
done

# Abort if necessary
if [ $IERR != "0" ] ; then
	echo "Errors in configuration - cannot create makefile."
	exit
fi


# Define shared library extension, SHEXT=value, in the config.<sys> file
# if different from ‘so‘
SHEXT=`grep SHEXT= $CFG | awk '{print substr($1,index($1,"=")+1)}'`
if [ "a$SHEXT" = "a" ]; then
	SHEXT=so
fi

# ---------------  create config.inc ----------------------
F=$SRC/config.inc
echo "C generated by configure:" > $F
cat >> $F << \EOD
      CHARACTER*32 PACKAGE,PACKAGE_VERSION,PACKAGE_DATE,SYSNAME
      REAL BUILD_NUMBER
      CHARACTER*3 SHEXT
      PARAMETER (
EOD
echo "     *  PACKAGE=\"$PGMNAME\"," >> $F
echo "     *  PACKAGE_VERSION=\"$VERSION\"," >> $F
echo "     *  PACKAGE_DATE=\"$BDATE\"," >> $F
echo "     *  BUILD_NUMBER=$BUILDNUM," >> $F
echo "     *  SYSNAME=\"`uname -s`\"," >> $F
echo "     *  SHEXT=\"$SHEXT\"" >> $F
cat >> $F << \EOD
     * )
EOD
# ---------------  create makefile for EXCi library ----------------------
configure.EXCI $1 $DBG

#  ---------------  create makefile for RESTRAX  --------------------------
MK=makefile

echo "#  Automatically created makefile for $PGMNAME $VERSION" > $MK
echo "# `date`    "                                            >> $MK
echo "#  Configuration: $CFG " >>  $MK
echo "#------------------------------------------------------" >> $MK
echo "#"  >> $MK
echo "PGMNAME=$PGMNAME" >> $MK
echo "VERSION=$VERSION" >> $MK
echo "SRC=$SRC" >> $MK
echo "EXCSRC=$EXCSRC" >> $MK
echo "BINDIR=$BINDIR" >> $MK
echo "LIBDIR=$LIBDIR" >> $MK
echo "INCLDIR=$INCLDIR" >> $MK
echo "EXEC=$EXEC" >> $MK
echo "SHEXT=$SHEXT" >> $MK
echo "DBG=$DBG" >> $MK
echo "DOCFILES=$DOCFILES" >> $MK
echo "DOCEXCI=$DOCEXCI" >> $MK


# copy the contents of config.<sys> file ino makefile
echo " " >> $MK
cat $CFG >> $MK
echo " " >> $MK

srcsubdirs="ness"
SOURCES="`ls $SRC/*.f `"

for F in $srcsubdirs
do
	if test ! -d $BINDIR/$F; then
		mkdir $BINDIR/$F;
		chmod 755 $BINDIR/$F;
	fi
	SOURCES="$SOURCES `ls $SRC/$F/*.f`";
done

SOURCES="$SOURCES `ls $SRC/cfiles/*.c`";


OBJ="OBJECTS = "
for F in $SOURCES
do
	F1=`echo $F | awk '{print substr($1,index($1,"/")+1,index($1,".")-index($1,"/")-1)}'`
	OBJ="$OBJ $BINDIR/$F1.o "
done 
echo $OBJ >> $MK

 
# find PGPLOT library
PGPATH="$PGLOT_DIR /usr/local/pgplot /usr/local/lib /usr/lib "

# try preferably the archive version
# override this search by setting $LPGPLOT 
if test -z "$LPGPLOT" ; then
	for F in $PGPATH
	do
		if test -f $F/libpgplot.a ; then
			LPGPLOT="$F/libpgplot.a"
			break 
		elif test -f $F/libpgplot.$SHEXT ; then
			LPGPLOT="-L$F -lpgplot" 
			break
		fi
	done
fi



if test -z "$LPGPLOT" ; then
	echo "PGPLOT library not found." 
	echo "Define '\$LPGPLOT variable as a full pathname of libpgplot.a'" 
	exit
fi

echo "PGPLOT link: "$LPGPLOT
echo "LPGPLOT=$LPGPLOT" >> $MK

#  Copy following lines to $MK
cat >> $MK << \EOD

all: $(OBJECTS) $(BINDIR)/$(EXEC) excilib

binary: $(BINDIR)/$(EXEC)  excilib

$(BINDIR)/$(EXEC): $(OBJECTS)
	@echo 
	@echo 'Linking $(PGMNAME) $(VERSION) '
	$(restrax_LD) $(restrax_LDFLAGS) $(OBJECTS)  $(LPGPLOT) $(restrax_LDADD) -o $(BINDIR)/$(EXEC) 
	@echo 'Executable for $(PGMNAME) $(VERSION) was built. '
	chmod 755 $(BINDIR)/$(EXEC)
#	ldd -r -v $(BINDIR)/$(EXEC)
#	rm -f $(ABSTEXCI)
	@echo 


$(BINDIR)/%.o: $(SRC)/%.f
	$(FC) $(FCFLAGS) $(DBG) -I$(INCLDIR) -I$(SRC) -I$(SRC)/ness -c $< -o $@

$(BINDIR)/cfiles/%.o: $(SRC)/cfiles/%.c
	$(CC) $(CCFLAGS) $(DBG) -I$(INCLDIR) -c $< -o $@	


# Dependences: 
EOD

for F in $SOURCES
do
	F1=`echo $F | awk '{print substr($1,index($1,"/")+1,index($1,".")-index($1,"/")-1)}'`
	DEP="$BINDIR/$F1.o: $F "
	TMP=`grep -i include $F | grep "\.inc" | egrep -iv "C |\!" | sort -u -k 2,2 | awk -F "'" '{print $2}'`
	TMP1=`grep -i include $F | grep "\.h" | egrep -iv "C |\!" | sort -u -k 2,2 | awk -F '"' '{print $2}'`
	TMP="$TMP $TMP1"
	for T in $TMP
	do
		if [ -f $SRC/$T ] ; then
			DEP="$DEP $SRC/$T"
		elif [ -f $SRC/ness/$T ] ; then
			DEP="$DEP $SRC/ness/$T"
		else
			DEP="$DEP $INCLDIR/$T"
		fi
	done
	echo $DEP >> $MK
done

cat >> $MK << \EOD

excilib:
	make -f $(EXCSRC)/makefile

srcdoc:
	res2html -sT  -tRESTRAX -uhttp://omega.ujf.cas.cz/restrax -ddoc/srcdoc $(DOCFILES)
	res2html -sT  -tEXCI  -bsrcexci/  -uhttp://omega.ujf.cas.cz/restrax -ddoc/srcdoc $(DOCEXCI)

clean:
	make -f $(EXCSRC)/makefile clean
	rm -f $(OBJECTS) $(BINDIR)/core core 

cleandist:
	make -f $(EXCSRC)/makefile cleandist 
	rm -f $(EXCSRC)/makefile*
	rm -f $(OBJECTS) $(BINDIR)/$(EXEC) $(LIBDIR)/* core makefile* ZipBin ZipDist ZipExci ZipDoc

EOD

#--------------------------------------------------------
#  Create and execute makefile for EXCI
#--------------------------------------------------------

if [ "$2" != "-all" -a "$3" != "-all" ] ; then
	cp $MK makefile.$1
	exit
fi

MYDIR=`pwd`
Make_run "$MYDIR" "$EXEC" "auto"
Make_start "$MYDIR" "$PGMNAME" "$VERSION"  "$BDATE"

#--------------------------------------------------------
#  Create Install file
#--------------------------------------------------------
echo "#!/bin/sh" > Install
echo "PGMNAME=\"$PGMNAME\"" >> Install
echo "VERSION=\"$VERSION\"" >> Install
echo "BDATE=\"$BDATE\"" >> Install
echo "EXEC=\"$EXEC\"" >> Install
echo "ELIB=\"$ELIB\"" >> Install
echo "SHEXT=\"$SHEXT\"" >> Install

cat templates/Install_template >> Install
if [ -f Install ]; then
	chmod 740 Install
	echo Install script created
fi

#--------------------------------------------------------
#  Create ZipDist file
#--------------------------------------------------------
echo "#!/bin/sh" > ZipDist
echo "PGMNAME=\"$PGMNAME\"" >> ZipDist
echo "VERSION=\"$VERSION\"" >> ZipDist
cat templates/ZipDist_template >> ZipDist
if [ -f ZipDist ]; then
	chmod 740 ZipDist
	echo ZipDist script created
fi

#--------------------------------------------------------
#  Create ZipBin file
#--------------------------------------------------------
echo "#!/bin/sh" > ZipBin
echo "PGMNAME=\"$PGMNAME\"" >> ZipBin
echo "VERSION=\"$VERSION\"" >> ZipBin

cat templates/ZipBin_template >> ZipBin
if [ -f ZipBin ]; then
	chmod 740 ZipBin
	echo ZipBin script created
fi

#--------------------------------------------------------
#  Create ZipExci file
#--------------------------------------------------------
echo "#!/bin/sh" > ZipExci
echo "PGMNAME=\"$PGMNAME\"" >> ZipExci
echo "VERSION=\"$VERSION\"" >> ZipExci

cat templates/ZipExci_template >> ZipExci
if [ -f ZipExci ]; then
	chmod 740 ZipExci
	echo ZipExci script created
fi

#--------------------------------------------------------
#  Create ZipDoc file
#--------------------------------------------------------
echo "#!/bin/sh" > ZipDoc
echo "PGMNAME=\"$PGMNAME\"" >> ZipDoc
echo "VERSION=\"$VERSION\"" >> ZipDoc

cat templates/ZipDoc_template >> ZipDoc
if [ -f ZipDoc ]; then
	chmod 740 ZipDoc
	echo ZipDoc script created
fi

cat >> $MK << \EOD
install:
	@echo "Call script: 'Install <target directory>' "

dist:
	ZipDist

distbin:
	ZipBin

distexci:
	ZipExci

distdoc:
	ZipDoc

EOD

cp $MK makefile.$1

