#! /bin/sh

########################################################################
## Create It'sGotTheVibez distribution tarball
########################################################################
## $Id$
## $URL$
########################################################################

set -e
umask 022

########################################################################
## Determine current app version & setup environment
VER=$(cat itsgotthevibez.py | egrep "^VERSION='([^']+)'" | sed -r "s/^VERSION='([^']+)'/\1/")

echo -n "$VER" > itsgotthevibez_distversion.txt

TARGET_DIR="itsgotthevibez_${VER}"
TARGET_TAR="$TARGET_DIR".tar.bz2

########################################################################
## Copy required files to distribution directory
[ -e "$TARGET_TAR" ] && {
		echo "$TARGET_TAR existiert bereits."
		exit 1
}

mkdir "$TARGET_DIR"

cp config.example *.py GPL_V2 changelog "$TARGET_DIR"/

chmod 644 "$TARGET_DIR"/*
chmod +x "$TARGET_DIR"/itsgotthevibez.py

########################################################################
## Build German and English README files

cat > "$TARGET_DIR"/README.TXT <<EOF
************************************************************************
************************************************************************
*** It'sGotTheVibez $VER README
*** (c) 2006-2008 Gunter Ohrner <gunter _(@)_ ohrner.net>
***
*** It'sGotTheVibez is a scrobbler for last.fm, see below for details.
***
*** Licensed under the GNU GPL V2.0, see file GPL_V2 for reference
************************************************************************
************************************************************************


EOF

cat > "$TARGET_DIR"/LIESMICH.TXT <<EOF
************************************************************************
************************************************************************
*** It'sGotTheVibez $VER LIESMICH
*** (c) 2006-2008 Gunter Ohrner <gunter _(@)_ ohrner.net>
***
*** It'sGotTheVibez ist ein Scrobbler für lastfm.de, siehe unten
***
*** Lizensiert unter der GNU GPL -Lizenz 2.0, siehe Datei GPL_V2.
************************************************************************
************************************************************************


EOF

## Adjust HTML page layout.
export BUILD_README=1

PHP_INCLUDEPATH=$(cat php_includepath.txt)

WEBSITE_BASE_URL=http://www.ohrner.net/

BASEPATH=~/misc/prog/hp/

while read PAGEPATH TARGET_FILE
do
	php --define include_path="$PHP_INCLUDEPATH" $BASEPATH$PAGEPATH \
		| elinks -dump 1 -dump-width 72 -force-html \
		| sed -r -e "s!file:///dev/stdin!$WEBSITE_BASE_URL$PAGEPATH!g" \
		-e "s!file:///!$WEBSITE_BASE_URL!g" \
		>> "$TARGET_DIR"/$TARGET_FILE
done <<EOF
software/itsgotthevibez_en.php README.TXT
software/itsgotthevibez_de.php LIESMICH.TXT
EOF

########################################################################
## Create final distribution tarball.
tar cjvf "$TARGET_TAR" "$TARGET_DIR"/

echo "makedist $VER" > call_makedistbat.bat

## Finished. :-)