#!/usr/bin/perl -w

# My generic configure script for mod_mp3
# -Brian (brian@tangent.org)

use strict;
use Getopt::Long;
use File::Find;
use File::Path;


(my $REVERSION) = ' $Revision: 1.28 $ ' =~ /\$Revision:\s+([^\s]+)/;
my $VERSION = '0.35';

my @SRC = qw| mod_mp3 directives ice load shout utility ogg common id3 log internal_dispatch encode|;
my $DEF = "";
my $INC = "";
my $LIB = '$(ACFLAGS)';

my @path = qw| /usr/bin /usr/sbin /bin /usr/local/apache/bin /usr/local/bin /usr/local/sbin|;

my ($with_mysql, $with_db, $apxs, $debug, $content_disposition, $perl, $no_select_display);

GetOptions(
	'with-mysql:s' => \&with_mysql,
	'with-db:s' => \&with_db,
	'with-perl=s' => \$perl,
	'with-debug' => \$debug,
	'with-content_disposition' => \$content_disposition,
	'with-apxs:s' => \$apxs,
	'without-select' => \$no_select_display,
	'version' => \&version,
	'help' => \&usage
);

$DEF .= ' -DDEBUG -Wall '
	if $debug;

$DEF .= ' -DCONTENT_DISPOSITION '
	if $content_disposition;

$DEF .= ' -DSELECT_ENABLED '
	unless $no_select_display;


$perl = find_perl($perl);
find_apxs($apxs);

find_php($apxs) 
	if $with_mysql;

print "Writing Makefile\n";
print "Now type:\n";
print "\tmake clean; make; make install\n";
print "\nThanks for using mod_mp3\n";
print "\t -Brian Aker (brian\@tangent.org)\n\n";

print_makefile();
1;

sub usage {
	print <<EOT;

Usage: configure [OPTIONS]

This creates a Makefile for mod_mp3.

Main options:
	-h	Help (this message)
	-v	Version

Dispatch options:
	--with-internal-dispatch (default)
	--with-mysql[=dir]
	--with-postgres[=dir] (not finished)
	--with-bdb[=dir] (not finished)

Display options:
	--without-select

General options:
	--with-apxs=/path/to/apxs
	--with-perl=/path/to/perl
	--with-debug
	--with-content_disposition


EOT
	exit;
}

sub version {
	print <<EOT;

configure $VERSION ()

EOT
	exit;
}

sub with_mysql {
	my ($state, $value) = @_;
	my $mysql_inc = '';
	my $mysql_lib = '';

	$with_mysql = 1;
	print "checking for mysql... ";
	
	if ($value) { 
		if (! -e "$value/include/mysql/mysql.h") {
			print "Error! $value/include/mysql/mysql.h not found\n";
			exit 1;
		}
		if (! -e "$value/lib/libmysqlclient.so") {
			print "Error! $value/lib/libmysqlclient.so not found\n";
			exit 1;
		}
		$INC .= " -I$value/include/mysql/ ";
		$LIB .= " -L$value/lib/ -lmysqlclient -lm ";
		$DEF .= " -DMYSQL_ENABLED ";
		print "found\n";
		push @SRC, 'mysql_dispatch';

		return;
	} 

	if (-e '/usr/include/mysql/mysql.h') {
		$mysql_inc = '/usr/include/mysql';
	}

	if (-e '/usr/lib/libmysqlclient.so') {
		$mysql_lib = '/usr/lib/';
	}

	if (-e '/usr/local/mysql/include/mysql/mysql.h') {
		$mysql_inc = '/usr/local/mysql/include/mysql/';
	}

	if (-e '/usr/local/mysql/lib/libmysqlclient.so') {
		$mysql_lib = '/usr/local/mysql/lib/';
	}

	if (-e '/usr/lib/mysql/libmysqlclient.so') {
		$mysql_lib = '/usr/lib/mysql/';
	}

	if (-e '/usr/local/include/mysql/mysql.h') {
		$mysql_inc = '/usr/local/include/mysql';
	}

	if (-e '/usr/local/lib/libmysqlclient.so') {
		$mysql_lib = '/usr/local/lib/';
	}

	if (-e '/usr/local/include/mysql/mysql.h') {
		$mysql_inc = '/usr/local/include/mysql/';
	}

	if (-e '/usr/include/mysql/mysql.h') {
		$mysql_inc = '/usr/include/mysql/';
	}

	if (-e '/usr/local/lib/mysql/libmysqlclient.so') {
		$mysql_lib = '/usr/local/lib/mysql/';
	}

	#edited by Vidyut luther to allow for NuSphere's default installation
	#of NuSphere MySQL. 
	if (-e '/usr/local/nusphere/mysql/include/mysql.h') {
		$mysql_inc = '/usr/local/nusphere/mysql/include/';
	}

	if (-e '/usr/local/nusphere/mysql/lib/libmysqlclient.a') {
		$mysql_lib = '/usr/local/nusphere/mysql/lib/';
	}

	if ( $mysql_inc && $mysql_lib ) {
		$INC .= " -I$mysql_inc ";
		$LIB .= " -L$mysql_lib -lmysqlclient -lm ";
		$DEF .= " -DMYSQL_ENABLED ";
		print "found\n";
		push @SRC, 'mysql_dispatch';
	} else {
		print "could not find mysql\n";
	}
}

