#!/bin/sh
##############################################################
#	build_e17 version 2.1.0 2007-06-08
# 
# Anders Trobäck <enlightenment@troback.com>
#
# This script will build Enlightenment R17 (E17) from CVS on
# FreeBSD.
#
# The script needs (all are in ports): 
# gmake, gnu-libtool, gnu-autoconf and gnu-automake to run
# Enlightenment needs curl, libidn and jpeg to run
#	and the mail module needs opnssl.
#
# Tested on FreeBSD 5.4, 6.0, 6.1 and 6.2.
#
##############################################################

# Where to install e17
INSTALLDIR="/usr/devel/enlightenment/current"

# Some settings
PATH="$PATH:/usr/local/gnu-autotools/bin:$INSTALLDIR/bin"
MAKEBIN="gmake"
LDCONFIGBIN="ldconfig -m"
export ACLOCAL_FLAGS="-I /usr/local/share/aclocal"
export PKG_CONFIG_PATH="$INSTALLDIR/lib/pkgconfig:/usr/local/libdata/pkgconfig"

# What to install
LIBS="\
e17/libs/eet \
e17/libs/edb \
e17/libs/evas \
e17/libs/ecore \
e17/libs/efreet \
e17/libs/embryo \
e17/libs/imlib2 \
e17/libs/edje \
e17/libs/epeg  \
e17/libs/epsilon \
e17/libs/esmart \
e17/libs/emotion \
e17/libs/engrave \
e17/libs/ewl \
e17/libs/imlib2_loaders \
e17/libs/etk \
e17/libs/exml \
eterm/libast \
"

APPS="\
e17/apps/e \
e17/apps/e_utils \
e17/apps/iconbar \
e17/apps/entice \
e17/apps/entrance \
e17/apps/elicit \
e17/apps/examine \
e17/apps/imlib2_tools \
misc/ebrace \
misc/equate \
misc/engage \
e17/apps/evfs \
e17/proto/entropy \
e17/proto/exhibit \
"

MODS="\
e_modules/bling \
e_modules/cpu \
e_modules/deskshow \
e_modules/devian \
e_modules/emu \
e_modules/flame \
e_modules/language \
e_modules/mail \
e_modules/mem \
e_modules/net \
e_modules/rain \
e_modules/screenshot \
e_modules/slideshow \
e_modules/snow \
e_modules/taskbar \
e_modules/tclock \
e_modules/uptime \
e_modules/weather \
e_modules/winselector \
e_modules/wlan \
"


WM="\
e17/libs/eet \
e17/libs/evas \
e17/libs/ecore \
e17/libs/embryo \
e17/libs/edje \
e17/libs/efreet \
e17/apps/e \
"

###############################
# Do not edit below this line #
###############################

## Global variables ##

ROOTDIR=`pwd`
CVS_BASE=$ROOTDIR/cvs
CVSSERVER=anoncvs.enlightenment.org
CVSROOT=/var/cvs/e
CLEANONLY=""	
MYLDFLAGS="$LDFLAGS -L$INSTALLDIR/lib -L/lib -L/usr/lib -L/usr/local/lib -L/usr/X11R6/lib"
MYCPPFLAGS="$CPPFLAGS -I$INSTALLDIR/include -I/usr/include -I/usr/local/include -I/usr/X11R6/include"

## End global variables ##

## Functions ##
cvsco(){
	mkdir -p "$CVS_BASE" 2>/dev/null
	cd $CVS_BASE
	echo "Updating E17 CVS (type return when prompted for password)"
	cvs -d:pserver:anonymous@$CVSSERVER:$CVSROOT login
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT co e17
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT co e_modules
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT co misc
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT co eterm
	cvs -d:pserver:anonymous@$CVSSERVER:$CVSROOT logout
	exit 0
}

cvsup(){
	mkdir -p "$CVS_BASE" 2>/dev/null
	cd $CVS_BASE
	echo "Updating E17 CVS (type return when prompted for password)"
	cvs -d:pserver:anonymous@$CVSSERVER:$CVSROOT login
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT up e17	
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT up e_modules
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT up misc
	cvs -z3 -d:pserver:anonymous@$CVSSERVER:$CVSROOT up eterm
	cvs -d:pserver:anonymous@$CVSSERVER:$CVSROOT logout
	exit 0
}

