#!/bin/sh
##########################################################################
#                  Aubit 4gl compiler - make system
#    prepmake - utillity script to convert makefiles to amake format
#    ---------------------------------------------------------------
#                    See manual for information
#
# $Id: prepmake,v 1.8 2004/10/04 13:49:58 afalout Exp $
#
#Run "prepmake" in directory containing make file, "makefile".
#This will create file "makefile.prep" containing instruction needed for
#dumping program definitions to individual make files (*.mk). Note: this
#functionallity depends on fact that all (Maximise) makefiles use command
#"fgllink" somewhere in each defined prgram target, and list all source
#files in dependencies. If for any reason this is not true for some makefie
#you want to process, look at the script, it should be easy to substitute
#this with some other present command.
#
#"prepmake" will then run first "touch *.4gl" (to force all targets into
#thinking they need building) and then "make -f makefile.prep". This will
#create one make file for each program defined in makefile.prep, named as
#<progname>.mk, using script "genmake". Each .mk files will contain
#definitions of include headers and footers, and names of source files
#needed to build that program, and nothing else. allsinglelibs.mk will
#also be created, and contain library objects - this file will probably
#not need to be used, since all makefiles for building programs should
#be able to build objects for themselves.
#
#WARNING: some Maximise moduls depend on other modules already being
#compiled. If they are not, makefile.prep will fail.
#Compile modules in this order:
#	winds csimods main ar eo fa gl in jm lc qe re ss ap pu wo
#
#
##########################################################################

########################
function replace {
########################

	if test "$QUIET" = "0"
    then
	    echo "Replacing    [$TOREPLACE]	with	[$INCLCMT]"
    fi
	sed -e "s%$TOREPLACE%$INCLCMT%g" $RESULTMAKEFILE > $TMPCOMPOSITE
	mv $TMPCOMPOSITE $RESULTMAKEFILE

}



########################
function r_ln {
########################

	#---------------------------------------------------------
	TOREPLACE="ln "
	INCLCMT='#'$TOREPLACE
	replace
	#---------------------------------------------------------
}

########################
function r_rm {
########################

	#---------------------------------------------------------
	#fonflicts with "fglfo-rm"
	#TOREPLACE="rm "
    TOREPLACE='	rm '
	INCLCMT='#'$TOREPLACE
	replace
	#---------------------------------------------------------
}


########################
function r_fgllink {
########################

	#replacing all "fgllink" lines with our command:
	#INCLCMT='	genmake $(basename $(@F)) $(addsuffix .4gl,$(basename $(^F))) #'
	#we don't want to rename ALL dependencies to .4gl they may be .per files!
	#INCLCMT='	genmake $(basename $(@F)) $(^F) #'
	#we alos don't want to strip extension from object name, it may be library
	#target, not executable:
	#---------------------------------------------------------

	if test "$FROMDEPS" = "1"
    then
		TOREPLACE=fgllink
		INCLCMT='	genmake $(@F) $(^F) #'$TOREPLACE
    else
		#fgllink -o  $G/P11.42r $L $G/P11.42m $G/P11a.42m ...
		TOREPLACE="fgllink -o"
		INCLCMT='	genmake '
    fi
	replace
	#---------------------------------------------------------

}

########################
function r_cat {
########################

	#commenting out all "cat" lines, so they don't get executed:
	#---------------------------------------------------------
	TOREPLACE="cat "
	INCLCMT="#$TOREPLACE"
	replace
	#---------------------------------------------------------

}

#######################
function r_fglcomp {
#######################

	#replace all fglcomp lines with fakes, so we don't go trough actual compile:
	#INCLCMT='touch $(addsuffix .42m,$(basename $($<)))'
	#INCLCMT='touch $(basename $<).42m'
	#---------------------------------------------------------
	TOREPLACE="fglcomp"
	INCLCMT='touch $(notdir $(basename $<)).42m #'$TOREPLACE
	replace
	#---------------------------------------------------------

}


#######################
function r_fgl2p {
#######################

	#---------------------------------------------------------
	#this is usefull only in CM, and I want form file names, so I'll
    #get this from dependencies anyway:
	#if test "$FROMDEPS" = "1"
	if test "1" = "1"
    then
		TOREPLACE='	fgl2p'
		INCLCMT='	genmake $(@F) $(^F) #'$TOREPLACE
    else
		TOREPLACE='	fgl2p -o'
		INCLCMT='	genmake '
    fi
	replace
	#---------------------------------------------------------

}