sub with_db {
	my ($state, $value) = @_;
	my $db_inc = '';
	my $db_lib = '';

	$with_db = 1;
	print "checking for db... ";
	
	if ($value) { 
		if (! -e "$value/include/mysql/mysql.h") {
			print "Error! $value/include/mysql/mysql.h not found\n";
			exit 1;
		}
		if (! -e "$value/lib/libdb.so") {
			print "Error! $value/lib/libdb.so not found\n";
			exit 1;
		}
		$INC .= " -I$value/include/ ";
		$LIB .= " -L$value/lib/ -ldb ";
		$DEF .= " -DMYSQL_ENABLED ";
		print "found\n";

		return;
	} 

	if (-e '/usr/include/db.h' && -e '/usr/lib/libdb.so') {
		print "found\n";
		return;
	}

	if (-e '/usr/local/include/db.h') {
		$db_inc = '/usr/local/include';
	}

	if (-e '/usr/local/lib/libdb.so') {
		$db_lib = '/usr/local/lib';
	}

	if ( $db_inc && $db_lib ) {
		$INC .= " -I$db_inc ";
		$LIB .= " -L$db_lib -ldb ";
		print "found\n";
	} else {
		print "could not find db\n";
	}
}


sub find_php {
	my ($value) = @_;
	my $found;
	print "checking for php... ";
	open(APXS, "$value -q LIBEXECDIR |");
	my @command = <APXS>;
	$found = 1
		if (-e "$command[0]/libphp4.so");
	$found = 1
		if (-e "/usr/lib/apache-extramodules/libphp4.so");

	if ($found) {
		print "found\n";
		print <<EOT;
+--------------------------------------------------------------------+
| Found PHP:                                                         |
| You have chosen to install mod_mp3 with PHP. This is fine, but     |
| there is a chance that if you compiled PHP with mysql support      |
| directly then this will cause Apache to core.                      |
+--------------------------------------------------------------------+
EOT

	} else {
		print "not found\n";
	}
	close(APXS);
}

sub find_perl {
	my ($option, $value) = @_;
	print "checking for perl... ";
	
	if ($value) {
		if (-e !"$value") {
			print "not found\n";
			exit 1;
		} 
	} else {
		$value = 'perl';
	}
	print "found\n";

	return $value;
}

sub find_apxs {
	my ($value) = @_;
	print "checking for apxs... ";
	
	if ($value) { 
		if (! -e $value) {
			print "Error! $value not found\n";
			exit 1;
		}
		$apxs = $value;
		print "found\n";

		return;
	} 

	for (@path) {
		if (-e "$_/apxs") {
			$apxs = $_ . '/apxs';
		}
	}


	if ( $apxs ) {
		print "found\n"
	} else {
		print "could not find apxs\n";
		exit 1;
	}
	
}

sub print_makefile {
	my $data = join('', <DATA>);
	my ($src, $obj, $compile);


	for (@SRC) {
		$compile .= qq|\nsrc/$_.o : src/$_.c\n|;
		$compile .= qq|\t\$(CC) \$(INC) -c src/$_.c \$(DEF) -o src/$_.o \n|;
		$src .=" $_.c";
		$obj .=" src/$_.o";
	}

	$data =~ s|%APXS%|$apxs|sg;
	$data =~ s|%VERSION%|$VERSION|sg;
	$data =~ s|%SRC_COMPILE%|$compile|sg;
	$data =~ s|%SRC%|$src|sg;
	$data =~ s|%OBJ%|$obj|sg;
	$data =~ s|%PERL%|$perl|sg;
	$data =~ s|%DEF%|$DEF|sg;
	$data =~ s|%INC%|$INC|sg;
	$data =~ s|%LIB%|$LIB|sg;


	open(MAKE, ">Makefile") or die "Could not open Makefile";
	print MAKE $data;
	close(MAKE);
}

