#! /bin/sh

modulesfile=modules
topdir=..
output=$topdir/Makefile

cat > $output  <<EOF
#
#	Makefile for dcmtk (DICOM Toolkit)
#

SHELL = /bin/sh
configdir = config

include \$(configdir)/Makefile.def

.NOTPARALLEL:

EOF
if test -f "$modulesfile" ; then
	tmpmodules=`cat $modulesfile`
else
	tmpmodules=`(cd $topdir; ls)`
fi
for dir in $tmpmodules; do
	if test -d $topdir/$dir ; then
		modules="$modules $dir"
	fi
done

modules=`echo $modules | sed -e "s/config//g" | sed -e "s/doxygen//g"`
for module in config $modules; do
	makeall="$makeall ${module}-all"
if test $module != config ; then
	makelibsrcall="$makelibsrcall ${module}-libsrc-all"
fi
	maketestsall="$maketestsall ${module}-tests-all"
	makeinstall="$makeinstall ${module}-install"
	makeinstallbin="$makeinstallbin ${module}-install-bin"
	makeinstalldoc="$makeinstalldoc ${module}-install-doc"
	makeinstalldata="$makeinstalldata ${module}-install-data"
	makeinstalletc="$makeinstalletc ${module}-install-etc"
	makeinstalllib="$makeinstalllib ${module}-install-lib"
	makeinstallinclude="$makeinstallinclude ${module}-install-include"
	makeinstallsupport="$makeinstallsupport ${module}-install-support"
	makecheck="$makecheck ${module}-check"
	makecheckexhaustive="$makecheckexhaustive ${module}-check-exhaustive"
done

cat >> $output <<EOF
all: $makeall

libsrc-all: $makelibsrcall

tests-all: $maketestsall

install: $makeinstall dcmtk-install-doc install-man

install-all: install install-lib install-html

install-bin: $makeinstallbin

install-doc: $makeinstalldoc

install-data: $makeinstalldata

install-etc: $makeinstalletc

install-lib: $makeinstalllib

install-include: $makeinstallinclude

install-support: $makeinstallsupport

check: tests-all
	\$(MAKE) -s check-nosilent

check-nosilent: $makecheck

check-exhaustive: tests-all
	\$(MAKE) -s check-nosilent-exhaustive

check-nosilent-exhaustive: $makecheckexhaustive

dcmtk-install-doc:
	\$(configdir)/mkinstalldirs \$(DESTDIR)\$(docdir)
	for file in ANNOUNCE CHANGES COPYRIGHT CREDITS FAQ HISTORY VERSION docs/CHANGES.???; do \\
		\$(INSTALL_DATA) \$\$file \$(DESTDIR)\$(docdir) ;\\
	done

install-man:
	(cd doxygen && \$(MAKE) DESTDIR="\$(DESTDIR)" install-man)

install-html:
	(cd doxygen && \$(MAKE) DESTDIR="\$(DESTDIR)" install-html)

man:
	(cd doxygen && \$(MAKE) DESTDIR="\$(DESTDIR)" man)

html:
	(cd doxygen && \$(MAKE) DESTDIR="\$(DESTDIR)" html)

help:
	@echo "Some available make targets:"
	@echo ""
	@echo "all:                      build all libraries, tests and applications"
	@echo "libsrc-all:               build all libraries"
	@echo "install-all:              install the toolkit with its libraries and documentation"
	@echo "install:                  install all applications"
	@echo "install-lib:              install the libraries"
	@echo "install-html:             install the HTML documentation"
	@echo "html:                     build the HTML documentation with doxygen"
	@echo "man:                      build the man pages with doxygen"
	@echo "check:                    build and run all tests, not including exhaustive tests"
	@echo "check-exhaustive:         build and run all tests, including exhaustive tests"
	@echo "dependencies:             update all Makefile.dep dependency files"
	@echo "clean:                    remove compiler output"
	@echo "distclean:                remove compiler output and generated build structure"
	@echo "                          (This removes all files generated by configure)"
	@echo ""
	@echo "Additionally, there are a number of module-specific targets available."
	@echo "For example:"
	@echo ""
	@echo "<module>-all              build the complete module"
	@echo "<module>-libsrc-all       build the module's libraries"
	@echo "<module>-install          install the module"
	@echo "<module>-install-lib      install the module's libraries"
	@echo "<module>-check            build and run the module's tests, not including exhaustive tests"
	@echo "<module>-check-exhaustive build and run the module's tests, including exhaustive tests"
	@echo ""
	@echo "The following modules are available:"
	@echo ""
	@echo "$modules"