#######################
function r_fglform {
#######################

	#replace all fglform lines with fakes, so we don't go trough actual compile:
	#INCLCMT='touch $(notdir $(basename $<)).42f'
	#---------------------------------------------------------
	TOREPLACE="fglform"
	INCLCMT='touch $(basename $<).42f #'$TOREPLACE
	replace
	#---------------------------------------------------------
}

#####################
function r_form4gl {
#####################

	#---------------------------------------------------------
	TOREPLACE="form4gl"
	INCLCMT='touch $(basename $<).frm #'$TOREPLACE
	replace
	#---------------------------------------------------------

}

#####################
function r_fglpc {
#####################

	#we need all fglpc replaced with commands, since in winds most files
	#are not programs, but library functions.
	#---------------------------------------------------------
	#if test "$FROMDEPS" = "1"
    #then
		TOREPLACE="fglpc"
		INCLCMT='	genmake $(@F) $(^F) #'$TOREPLACE
    #else
	#	#fglpc -a -p $P P11.4gl;fgl2p P11.4gl;mv P11.42m $G
	#	TOREPLACE="fgllink -o"
	#	INCLCMT='	genmake '
    #fi
	replace
	#---------------------------------------------------------
}


#################
function doit {
#################

	if test "$SKIP" = "1"
    then
        if [ -f $RESULTMAKEFILE ]
        then
            echo "SKIP is on, $RESULTMAKEFILE exists - skipping..."
            return
        fi
    fi

	if [ ! -f makefile ]
    then
	    if [ ! -f Makefile ]
    	then
			echo "WARNING: no file called 'M/makefile' in $PWD"
	        EXITCODE=1

            if test "$RECURSIVE" = "0"
            then
				exit $EXITCODE
            else
				if test "$STOPONERROR" = "1"
    	        then
					#exit $EXITCODE
                    return
                fi
			fi
            return
        else
			cp Makefile $RESULTMAKEFILE
		fi
    else
		cp makefile $RESULTMAKEFILE
    fi

	if test "$CLEAN" = "1"
	then
        echo "Deleting all .mk files ..."
		rm -f *.mk
    fi

    #add default rules, so make don't complain about missing rules
    #or missing dependencies:

    echo " " >> $RESULTMAKEFILE
	echo ".SUFFIXES:   .ao .4gl .c .4ae .afr .per .iem .msg .hlp .4go .4gi .42m .42r" >> $RESULTMAKEFILE
	echo " " >> $RESULTMAKEFILE

	#echo ".4gl:" >> $RESULTMAKEFILE
    #echo "	touch $<" >> $RESULTMAKEFILE
	#echo ".4go:" >> $RESULTMAKEFILE
    #echo "	touch $<" >> $RESULTMAKEFILE
	#echo ".4gi:" >> $RESULTMAKEFILE
    #echo "	touch $<" >> $RESULTMAKEFILE
	#echo ".4gl.4go:" >> $RESULTMAKEFILE
    #echo "	touch $<" >> $RESULTMAKEFILE
	#echo '$P/*.4go :' >> $RESULTMAKEFILE
    #echo '	touch $P/$<' >> $RESULTMAKEFILE
	#echo '$P/.4go :' >> $RESULTMAKEFILE
    #echo '	touch $P/$<' >> $RESULTMAKEFILE

    echo '%::' >> $RESULTMAKEFILE
    echo '	touch $@' >> $RESULTMAKEFILE
    echo '.DEFAULT:' >> $RESULTMAKEFILE
    echo '	touch $@' >> $RESULTMAKEFILE
	echo " " >> $RESULTMAKEFILE


    r_ln
    r_rm


	if [ "$SUBDIR" = "ap" -o "$DOALL" = "1" ]
	then
		r_fgllink
		r_cat
    fi

	if [ "$SUBDIR" = "gl" -o "$DOALL" = "1" ]
	then
		r_fglcomp
		r_fglform
		r_form4gl
	fi

	if [ "$SUBDIR" = "winds" -o "$DOALL" = "1" ]
	then
	    r_fglpc

		#but we also need to process fgllink, since it creates composite library
		#objects (.42x)
	fi

	#in CM:
	r_fgl2p


	if [ "$SUBDIR" = "main" -o "$DOALL" = "1" ]
	then
		if [ "$MAXDIR" != "" ]
        then
			touch $MAXDIR/prog/lic_calc.4go
	        touch $MAXDIR/prog/lic_check.4go
	        touch $MAXDIR/prog/lic_start.4go
	        touch $MAXDIR/prog/menuwind.4go
        fi
	fi


#############################################
#Run prepared makefile, to create .mk files:

	if test "$VERBOSE" = "1"
	then
		echo " "
	   	echo "-------------- Running make -f $RESULTMAKEFILE --------------------"
		echo " "
	else
		if test "$QUIET" = "0"
    	then
			echo "Running make -f $RESULTMAKEFILE ..."
        fi
	fi

	touch *.4gl
	touch *.per
	touch *.msg
	make -f $RESULTMAKEFILE > prepmake.log 2>&1
	RET=$?

	if test "$RET" != "0"
	then
	    if test "$VERBOSE" = "1"
        then
			tail prepmake.log
		   	echo "----------------------- Finished running make ---------------------------"
            echo "PWD = $PWD"
		fi
		echo "Error preparing make file, see $RESULTMAKEFILE and prepmake.log"
	    EXITCODE=1

		if test "$STOPONERROR" = "1"
        then
	        exit $EXITCODE
        else
			return
        fi
	else
		if test "$QUIET" = "0"
    	then
			echo "----------------------- Finished running make ---------------------------"
        fi
		#if we created single libs file, we need to add footer at the end:
		FOOTERNAME='${AUBITDIR}/incl/footer.mki'

		if [ -f $FILENAME ]
	    then
			echo >> $FILENAME
		    #echo "#-------------------------------------" >> $FILENAME
			#echo >> $FILENAME
			echo include $FOOTERNAME >> $FILENAME
			echo >> $FILENAME
	    fi

		ls *.mk > /dev/null 2>&1
		RET=$?
		if test $RET != "0"
		then
	        echo "ERROR(?): No .mk files created in in $PWD"
	    fi

		if test "$QUIET" = "0"
    	then
            echo "Done in $PWD"
            EXITCODE=0
			return
        fi
	fi

}


