#!/bin/sh

#################################################
#                                               #
#        Assign/reset vars we will use          #
#                                               #
#################################################

prefix="/usr/local"
manprefix='$(PREFIX)'
#compiler flags
CC="g++"
common_cflags="-Wall"
devel_cflags="-g -Werror -fno-builtin"
optimization_cflags="-O3"
release_cflags=$optimization_cflags
OFLAGS="-ansi"
cflags=$common_cflags

#linker flags

lflags=""

#state vars
debug="no"
devel="no"
parachute="no"
static="no"
LFS="yes"

# cp flags
cpflags="d"

# some stuff for MacOSX -----------------------------
HOSTNAME=`./config.guess`

if [ "${HOSTNAME}" = "powerpc-apple-darwin6.0" ]; then
#compiler flags
CC="c++"
common_cflags="-pedantic -Wall"
#devel_cflags="-g -Werror -fno-builtin"
devel_cflags="-g -fno-builtin"
optimization_cflags="-O0"
release_cflags=$optimization_cflags
cflags=$common_cflags
OFLAGS="-D_MACOSX"
#linker flags
lflags="-x"
#state vars
debug="no"
devel="no"
parachute="no"
static="no"
LFS="yes"

#cp flags macosx does not know about cp -d !
cpflags=""

fi

# IRIX support (thank to Eric Sokolowsky)
if [ "${HOSTNAME}" = "mips-sgi-irix6.5" ]; then
  OFLAGS="";
fi

# cygwin support
if [ "${HOSTNAME}" = "i686-pc-cygwin" ]; then
  OFLAGS="";
fi

#################################################
#                                               #
#        Process command line arguments         #
#                                               #
#################################################




for option in $* ;
do
     case "$option" in
     -debug | --debug) debug="yes";;
	 -man=* | --man=* | -manprefix=* | --manprefix=*) manprefix=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	 -devel | --devel) devel="yes";;
	 -no-LFS | --no-LFS | -no-lfs | --no-lfs) LFS="no";;
	 -stat | -static | --stat | --static) static="yes";;
	 -par | -parachute | --par | --parachute) parachute="yes";;
	 -prefix=* | --prefix=*) prefix=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	 -dist | dist | --distrib | -distrib) rm -f Makefile && cat << EOF >makefile && exit 0;;
# Dummy Makefile, please run ./configure
default:
	@echo ""
	@echo " -----------------------------------------------------------------------------"
	@echo "    Hello !"
	@echo "    I'm afraid I'm a dummy Makefile."
	@echo ""
	@echo " My goal in life is to politely ask you to run the configure script to actual-"
	@echo " ly generate a real Makefile."
	@echo " Would you be kind enough to type \"./configure --help\" to see the options that"
	@echo " will suit your needs ?   Please note that typing \"./configure\" without option"
	@echo " will generate a Makefile that will suit most people needs."
	@echo ""
	@echo " I wish you a good day. Please don't drive to fast."
	@echo " -----------------------------------------------------------------------------"
	@echo ""
EOF
	 -h | -help | --help) cat << EOF && exit 0;;

This dumb piece of script is NOT the standard GNU configure script.
Use this to generate a Makefile that suits your needs.

The default target is to use shared libraries, disable builtin parachute,
enable Large File support (i.e. files > 2/4 Go) whenever possible, disable
debugging facility and install mpgtx with prefix /usr/local

Options you can use to change this dramatically standard behaviour are :
     --no-LFS         Do not try to include Large File support.
     --static         Link statically. Usefull if you plan to use mpgtx on
                      the multiple systems with different libraries.
     --parachute      Enable a builtin parachute that can catch segmentation
                      faults and tell the user who to call and what to tell.
     --manprefix=MPTH Set the manual page installation directory to a different
                      path from the regular installation directory.
     --devel          Add some debugger facility and disable optimizations.
     --debug          Same as above but changes mpgtx output to something
                      ugly and usefull.
     --prefix=PREFIX  Set the installation directory to PREFIX.

     --distrib        Try to make the source files distribution ready.
                      You can not build anything with this option

EOF
     *)    echo "the option [$option] is not known try \"configure --help\" for known options."; exit 1;
     esac
done


#################################################
#                                               #
#        Check some things here                 #
#                                               #
#################################################

# test if compiler is gcc version 3.0 or 3.1
# if so change optimization flags to -O2
# they do not seem to like -O3 with mpgtx very much

