#!/bin/sh
##########################################################################
# Build-Script for Midgard Packages
# ---------------------------------
# Author: Torben Nehmer, Linksystem Muenchen <t.nehmer@buero.link-m.de>
# $Id: yamp,v 1.8 2002/08/21 16:12:24 philipp Exp $
#
#
# This Script is used to export Midgard snippets from a Development 
#   Database and to create the distribution	Packages.
#
# SYNTAX:
#   build configfile command
#
# COMMANDS:
# xml: Builds the .xml.gz's.
# package: Builds midhoo-release.tar.gz 
# all: Launches xml, cvs and package per default, can be customized
#      in ALL_COMANDS
#
# cvs: Lauches a cvs commit after updateing the XML Files
# clean: Clean up the source tree including any package files.
#        This should leave only the cvs files.
#
# CVS support: The Makefile requires that cvs requires no argument
#	to access the repository. Ensure that the environment is 
#	correct.
#
# Naming conventions - for a package <package> these files will be used:
# <package>.xml is the XML File in the repository. Will be generated
#	out of the <package>.xml.gz export from repligard.
# <package>.conf is the Config File in the repository. This file has to
#	make repligard export the desired xml.gz File.
#
# The Package command uses the xml Files specified above and the 
#   files in OTHER_FILES to create a PACKAGE_NAME-VERSION.tar.bz2
#
# These Parameters are now all defined in the config file. See Example.
#
# LICENCE:
#
# (C) Torben Nehmer, Linksystem Muenchen <t.nehmer@buero.link-m.de> 2001
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
# 
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Library General Public License for more details.
# 
#  If you do not have access to a copy of the GNU Library General Public 
#  License already, write to the Free Software Foundation, Inc., 59 Temple 
#  Place - Suite 330, Boston, MA 02111-1307, USA.
#
##########################################################################
# INTERNAL CONFIGURATION VARIABLES
#
# The Version is used to keep the config file versions in sync.
YAMP_VERSION="1.0.1"
YAMP_CONFIG_VERSION=1

# Load Library Components
INSERT_LOGIN_PL="/usr/local/bin/insert_login.pl"

# Set this paths to represent the state on your system
BZIP2="bzip2"
GZIP="gzip"
BUNZIP2="bunzip2"
GUNZIP="gunzip"
TAR="tar"

##########################################################################
# HELPER FUNCTION insert_login
#
# Calls external Perl Script insert_login.pl to insert the login 
# information for a specified user. 
# 
# example: insert_login xmlfile guid user password
# 
insert_login () {
	if [ $# != 4 ]; then
		echo "YAMP $YAMP_VERSION ERROR:"
		echo "Usage: insert_login guid user password"
		exit 127;
	fi 
	if [ ! -f $INSERT_LOGIN_PL ]; then
		echo "YAMP $YAMP_VERSION ERROR:"
		echo "insert_login: $INSERT_LOGIN_PL not found!"
		exit 127;
	fi
	if [ ! -f $1 ]; then
		echo "YAMP $YAMP_VERSION ERROR:"
		echo "insert_login: $1 does not exist!"
		echo "We are in: " `pwd`
		exit 4;
	fi

	cat $1 | $INSERT_LOGIN_PL $2 $3 $4 > $1.new
	mv -f $1.new $1
}

##########################################################################
# HELPER FUNCTION extract
#
# Default operation to process an xml.gz File:
# - Extract it
# - Insert CVS Header after the first line
#
# example: Use "extract somefile.xml" to process somefile.xml.gz 
#   You have to omit the gz extension!
#
# Don't bother with the really scary sed replacement. It just inserts
# the CVS Id Tag into the file. Since CVS does not know selective 
# keyword expansion I had to find a way to prevent this expansion.
#
extract() {
	if [ $# != 1 ]; then
		echo "YAMP $YAMP_VERSION ERROR:"
		echo "Usage: extract-default {xml-gz file without the .gz extension}"
		exit 127
	fi
	
	rm -f $1
	gunzip -c $1.gz | sed 's/<Database/<!-- \$I --><Database/' | sed 's/ --><Database /d\$ -->\
<Database /' > $1

	rm -f $1.gz
	
	# call customizer
	extractcustomop $1
}

##########################################################################
# HELPER FUNCTION help
#
# Prints the help message
#
# Currently disabled:
# cvs    : Lauches a cvs commit after updateing the XML Files
#
help() {
cat << EOF
Yet Another Midgard Package Tool, Version $YAMP_VERSION
(C) Torben Nehmer, Linksystem Muenchen <t.nehmer@buero.link-m.de> 2001

SYNTAX: yamp configfile command

COMMANDS:
 xml    : Builds the .xml.gz files.
 package: Builds midhoo-release.tar.gz 
 all    : Launches xml, cvs and package per default, can be customized
          in ALL_COMANDS

 clean  : Clean up the source tree including any package files.
          This should leave only the cvs files.
EOF
}

		
################
### BEGIN Skript
################

################
# Error Checking
#

if [ $# != 2 ]; then
	help
	exit 1
fi

if [ ! -f $1 ]; then
	echo "YAMP $YAMP_VERSION ERROR:"
	echo Configuration file does not exist!
	exit 2
fi

# Source Config File
. $1

if [ $CONFIG_VERSION != $YAMP_CONFIG_VERSION ]; then
	echo "YAMP $YAMP_VERSION ERROR:"
	echo "Configuration file Version incorrect";
	exit 3
fi



case "$2" in

##########################################################################
# COMMAND xml
#
# Creates/Updates the .xml files
#
	xml)
for i in $PACKAGES
do 
	$REPLIGARD $REPLIGARD_PARAM $REPLIGARD_CONFIG $i.conf -e $i.xml.gz
	extract $i.xml
done 
	;;


##########################################################################
# COMMAND package
#
# Creates the Package from the current tree.
#
	package)
mkdir $PACKAGE_NAME-$VERSION
for i in $PACKAGES 
do
	$GZIP -c $i.xml > $PACKAGE_NAME-$VERSION/$i.xml.gz
done
for i in $OTHER_FILES
do
	cp $i $PACKAGE_NAME-$VERSION
done
$TAR -cvjf $PACKAGE_NAME-$VERSION.tar.bz2 $PACKAGE_NAME-$VERSION
rm -rf $PACKAGE_NAME-$VERSION
	;;


##########################################################################
# COMMAND all
#
# Builds everything, as defined in ALL_COMMANDS
#
	all)
for i in $ALL_COMMANDS
do
	$0 $1 $i
done
	;;


##########################################################################
# COMMAND cvs 
#
# Do a commit
#
#	cvs)
# $CVS commit
# ...Not yet implemented...
#	;;
 

##########################################################################
# COMMAND clean
#
# Cleans up the repository from build-scrap. Shouldn't happen though,
# because the other commands clean up after they've run. Just to be
# complete.
#
	clean)
for i in $PACKAGES 
do
	rm -f $i.xml.gz $i.xml
done
rm -rf $PACKAGE_NAME-$VERSION*
	;;


##########################################################################
# COMMAND Help Message
#
# Prints the help message in case of error.
#
	*)
help
return 1
	;;
esac