########################
#MAIN
########################

#############################
#Configuration:
	FILENAME=allsinglelibs.mk
	rm -f $FILENAME
	TMPCOMPOSITE=/tmp/makefile.1
	RESULTMAKEFILE=makefile.prep
	GENMAKE=genmake
    DOALL=1
	QUIET=0
	RECURSIVE=0
    STOPONERROR=1

    #If you want to extract components from list of dependencies, instead
	#directly form link line, change this to 1:
    #This is a tradeoff, at least with Maximise: if you take names from
    #command line, you will loose form files names, that can be listed
    #only as dependencies; but if you take names from dependencies, you
    #will not get distiction between library extensions (.42x/.42o)
	FROMDEPS=0

	###########
	for a in $@
    ###########
	do
		##########
		case $a in
	    ##########

                -help)
                    echo " prepmake [options] [name]"
                    echo " -verbose"
                    echo ' -quiet'
                    echo " -clean"
                    echo ' -skip'
					echo " -all"
                    echo ' -r'
                    echo " name = Maximise module/subdir name"
                    echo " "
                    exit 1
                ;;


			   	-verbose | --verbose) 	VERBOSE=1
			   	;;

				-quiet)  	VERBOSE=0
                            QUIET=1
               	;;

			   	-clean) 	CLEAN=1
			   	;;

			   	-skip) 		SKIP=1
			   	;;


			   	-all) 		DOALL=1
			   	;;

			   	-r) 		RECURSIVE=1
			   	;;

			   	*)
			   				if [ -d $a ]
							then
								SUBDIR=$a
                            else
								echo "ERROR: parameter $a not recognised - not subdir"
								exit 1
                            fi
               	;;


		  ####
		  esac
          ####
	####
	done
    ####

    echo "Processing \"$PWD\""

    ####
	doit
    ####


    if test "$RECURSIVE" = "1"
    then

	    ROOTDIR=$PWD
		ALLSUBDIRS=`ls -l | grep "^d" | awk '{print $NF}'`

		################################
        for subsystem in $ALLSUBDIRS; do
		################################

            echo "Processing \"$subsystem\""
			cd $ROOTDIR/$subsystem ;

            if [ "$subsystem" = "cm" ]
            then
				cd $ROOTDIR/$subsystem/source
            fi

		    ####
			doit
		    ####

        ####
        done
        ####

    fi

	if test "$QUIET" = "0"
   	then
		echo "'prepmake' completed. You can now run generated make files using "
		echo "'amake' script. see 'amake -help' for instructions"
    else
        echo "prepmake: OK"
    fi

	exit 0

#------------------------------ EOF -------------------------------------
