#!/usr/bin/perl

use warnings ;
use strict ;

## Settings
my $configure_opts="--prefix=/usr --with-external-ffmpeg --enable-vdpau --disable-dxr3" ;
my $custom_patches="enable" ; # enable or empty see section # custom patches to xine-lib

 # get latest hg xine 1.2 and vdpau patch, patch xine-lib , configure and make , afterwards script make install as root


unless ( -d "./xine-vdpau" ) { mkdir "./xine-vdpau" } ;
chdir "./xine-vdpau" ;


# get latest vdpau patch name
system ("wget http://www.jusst.de/vdpau/files/xine-lib-1.2/") ;
my @latest_patch ;
if ( -f "./index.html" ) {
	open FH , "./index.html" ;
	while ( my $line = <FH> ) {
		if ( $line =~ /(xine-lib-1\.2-vdpau-r\d+\.diff\.bz2)/ ) { push @latest_patch , $1 }
	}
	close FH ;
unlink "./index.html" ;
}

# get latest vdpau patch and bunzip

unless ( -f  "$latest_patch[$#latest_patch]" ) {
	system ("wget http://www.jusst.de/vdpau/files/xine-lib-1.2/$latest_patch[$#latest_patch]") ;
	system ("bunzip2 $latest_patch[$#latest_patch]") ;
}
( my $vdpau_patch  = $latest_patch[$#latest_patch] ) =~ s/\.bz2// ;
undef @latest_patch ;
 print "latest vdpau patch : $vdpau_patch\n" ;

# get HG xine
print "checkout xine-lib HG\n" ;
system ("rm -rf ./xine-lib-1.2") ;
system ("hg clone http://hg.debian.org/hg/xine-lib/xine-lib-1.2") ;
chdir  "xine-lib-1.2" ;

# patch xine
system ("patch -p1 -i ../$vdpau_patch") ;

# custom patches to xine-lib
if ( $custom_patches eq "enable") {
# 	my gcc have no -Werror-implicit-function-declaration Flag ( dont no why this fails )
#  	patch out : CC_CHECK_CFLAGS([-Werror-implicit-function-declaration], [warnflags="$warnflags -Werror-implicit-function-declaration"])
	print "patching configure.ac \"no -Werror-implicit-function-declaration\"\n" ;
	open FH , "<./configure.ac" ;
	my @cac = <FH> ;
	close FH ;
	rename "./configure.ac" , "./configure.ac.bak" ;
	open FH , ">./configure.ac" ;
	foreach ( @cac ) {
		next if ( $_ =~/CC_CHECK_CFLAGS\(\[-Werror-implicit-function-declaration\], \[warnflags=\"\$warnflags -Werror-implicit-function-declaration\"\]\)/ ) ;
		print FH "$_" ;
	}
	close FH ;
}

# configure xine
print "configure xine-lib\n" ;
system ("./autogen.sh $configure_opts") ;
 # make xine

print "compile xine-lib\n" ;
system ("make") ;

# make install
print "make install xine-lib\n" ;
$|++;
print "enter root password at the prompt below to make install:$/";
my $root_out = `su root -c "make install"`;
$|--;