gcc_major=`gcc --version 2>&1 | head -n1 | sed 's/^[^0-9]*//' | cut -d. -f1`
gcc_minor=`gcc --version 2>&1 | head -n1 | sed 's/^[^0-9]*//' | cut -d. -f2`

if test "$gcc_major" = "3"; then
    if test "$gcc_minor" = "0" -o "$gcc_minor" = "1"; then
	optimization_cflags="-O2";
    fi
fi

# Now check if the system handles large file support
# unless user has specified not to do so
if test $LFS = "yes"; then
	echo -n  "Checking Large File Support ... ";
	cat << EOF >__LFStest.cpp
		#include <stdio.h>
		int main(){
		return (int)(fseeko(stdin,0,SEEK_SET));
		}
EOF
	$CC __LFStest.cpp -o __LFStestPASSED -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 1>__LFSout 2>__LFSerror
	if test -f __LFStestPASSED; then
		echo "PASSED";
		cflags="$cflags -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE";
	else
		echo "FAILED";
		LFS="no (not supported)"
	fi 
	rm -rf __LFStest.cpp __LFStestPASSED __LFSerror __LFSout;
fi


#################################################
#                                               #
#        Format CFLAGS and LFAGS                #
#                                               #
#################################################


if test  $parachute = "no"; then
	cflags="$cflags -DNOSIGNAL_H";
fi


if test $debug = "yes"; then
	if test $devel = "yes"; then
		cflags="$cflags -D_DEBUG_";
	else
		cflags="$cflags $devel_cflags -D_DEBUG_";
	fi
fi

if test $devel = "yes"; then
	cflags="$cflags $devel_cflags";
fi

if test $static = "yes"; then
	lflags="$lflags -static";
fi

if test $devel = "no" -a $debug = "no"; then
lflags="$lflags -s";
cflags="$cflags $optimization_cflags";
fi




#################################################
#                                               #
#        Actually generate makefile             #
#                                               #
#################################################

makefilename="Makefile"

echo "Generating $makefilename ..."
# remove dumb makefile
rm -f makefile
# generate actual Makefile
cat << EOF >$makefilename
###################################################
# Hi.                                             #
# This file was generated by the configure script #
# but you may want to edit it at your convenience #
# If unsure, try a ./configure --help to see if   #
# standard options suit your needs.               #
# Have a nice day.                        laureck #
###################################################

# Installation directories. Edit at your convenience

EOF

echo "PREFIX=$prefix" >> $makefilename
echo "OFLAGS=$OFLAGS" >> $makefilename
echo "manprefix=$manprefix" >> $makefilename
echo "cpflags=$cpflags" >> $makefilename

cat << EOF >>$makefilename
INSTALLDIR=\$(PREFIX)/bin
MANDIR=\$(manprefix)/man/man1
MANDIRDE=\$(manprefix)/man/de/man1
# Compilation and linker flags

EOF

echo "CFLAGS=$cflags" >> $makefilename
echo "LFLAGS=$lflags" >> $makefilename

cat << EOF >>$makefilename

# Now onto the targets

default: warn mpgtx
	@echo "----------------------------------------------------------------------"
	@echo "Success building mpgtx. Now type \"make install\" to install it"
	@echo "----------------------------------------------------------------------"

warn:
	@echo "----------------------------------------------------------------------"
	@echo "I'm building mpgtx with following options :"
	@echo ""
EOF

if test $devel = "no" -a $debug = "no"; then
		echo -e "\t@echo \"    Target                    : RELEASE\"" >> $makefilename
else
	if test $debug = "yes"; then
		echo -e "\t@echo \"    Target                    : DEBUG\"" >> $makefilename
	else
		echo -e "\t@echo \"    Target                    : DEVELOPMENT\"" >> $makefilename
	fi
fi

echo -e "\t@echo \"    Manual installation prefix: $manprefix\"" >>$makefilename
echo -e "\t@echo \"    Large File support        : $LFS\"" >>$makefilename
echo -e "\t@echo \"    Link statically           : $static\"" >>$makefilename
echo -e "\t@echo \"    Support builtin parachute : $parachute\"" >>$makefilename
echo -e "\t@echo \"    Install prefix            : $prefix\"" >>$makefilename

cat << EOF >>$makefilename
	@echo ""
	@echo "Type \"./configure --help\" to see available options."
	@echo "----------------------------------------------------------------------"
	@echo ""