remove(){
	echo "Warning !!! Will remove sources and installed binaries !"
	echo "Press CRTL-C now to abort or press enter to continue."
	read INPUT
	rm -rf e17
	rm -rf e_modules
	rm -rf misc
	rm -rf eterm
	rm -rf $INSTALLDIR
	echo "done."
	exit 0
}

clean(){
	TOBUILD="$LIBS $APPS $MODS"
	CLEANONLY="YES"	
}

usage(){
		echo "Usage: $0 <cvsco|cvsup|libs|apps|mods|wm|all|remove|clean||select path_in_cvs"
		echo
		echo "EXAMPLES"
		echo "To check out all code from CVS and build everything use:"
		echo "  build_17 cvsco"
		echo "  build_17 all"
		echo "If you only want the window manager run (after a cvsco or cvsup):"
		echo "  build_17 wm"
		echo "To update just the modules after a previous build use:"
		echo "  build_e17 cvsup"
		echo "  build_e17 mods"
		echo "To build a selected lib, app or maybe a module:"
		echo "  build_e17 select e17/apps/e"
	exit 1
}

build(){
echo "**********************"
echo "Have to process $TOBUILD"
echo "**********************"
for i in $TOBUILD
do
	echo "**********************"
	echo "Processing $i"
	echo "**********************"

	cd $CVS_BASE/$i
	if [ "$CLEANONLY" = "" ]
	then
		env LDFLAGS="$MYLDFLAGS" CPPFLAGS="$MYCPPFLAGS" ./autogen.sh
	fi

	$MAKEBIN clean

	if [ "$CLEANONLY" = "" ]
	then
		#Configure options
		CONFIGOPTIONS=""
		# Patch for Etk
		if [ "$i" = "e17/libs/etk" ]
		then
			MYCFLAGS="-I/usr/local/include -L/usr/local/lib -I/usr/X11R6/include -L/usr/X11R6/lib -lintl"
		fi
		# Patch for Evas
		if [ "$i" = "e17/libs/evas" ]
		then
			MYCPPFLAGS="$MYCPPFLAGS -I/usr/local/include/cario"
		fi
		env CFLAGS="$MYCFLAGS" LDFLAGS="$MYLDFLAGS" CPPFLAGS="$MYCPPFLAGS" \
			./configure --prefix=$INSTALLDIR $CONFIGOPTIONS && \
			$MAKEBIN && $MAKEBIN install

		if [ $? -ne 0 ]
		then
			echo "Error building $i !" >> $ROOTDIR/errors
		fi
	fi
done

}

## End functions ##

## Main program ##

if [ $# -lt 1 -o $# -gt 2 ]
then 
	usage
fi

[ -e $ROOTDIR/errors ] && rm $ROOTDIR/errors

case "$1" in
	cvsco)
		cvsco
	;;
	cvsup)
		cvsup
	;;
	libs)
		TOBUILD="$LIBS"
		build
	;;
	apps)
		TOBUILD="$APPS"
		build
	;;
	mods)
		TOBUILD="$MODS"
		build
	;;
	wm)
		TOBUILD="$WM"
		build
	;;
	all)
		TOBUILD="$LIBS $APPS $MODS"
		build
		echo "All done. Don't forget to update your ldconfig_paths in /etc/rc.conf, add $INSTALLDIR/lib !"
	;;
	remove)
		remove
	;;
	clean)
		TOBUILD="$LIBS $APPS $MODS"
		CLEANONLY="YES"	
	;;
	select)
		if [ $# -ne 2 ]
		then
			echo "You must specify one or several things to build."
			echo "e.g. : $0 select e17/libs/imlib2"
			echo "e.g. : $0 select \"e17/apps/entice e17/apps/e\""
			exit 1
		fi
		TOBUILD=$2
		build
	;;
	*)
		usage
	;;
esac

[ -e $ROOTDIR/errors ] && cat $ROOTDIR/errors
exit 0

## End main program ##