EOF

for module in config $modules; do
	cat >> $output <<EOF
${module}-all:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" all)

${module}-libsrc-all:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" libsrc-all)

${module}-tests-all:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" tests-all)

${module}-install:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install)

${module}-install-bin:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-bin)

${module}-install-doc:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-doc)

${module}-install-data:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-data)

${module}-install-etc:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-etc)

${module}-install-lib:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-lib)

${module}-install-include:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-include)

${module}-install-support:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" install-support)

${module}-check:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" check)

${module}-check-exhaustive:
	(cd $module && \$(MAKE) ARCH="\$(ARCH)" DESTDIR="\$(DESTDIR)" check-exhaustive)

EOF
done
for tag in dependencies clean distclean ; do
	echo "${tag}:" >> $output
	if test $tag = dependencies ; then
		echo "	-(cd config && \$(MAKE) ${tag})" >> $output
	fi
	for module in $modules; do
		echo "	(cd ${module} && \$(MAKE) ${tag})" >> $output
	done
	if test $tag != dependencies ; then
		echo "	(cd doxygen && \$(MAKE) ${tag})" >> $output
		echo "	-(cd config && \$(MAKE) ${tag})" >> $output
	fi
	if test $tag = clean ; then
		echo "	rm -f \$(TRASH)" >> $output
	elif test $tag = distclean ; then
		echo "	rm -f \$(TRASH)" >> $output
	fi
	echo >> $output
done

output=$topdir/configure
cat > $output <<EOF
#! /bin/sh
if test -d config ; then
	if test -f config/$modulesfile ; then
		tmp=\`cat config/$modulesfile\`
		modules=\`ls -d \$tmp\`
	else
		for dir in \`ls\`; do
			if test -d \$dir ; then
				modules="\$modules \$dir"
			fi
		done
	fi
	modules=\`echo \$modules | sed -e "s/config//g"\`
	modules="config \$modules doxygen"
else
	echo "Cannot find configure directory (config or ../config)"
	exit 1
fi
if test "x--help" = "x\$*"; then
	config/configure --help
elif test "x--help=short" = "x\$*"; then
	config/configure --help=short
elif test "x--help=recursive" = "x\$*"; then
	config/configure --help=recursive
elif test "x--version" = "x\$*"; then
	config/configure --version
elif test "x-h" = "x\$*"; then
	config/configure -h
elif test "x-V" = "x\$*"; then
	config/configure -V
else
	args=""
	ok=0
	while [ \$# -gt 0 ]; do
		if test "x--ignore-deprecation" = "x\$1"; then
			ok=1
		else
			args="\$args \$1"
		fi
		shift
	done
	if [ \$ok -eq 0 ]; then
		echo ""
		echo "== DEPRECATION WARNING =="
		echo ""
		echo "Configuring DCMTK via GNU Autoconf is deprecated and will be removed.  Please have"
		echo "a look at CMake, which we now consider equally mature.  If you run into serious"
		echo "problems using CMake or strongly prefer GNU Autoconf for some other reason write"
		echo "an email to the"
		echo ""
		echo "  DCMTK Team <bugs@dcmtk.org>"
		echo ""
		echo "and use"
		echo ""
		echo "  ./configure --ignore-deprecation"
		echo ""
		echo "to run the old configure script anyway."
		echo ""
		exit 1
	fi
	for module in \$modules ; do
		echo "Running configure for module \$module."
		if test ! -x "\$module/configure" ; then
			echo "Module \$module does not contain a configure script!" >&2
			exit 1
		fi
		(cd \$module && ./configure \$args ) || exit 1
	done
fi
EOF
chmod 755 $output