__DATA__
##
##  Makefile -- Build procedure for mod_mp3
##

#   the used tools
APXS=%APXS%
APACHECTL=apachectl
VERSION = %VERSION%
DISTNAME = mod_mp3
DISTVNAME = $(DISTNAME)-$(VERSION)

SRC = %SRC%
OBJ = %OBJ%

SHELL = /bin/sh
PERL = %PERL%
NOOP = $(SHELL) -c true
RM_RF = rm -rf
SUFFIX = .gz
COMPRESS = gzip --best
TAR  = tar
CP  = cp
TARFLAGS = cvf
PREOP = @$(NOOP)
POSTOP = @$(NOOP)
TO_UNIX = @$(NOOP)

AINCLUDEDIR=-I`$(APXS) -q INCLUDEDIR` `$(APXS) -q CFLAGS`
ACFLAGS=-I`$(APXS) -q INCLUDEDIR`
LD_SHLIB=`$(APXS) -q LDFLAGS_SHLIB`
CC=`$(APXS) -q CC`
LD=`$(APXS) -q LD_SHLIB`
CONFDIR=`$(APXS) -q SYSCONFDIR`

DEF = %DEF% 
INC = %INC% $(AINCLUDEDIR) $(DEF)
LIB = %LIB%

#   the default target
all: mod_mp3.so

#   compile the DSO file
mod_mp3.so: $(OBJ)  Makefile
	$(APXS) -c $(LIB) $(OBJ)

%SRC_COMPILE%

#   install the DSO file into the Apache installation
#   and activate it in the Apache configuration
install: all
	$(APXS) -i -a -n 'mp3' src/mod_mp3.so
	if [ -f $(CONFDIR)/mp3.conf ]; then\
		echo "Preserving old mp3.conf"; \
		$(CP) support/mp3.conf $(CONFDIR)/mp3.conf.default; \
	else \
		$(CP) support/mp3.conf $(CONFDIR)/mp3.conf; \
	fi
	@echo "+--------------------------------------------------------+"; \
	echo "| All done.                                              |"; \
	echo "| If you want to use the default mod_mp3 configure file  |"; \
	echo "| go add:                                                |"; \
	echo "|                                                        |"; \
	echo "| Include $(CONFDIR)/mp3.conf                            |"; \
	echo "|                                                        |"; \
	echo "| to your httpd.conf for apache.                         |"; \
	echo "| If not, cat its content into your httpd.conf file.     |"; \
	echo "|                                                        |"; \
	echo "| Thanks for installing mod_mp3.                         |"; \
	echo "+--------------------------------------------------------+"; \



#   cleanup
clean:
	-rm -f $(OBJ) mod_mp3.so

#   simple test
test: reload
	xmms http://127.0.0.1:8000/ &

#   reload the module by installing and restarting Apache
reload: install stop start

#   the general Apache start/restart/stop procedures
start:
	$(APACHECTL) start
restart:
	$(APACHECTL) restart
stop:
	$(APACHECTL) stop

dist: version $(DISTVNAME).tar$(SUFFIX)

version:
	echo $(VERSION) > VERSION
	echo "#define VERSION \"$(VERSION)\"" > src/version.h

$(DISTVNAME).tar$(SUFFIX) : distdir
	$(PREOP)
	$(TO_UNIX)
	$(TAR) $(TARFLAGS) $(DISTVNAME).tar $(DISTVNAME)
	$(RM_RF) $(DISTVNAME)
	$(COMPRESS) $(DISTVNAME).tar
	$(POSTOP)

distdir :
# $(RM_RF) $(DISTVNAME)
	$(PERL) -MExtUtils::Manifest=manicopy,maniread \
	-e "manicopy(maniread(),'$(DISTVNAME)', '$(DIST_CP)');"

docs :
	tpod2html support/faq.pod > faq.html
	pod2text support/faq.pod > faq


push :
	if [ -F *.gz ] ; then \
		rm *.gz; \
	fi
	make dist
	scp *.gz root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/download
	scp *.gz root@ftp.tangent.org:/export/ftp/pub/apache
	scp faq.html root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/faqs/mod_mp3.html

proto :
	$(PERL) utils/protobuild.pl > src/proto.h

