#!/bin/sh
#
# debuild.sh
#
# Copyright (c) 2002-2003 The Debian Book Team
# http://debian-book.fccf.net/AUTHORS
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program 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 General Public License for more details.
#
# ChangeLog
#
# 1.00	(xxxx-xx-xx)	George Danchev <danchev@spnet.net>
#			initial code
#
# 1.10  (2003-03-07)	Alexander Velin <velin@zadnik.org>
#			Added md5sum checking
#
# 1.20	(2003-03-11)	Alexander Velin <velin@zadnik.org>
#			md5sum fixup
#			cleanups
#
# 1.30  (2003-03-11)	George Danchev <danchev@spnet.net>
#			minor cleanups
#
# 1.40	(2003-04-05)	Ognyan Kulev <ogi@fmi.uni-sofia.bg>
#			made code more compact
#
# 1.50	(2003-04-07)	Ognyan Kulev <ogi@fmi.uni-sofia.bg>
#			minor cleanups
#
# 1.60	(2003-04-17)	George Danchev <danchev@spnet.net>
#			changed default md5sum
#
# 1.70 (2003-06-20)	Alexander Velin <velin@zadnik.org>
#			restructure
#			merged md5/no-md5 
#			rsync support
#
# 1.71 (2003-07-01)	Ognyan Kulev <ogi@fmi.uni-sofia.bg>
#			terminate on error in any command
#			comment out NOTIFY=
#			send SMS to $NOTIFY only if NOTIFY variable is defined
#			
#
# This scripts is intended to ease the task of automatic (cron'ed) 
# CVS mirroring and building. On first run (which you will do manually) 
# you will be asked for anonymous CVS access password, just hit <Enter>
# and the relevant details will be stored in $HOME/.cvspass file 
#
# Heavily influenced by the old mirror-and-build-md5.sh
#
# NOTE: Always check this script before running it (use regular user, not 'root')
#
#
# Variables
#
# name of this shell script
	NAME=debuild.sh

# the repository to fetch the source from
	REPO=:pserver:anonymous@photo-forum.net:/home/cvsroot

# the name of module to fetch
	MODULE=debian-book

# the path where you intend to store $MODULE. Must be absolute path.
# probably, your webserver DocumentRoot 
	LOCATION=/home/www/zadnik.org/www

# the building dir (in this case it is sub-dir of $MODULE)
	BUILDDIR=src

# present while building new CVS sources
	MSGFILE=BUILDING_NEW_VERSION_PLEASE_WAIT

# email2sms address to notify. Uncomment to use.
# STDOUT of the script should give more details to cron email
	#NOTIFY=3598xnnnnnn@sms.mtel.net
		
# Makefile
	MFILE=$LOCATION/$MODULE/$BUILDDIR/Makefile

# `md5sum debian-book/src/Makefile`
	MD5TRUSTED="3e9db5437a8c54ca83ae9c80a40f4bb0"

# rsync setups
	RSYNC=`which rsync`
	RSERVER=zadnik.org
	ROUTPUT=$LOCATION/$MODULE/
	
set -e

umask 022

# 1 - rsync
# 2 - blind
# 3 - build
# 4 - buildev

case "$1" in
	rsync)
		if [ ! -x $RSYNC ]; then
			echo "rsync binary missing. (maybe apt-get install rsync ?)"
			exit 1
		fi
		if [ ! -d $ROUTPUT ]; then
			echo "$ROUTPUT directory missing, creating"
			mkdir -p $ROUTPUT
		fi
		echo $RSYNC -az -delete $RSERVER::$MODULE $ROUTPUT
		$RSYNC -az --delete $RSERVER::$MODULE $ROUTPUT
		;;

	blind)
		if [ ! -d $LOCATION ]; then
			echo "$LOCATION directory missing, creating"
			mkdir -p $LOCATION
		fi
		
		cd $LOCATION

		if  [ ! -d $MODULE ] ; then
			export CVSROOT=$REPO
			cvs -z3 login
			cvs -z3 co -P $MODULE
		fi

		if  [ -d $MODULE ] ; then
			cd $MODULE/$BUILDDIR
			make clean dists-clean
			cd ..
			touch $MSGFILE
			cvs up -Pd
			mv index.html index-old.html
    
			cd $BUILDDIR
			make dists
			make html htmlsplit pdf
			cd .. 
			rm $MSGFILE
			mv index-old.html index.html
		else 
			echo "Something weird happend while syncing sources from CVS" 
			exit 1
		fi 
		;;

	build)
		if [ ! -d $LOCATION ]; then
			echo "$LOCATION directory missing, creating"
			mkdir -p $LOCATION
		fi
		
		cd $LOCATION

		if  [ ! -d $MODULE ] ; then
			export CVSROOT=$REPO
			cvs -z3 login
			cvs -z3 co -P $MODULE
		fi

		if  [ -d $MODULE ] ; then
			cd $MODULE/$BUILDDIR

			MD5CUR=`md5sum $MFILE | awk '{print $1}'`

			if [ "$MD5CUR" != "$MD5TRUSTED" ] ; then
				echo "Local debian-book Makefile changed. No cleaning made, no cvs up made. Mirror consistent."
				test -n "$NOTIFY" && echo "local debook md5 failed - no cvsup" | mail $NOTIFY
				exit 1
			fi

			make clean dists-clean
			cd ..
			touch $MSGFILE
			cvs up -PdR
			mv index.html index-old.html
    
			MD5CUR=`md5sum $MFILE | awk '{print $1}'`

			if [ "$MD5CUR" != "$MD5TRUSTED" ] ; then
				echo "CVS debian-book Makefile changed. Cleaned, cvs up made, no building made. Mirror broken."
				test -n "$NOTIFY" && echo "cvs debook md5 failed - no building" | mail $NOTIFY
				exit 1
			fi
			
			cd $BUILDDIR
			make dists
			make html htmlsplit pdf

                        cd ..
                        rm $MSGFILE
                        mv index-old.html index.html

		else 
			echo "Something weird happend while syncing sources from CVS" 
			exit 0
		fi 
		;;

	buildev)
		echo "BUILD-DEV not implemented yet."
		;;

	*)
		echo "Usage: $NAME {rsync|blind|build|buildev}"
		exit 1
		;;
esac

