#!/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.40 $ ' =~ /\$Revision:\s+([^\s]+)/;
my $VERSION = '0.39';

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|;

print STDERR "ARGS @ARGV \n";
my ($playlist, $with_mysql, $with_pgsql, $with_db, $apxs, $debug, $content_disposition, $perl, $no_select_display, $ghttp, $no_directory);

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

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

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

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

unless ($no_directory) {
	if($ghttp = find_ghttp($ghttp)) {
		$DEF .= ' -DYP_ENABLED ';
		push @SRC, 'directory_register';
	}
}

with_db()
	if $playlist && !$with_db;

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

find_php($apxs) 
	if $with_mysql;

if (!$no_directory && $ghttp) {
	print "adding directory services support \n";
}
if ($debug) {
	print "adding debug info \n";
}

print "adding support for internal dispatch\n";
print "adding support for mysql dispatch\n"
	if $with_mysql;
print "adding support for postgres dispatch\n"
	if $with_pgsql;
if ($playlist) {
	print "adding support for playlist dispatch\n"
		if ($with_db == 1);
}
find_php($apxs) 
	if $with_mysql;

print_proto();
print "Writing proto.h\n";
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] (Alpha)
	--with-playlist

Display options:
	--without-select

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


EOT
	exit;
}

sub version {
	print <<EOT;

configure $VERSION ()

EOT
	exit;
}

sub find_ghttp {
	my ($state, $value) = @_;
	my $yp_inc = '';
	my $yp_lib = '';

	print "checking for ghttp for yp... ";
	
	if ($value) { 
		if (! -e "$value/include/ghttp.h") {
			print "Error! $value/include/ghttp.h not found\n";
			exit 1;
		}
		if (! -e "$value/lib/libghttp.so") {
			print "Error! $value/lib/libghttp.so not found\n";
			exit 1;
		}
		$INC .= " -I$value/include/ ";
		$LIB .= " -L$value/lib/ -lghttp ";
		$DEF .= " -DYP_ENABLED ";
		print "found\n";

		return 1;
	} 

	if (-e '/usr/include/ghttp.h') {
		$yp_inc = 1;
	}

	if (-e '/usr/lib/libghttp.so') {
		$yp_lib = 1;
	}

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

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


	if ( $yp_inc && $yp_lib ) {
		$INC .= " -I$yp_inc "
			unless $yp_inc == 1;
		$LIB .= " -L$yp_lib "
			unless $yp_lib == 1;
		$LIB .= " -lghttp ";
		$DEF .= " -DYP_ENABLED ";
		print "found\n";
		return 1;
	} else {
		print "could not find ghttp for yp support\n";
		return 0;
	}
}
sub with_pgsql {
	my ($state, $value) = @_;
	my $pgsql_inc = '';
	my $pgsql_lib = '';

	$with_pgsql = 1;
	print "checking for pgsql... ";
	
	if ($value) { 
		if (! -e "$value/include/pgsql/postgres.h") {
			print "Error! $value/include/pgsql/postgres.h not found\n";
			exit 1;
		}
		if (! -e "$value/lib/libpq.so") {
			print "Error! $value/lib/libpq.so not found\n";
			exit 1;
		}
		$INC .= " -I$value/include/pgsql/ ";
		$LIB .= " -L$value/lib/ -lpq ";
		$DEF .= " -DPGSQL_ENABLED ";
		print "found\n";
		push @SRC, 'pgsql_dispatch';

		return;
	} 

	if (-e '/usr/include/pgsql/postgres.h') {
		$pgsql_inc = '/usr/include/pgsql';
	}

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

	if (-e '/usr/local/pgsql/include/pgsql/postgres.h') {
		$pgsql_inc = '/usr/local/pgsql/include/pgsql/';
	}

	if (-e '/usr/local/mysql/lib/libpq.so') {
		$pgsql_lib = '/usr/local/pgsql/lib/';
	}

	if (-e '/usr/lib/mysql/libpq.so') {
		$pgsql_lib = '/usr/lib/pgsql/';
	}

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

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

	if (-e '/usr/local/lib/mysql/libpq.so') {
		$pgsql_lib = '/usr/local/lib/pgsql/';
	}

	if ( $pgsql_inc && $pgsql_lib ) {
		$INC .= " -I$pgsql_inc ";
		$LIB .= " -L$pgsql_lib -lpq ";
		$DEF .= " -DPGSQL_ENABLED ";
		print "found\n";
		push @SRC, 'pgsql_dispatch';
	} else {
		print "could not find pgsql\n";
	}
}

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/include/db.h') {
		$db_inc = 1;
	}

	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 (-e '/usr/lib/libdb.so.2') {
		$db_lib = 1;
	}

	if ( $db_inc && $db_lib ) {
		$INC .= " -I$db_inc "
			unless $db_inc == 1;
		$LIB .= " -L$db_lib "
			unless $db_inc == 1;
		$LIB .= " -ldb ";
		print "found\n";
		$DEF .= " -DPLAYLIST_ENABLED ";
		push @SRC, 'playlist_dispatch';
	} else {
		print "could not find db\n";
		$with_db = -1;
	}
}


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_proto {
	my $data;

	for my $file (@SRC) {
		my @array = `grep MP3_EXPORT src/$file.c`;

		for (@array) {
			s/^.*://; 
			s/^MP3_EXPORT\((.+)/$1/; 
			s/\)//; 
			s/ {$//; 
			s/{
$//; 
			s/$/;/;
			$data .= $_;
		}
	}

	open(PROTO, ">src/proto.h") or die "Could not open src/proto.h";
print PROTO <<EOF;
/*
 * Prototypes
 */

EOF

	print PROTO $data;
	close(PROTO);
}

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)

rpm: #all dist
	cp $(DISTVNAME).tar$(SUFFIX) /usr/src/redhat/SOURCES
	rpm -ba $(DISTNAME).spec
	cp /usr/src/redhat/RPMS/i386/$(DISTVNAME)*.i386.rpm .
	cp /usr/src/redhat/SRPMS/$(DISTVNAME)*.src.rpm .

version:
	echo $(VERSION) > VERSION
	echo "#define VERSION \"$(VERSION)\"" > src/version.h
	cat mod_mp3.spec | $(PERL) -e 'while (<STDIN>) { push @file, $$_} open(FILE, ">mod_mp3.spec"); for(@file) { if (/^Version/) { print FILE "Version: ${VERSION}\n" } else { print FILE $$_}}'

$(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 $(DISTVNAME).tar$(SUFFIX) root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/download
	scp $(DISTVNAME).tar$(SUFFIX) root@ftp.tangent.org:/var/ftp/pub/apache
	scp $(DISTVNAME)*.i386.rpm root@www.tangent.org:/usr/local/slash/site/www.tangent.org/htdocs/download
	scp $(DISTVNAME)*i386.rpm root@ftp.tangent.org:/var/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