mpgtx : commandline.cxx mpegOut.o mpeg.o chunkTab.o id3command.o common.hh
	$CC \$(CFLAGS) \$(OFLAGS) -o mpgtx commandline.cxx mpegOut.o mpeg.o chunkTab.o id3command.o \$(LFLAGS)
mpegOut.o : mpegOut.cxx mpegOut.hh mpeg.hh common.hh
	$CC \$(CFLAGS) \$(OFLAGS) -c mpegOut.cxx 
mpeg.o : mpeg.cxx mpeg.hh mpegOut.hh common.hh
	$CC \$(CFLAGS) \$(OFLAGS) -c mpeg.cxx 
chunkTab.o : chunkTab.cxx chunkTab.hh common.hh
	$CC \$(CFLAGS) \$(OFLAGS) -c chunkTab.cxx
id3command.o : id3command.cxx id3command.hh common.hh
	$CC \$(CFLAGS) \$(OFLAGS) -c id3command.cxx



clean :
	rm -f *.o mpgtx mpgjoin mpgcat mpgsplit mpginfo mpgdemux tagmp3
	cd man ; make clean
	cd man/de  ; make clean


install:
	ln -sf mpgtx mpgjoin
	ln -sf mpgtx mpgsplit
	ln -sf mpgtx mpgcat
	ln -sf mpgtx mpginfo
	ln -sf mpgtx mpgdemux
	ln -sf mpgtx tagmp3
	install -d -m 755  \$(INSTALLDIR)
	install -d -m 755  \$(MANDIR)
	install -d -m 755  \$(MANDIRDE)
	install -s -m 755 mpgtx \$(INSTALLDIR)
	cp -\$(cpflags)f mpgdemux mpgjoin mpgcat mpginfo mpgsplit tagmp3 \$(INSTALLDIR)
	install -m 644 man/mpgtx.1 man/tagmp3.1 \$(MANDIR)
	install -m 644 man/de/mpgtx.1 man/de/tagmp3.1 \$(MANDIRDE)
	cd man ; make
	cd man/de ; make
	cp -\$(cpflags)f man/mpgdemux.1 man/mpgjoin.1 man/mpgsplit.1 man/mpgcat.1 man/mpginfo.1 \$(MANDIR)
	cp -\$(cpflags)f man/de/mpgdemux.1 man/de/mpgjoin.1 man/de/mpgsplit.1 man/de/mpgcat.1 man/de/mpginfo.1 \$(MANDIRDE)


uninstall:
	rm -f	\$(INSTALLDIR)/mpgtx
	rm -f	\$(INSTALLDIR)/mpgjoin
	rm -f	\$(INSTALLDIR)/mpgsplit
	rm -f	\$(INSTALLDIR)/mpgcat
	rm -f	\$(INSTALLDIR)/mpginfo
	rm -f	\$(INSTALLDIR)/mpgdemux
	rm -f	\$(INSTALLDIR)/tagmp3

	rm -f	\$(MANDIR)/mpgtx.1
	rm -f	\$(MANDIR)/mpgjoin.1
	rm -f	\$(MANDIR)/mpgsplit.1
	rm -f	\$(MANDIR)/mpgcat.1
	rm -f	\$(MANDIR)/mpginfo.1
	rm -f	\$(MANDIR)/mpgdemux.1
	rm -f	\$(MANDIR)/tagmp3.1

	rm -f	\$(MANDIRDE)/mpgtx.1
	rm -f	\$(MANDIRDE)/mpgjoin.1
	rm -f	\$(MANDIRDE)/mpgsplit.1
	rm -f	\$(MANDIRDE)/mpgcat.1
	rm -f	\$(MANDIRDE)/mpginfo.1
	rm -f	\$(MANDIRDE)/mpgdemux.1
	rm -f	\$(MANDIRDE)/tagmp3.1

EOF

#################################################
#                                               #
#        Tell Mr X what he'll have              #
#                                               #
#################################################

echo ""
echo "Here are the options you choosed : "
if test $devel = "no" -a $debug = "no"; then
		echo "  Target                    : RELEASE"
else
	if test $debug = "yes"; then
		echo "  Target                    : DEBUG"
	else
		echo "  Target                    : DEVELOPMENT"
	fi
fi
echo "  Large File support        : $LFS"
echo "  Link statically           : $static"
echo "  Support builtin parachute : $parachute"
echo "  Install prefix            : $prefix"
echo ""
echo "Now Type \"make\" to build mpgtx (and have a nice day by the way) "

exit 0
