#! /bin/sh

###########################################################
#                                                   
#  Creates  'makefile' for RESTRAX under UNIX/Linux                          
#  (c) J. Saroun, 2004                       
#                                                        
#  Takes system-dependent information from the 'config.<sys>' files.                        
#                                                        
###########################################################

#------------  Define variables  ------------
PGMNAME="simres"
VERSION="5.0.1"
PACKNUM="501"
BUILDNUM="5.01"
BDATE=`date`

# Name of the executable:
EXEC=$PGMNAME$PACKNUM
# Path to the 'config.<sys> files:
CFGDIR=./config
# Path to the source files
SRC=src
# Path to executable files 
BINDIR=bin
# Path to library files 
LIBDIR=bin
# Path to public include files 
INCLDIR=include
# Documented source files 
DOCFILES="include/*.inc src/*.inc src/ness/*.inc src/*.f src/ness/*.f "
# Target directories - will be created if they do not exist
TARDIR="$BINDIR $LIBDIR $BINDIR/ness 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" ] ; then
	DBG="-g"
else
	DBG=""
fi

# check source directories
for F in $SRC $INCLDIR $CFGDIR $SRC/ness
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=$INCLDIR/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 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 "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


# 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


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="$PGPLOT_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: $(BIN)/$(EXEC)

$(BIN)/$(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)
	@echo 

$(BINDIR)/%.o: $(SRC)/%.f
	$(FC) $(FCFLAGS) $(DBG) -I$(INCLDIR) -I$(SRC) -I$(SRC)/ness -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="$BIN/$F1.o: $F "
	TMP=`grep -i include $F | grep "\.inc" | egrep -iv "C |\!" | sort -u -k 2,2 | awk -F "'" '{print $2}'`
	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

srcdoc:
	res2html -sT  -tSIMRES -uhttp://omega.ujf.cas.cz/restrax -ddoc/srcdoc $(DOCFILES)

clean:
	rm -f $(OBJECTS)  core 

cleandist:
	rm -f $(OBJECTS) $(BINDIR)/$(EXEC)  core makefile* ZipBin ZipDist ZipExci ZipDoc
	rm -f ness*
	rm -f *.dat


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 ver. $VERSION, build $BDATE "

#--------------------------------------------------------
#  Create Install file
#--------------------------------------------------------
echo "#!/bin/sh" > Install
echo "VER='$PGMNAME ver. $VERSION, build $BDATE'" >> Install
echo "EXEC=$EXEC" >> 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 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

distdoc:
	ZipDoc


EOD

cp $MK makefile.$1

