#!/bin/sh

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

prefix="/usr/local"

#compiler flags

common_cflags="-pedantic -Wall"
devel_cflags="-g -Werror -fno-builtin"
optimization_cflags="-O3"
release_cflags=$optimization_cflags

cflags=$common_cflags

#linker flags

lflags=""

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



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




for option in $* ; 
do 
     case "$option" in
     -debug | --debug) debug="yes";;
	 -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.
     --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
# if so change optimization flags to -O2 
# gcc 3 does not seem to like -O3 with mpgtx very much 

# next line is from Chris Danis
gcc_major=`gcc --version | cut -b 1`

if test $gcc_major = "3"; then
	optimization_cflags="-O2";
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
	g++ __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

cat << EOF >>$makefilename
INSTALLDIR=\$(PREFIX)/bin
MANDIR=\$(PREFIX)/man/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 \"    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
	g++ \$(CFLAGS) -ansi -o mpgtx commandline.cxx mpegOut.o mpeg.o chunkTab.o id3command.o \$(LFLAGS)
mpegOut.o : mpegOut.cxx mpegOut.hh mpeg.hh common.hh
	g++ \$(CFLAGS) -ansi -c mpegOut.cxx 
mpeg.o : mpeg.cxx mpegOut.hh common.hh
	g++ \$(CFLAGS) -ansi -c mpeg.cxx 
chunkTab.o : chunkTab.cxx common.hh
	g++ \$(CFLAGS) -ansi -c chunkTab.cxx
id3command.o : id3command.cxx common.hh
	g++ \$(CFLAGS) -c id3command.cxx



clean :
	rm -f *.o mpgtx mpgjoin mpgcat mpgsplit mpginfo mpgdemux tagmp3 man/mpginfo.1 man/mpgsplit.1 man/mpgcat.1 man/mpgjoin.1 man/mpgdemux.1


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 -s -m 755 mpgtx \$(INSTALLDIR)
	cp -df mpgdemux mpgjoin mpgcat mpginfo mpgsplit tagmp3 \$(INSTALLDIR)
	install -m 644 man/mpgtx.1 man/tagmp3.1 \$(MANDIR) 
	cd man ; make
	cd ..
	cp -df ./man/mpgdemux.1 ./man/mpgjoin.1 man/mpgsplit.1 man/mpgcat.1 man/mpginfo.1 \$(MANDIR)


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

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
