#!/bin/sh
# Automagically generate make rules to compile jar files 
# specified in the Makefile.aj
#
# (C) Tapsell-Ferrier Limited 2002  <nferrier@tapsellferrier.co.uk>
#
# This results in a file called: 
#  Makefile 
# unless there is an argument specified in which case 
# it results in a file specified in the argument.
# 
# Therefore to produce a Makefile.in you could do this:
#
#  automakejar Makefile.in
#

# Create the generic editor script.
SEDSCRIPT=/tmp/automakejar.$$
cat > ${SEDSCRIPT} <<\EOF
#-n
# A first stage sed script to produce a Makefile from a template.
# (C) Tapsell-Ferrier Limited 2002 <nferrier@tapsellferrier.co.uk>
#
# This script would be much easier if GNU Make's target specific variables
# cascaded into sub-targets. But they don't as of 3.79.1.
/^__JARFILENAME__.jar/,/^$/{
    # Remove the dummy target.
    /^__JARFILENAME__.jar:$/d
    # Make sure the variable definitions appear at the start of lines.
    s/^	sourcedir=/sourcedir=/
    s/^	sourcefiles=/sourcefiles=/
    s/^	classpath=/classpath=/
    s/^	classesdest=/classesdest=/
    s/^	otherfiles=/otherfiles=/
    s/^	manifest=/manifest=/
    # Mangle the names of the target variables
    s/sourcedir/__JARFILENAME___jar_sourcedir/
    s/sourcefiles/__JARFILENAME___jar_sourcefiles/
    s/classpath/__JARFILENAME___jar_classpath/
    s/classesdest/__JARFILENAME___jar_classesdest/
    s/otherfiles/__JARFILENAME___jar_otherfiles/
    s/manifest/__JARFILENAME___jar_manifest/
    # Ensure $variables are referenced properly
    s/\$\([A-Za-z_][A-Za-z_]*\)/$(\1)/g
    # Change quoted sh syntax to make syntax
    s/`\(.*\)`/$(shell \1)/
    # Ensure the variables appear
    /^[__JARFILENAME___jar_*]/p
    # Insert the rest of the template.
    /^$/i\
__JARFILENAME___jar_debugclasses=$(__JARFILENAME___jar_sourcefiles:.java=.class)\
__JARFILENAME___jar_classfiles=$(__JARFILENAME___jar_debugclasses:$(__JARFILENAME___jar_sourcedir)%=$(__JARFILENAME___jar_classesdest)%)\
\
__JARFILENAME__.jar: __JARFILENAME__-init $(__JARFILENAME___jar_classfiles) __JARFILENAME__-compilation $(__JARFILENAME___jar_otherfiles) $(__JARFILENAME___jar_manifest)\
	$(JAR) cf$(if $(__JARFILENAME___jar_manifest),m) __JARFILENAME__.jar $(__JARFILENAME___jar_manifest) $(__JARFILENAME___jar_otherfiles) -C $(__JARFILENAME___jar_classesdest) .\
\
.PHONY: __JARFILENAME__-init __JARFILENAME__-compilation\
\
__JARFILENAME__-init:\
	echo > filelist\
\
__JARFILENAME__-compilation: $(__JARFILENAME___jar_classesdest)\
	$(if $(shell cat filelist),$(JAVAC) $(JAVAC_OPTS) -d $(__JARFILENAME___jar_classesdest) -classpath $(call PATHMK,$(__JARFILENAME___jar_classesdest) $(call PATHMK,$(__JARFILENAME___jar_classpath))) @filelist)\
\
$(__JARFILENAME___jar_classesdest):\
	mkdir $@\
\
$(__JARFILENAME___jar_classesdest)/%.class: $(__JARFILENAME___jar_sourcedir)/%.java\
	@echo $? >> filelist\
\
echo___JARFILENAME___jar:\
	@echo sourcedir = $(__JARFILENAME___jar_sourcedir)\
	@echo sourcefiles = $(__JARFILENAME___jar_sourcefiles)\
	@echo classpath = $(call PATHMK,$(__JARFILENAME___jar_classpath))\
	@echo classesdest = $(__JARFILENAME___jar_classesdest)\
	@echo classfiles = $(__JARFILENAME___jar_classfiles)\
\
ifeq (${OS},Windows_NT)\
PATHMK = $(subst $(space),;,$(1))\
else\
PATHMK = $(subst $(space),:,$(1))\
endif
# Ensure we branch to end of script once template is done.
    t
}
# Output all other lines as they are.
p
EOF

# The file to produce.
Makefile=${1:-Makefile}

# The name of the template file.
MakeTemplate=`dirname $0`/Makefile.aj


echo Making $Makefile from $MakeTemplate

# Make a bespoke sed script and then edit the source Makefile.
for something in `sed -n 's/^\([A-Za-z0-9_-]*\)\.jar:/\1/p' ${MakeTemplate}`
do
  EDITSCRIPT=${SEDSCRIPT}.edit.$$
  sed -n "s/__JARFILENAME__/${something}/g ; p" ${SEDSCRIPT} > ${EDITSCRIPT}
  sed -n -f ${EDITSCRIPT} ${MakeTemplate} > ${Makefile}
done

# end.