#!/bin/bash

# Generates an altered version of XMLTV with a gui on the configuration process
# and then packs it using PAR into a self-contained executable for use with
# FreeGuide.

# You should already have updated the ~/code/xmltv-tkgui directory to merge
# the tkgui stuff with the current release, and then run the generate_patch
# script to generate tkgui.diff, which is used to patch the xmltv release
# source code before it is compiled with PAR.

echo "Creating patched XMLTV..."

# Find out the version of XMLTV we're working with
XMLTV_VERSION=xmltv-0.5.34
MODIFIED_VERSION=$XMLTV_VERSION-tkgui-bleb-1

# Remove any old directories
rm -rf $XMLTV_VERSION
rm -rf $MODIFIED_VERSION

# Untar the vanilla XMLTV source
tar -xjf $XMLTV_VERSION.tar.bz2 

# Rename the directory
mv $XMLTV_VERSION $MODIFIED_VERSION

# Patch the sources in the directory, remove unneeded file and add the new files
./addguipatch $MODIFIED_VERSION

# Tar up the patched sources for distribution
rm -rf $MODIFIED_VERSION.tar.bz2
tar -cjf $MODIFIED_VERSION.tar.bz2 $MODIFIED_VERSION

if [ "$1" != "--make-par" ]; then

	exit
	
fi

echo "Making PAR file..."

# Go into the new patched directory
cd $XMLTV_VERSION-tkgui

echo "Compiling the patched sources..."

# Compile it
echo | perl Makefile.PL
make

# Make a PAR file containing all the XMLTV scripts (still needs perl I think)
pp -p -o xmltv.par grab/de/tv_grab_de grab/dk/tv_grab_dk grab/es/tv_grab_es grab/fi/tv_grab_fi grab/hu/tv_grab_hu grab/it/tv_grab_it grab/na/tv_grab_na grab/nl/tv_grab_nl grab/uk/tv_grab_uk

# Alternative cross-platform version (exe_wrap needs modifying)
#pp -B --multiarch -o xmltv.par lib/exe_wrap.pl

echo "Removing main.pl..."

mkdir xmltv_par
cd xmltv_par

unzip -q ../xmltv.par

rm -f script/main.pl

cat MANIFEST | grep --invert-match script\/main.pl > MANIFEST.NEW

rm -f MANIFEST
mv MANIFEST.NEW MANIFEST

rm -f ../xmltv.par
zip -rq ../xmltv.par *

cd ..

rm -rf xmltv_par

echo "Creating executable..."

pp -o xmltv xmltv.par

mv -f xmltv.par xmltv ../

cd ..

rm -rf $XMLTV_VERSION-tkgui
rm -f xmltv.par


