#!/bin/bash
# Copyright (C) 2006  Nathan (Acorn) Pooley
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# 
# 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 (gpl.txt) for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to 
# the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# You can contact the author, Nathan (Acorn) Pooley, by writing
# to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
# or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
# 
#@DOC@ set up to build all software

log=config.log

touch Makefile.cfg
touch $log
mv Makefile.cfg Makefile.cfg.old
mv $log $log.old
_c() { echo "$*" >> Makefile.cfg ; }
echo "#" > Makefile.cfg
date > $log
_c "# DO NOT EDIT THIS FILE"
_c "#"
_c "# File is autogenerated by the configure script"
_c ""

_verb() {
	echo "$*" | tee -a $log >&2
}
_log() {
	echo "$*" >> $log
}
_warn() {
	echo "" | tee -a $log >&2
	echo "###" | tee -a $log >&2
	echo "### WARNING: $*" | tee -a $log >&2
	echo "###" | tee -a $log >&2
	echo "" | tee -a $log >&2
}
_err() {
	echo "" | tee -a $log >&2
	echo "###" | tee -a $log >&2
	echo "### ERROR: $*" | tee -a $log >&2
	echo "###" | tee -a $log >&2
	echo "" | tee -a $log >&2
}
_errcat() {
	echo "" | tee -a $log >&2
	echo "###" | tee -a $log >&2
	cat | sed -e 's|^|### ERROR: ' | tee -a $log >&2
	echo "###" | tee -a $log >&2
	echo "" | tee -a $log >&2
	exit 1
}

_verb "Log file is $log"
_verb ""

_notice() {
	_verb ""
	_verb "Wand Software Version 1.1 Copyright (C) 2006 Nathan (Acorn) Pooley"
	_verb "Wand software comes with ABSOLUTELY NO WARRANTY; for details"
	_verb "see gpl.txt.  This is free software, and you are welcome"
	_verb "to redistribute it under certain conditions; see gpl.txt for"
	_verb "details."
	_verb ""
}

tmp=tmp_config
tmp2=tmp_config2
err=0
\rm -f $tmp $tmp2

#
# check for Linux
#
ver=`uname`
_log "uname = $ver"
if [[ $ver != [Ll][Ii][Nn][Uu][Xx] ]] ; then
	_verb "WARNING: This software has been tested only on Linux and might or"
	_verb "might not work on other operating systems."
	_verb ""
fi

#
# check for sed
#
_log "sed --version || err=1"
sed --version > $tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_notice
	_fatal "This software requires that sed be in the current PATH."
	_verb "Log file is $log"
	exit 1
fi

#
# check for grep
#
_log "grep --version || err=1"
grep --version > $tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_notice
	_fatal "This software requires that grep be in the current PATH."
	_verb "Log file is $log"
	exit 1
fi

#
# check for make
#
\rm -f $tmp $tmp2
touch $tmp $tmp2
_log "make --version || gmake --version || err=1"
make --version > $tmp 2>&1 || gmake --version > $tmp2 2>&1 || err=1
cat $tmp $tmp2 >> $log
if (( ! err )) ; then
	_msg() {
		head $tmp
		head $tmp2
	}
	_msg | grep -i 'GNU *Make' > /dev/null 2>&1 || err=1
fi
if (( err )) ; then
	_notice
	_msg() {
		echo "This software requires gnu make"
		echo "You can download it for free from"
		echo "         http://savannah.gnu.org/projects/make/"
	}
	_msg | _errcat
	_verb "Log file is $log"
	exit 1
fi

#
# check for gcc
#
_log "gcc --version || err=1"
gcc --version > $tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_notice
	_msg() {
		echo "This software requires that gcc (The Gnu C compiler) be in"
		echo "the current PATH."
		echo "You can download gcc for free from"
		echo "      http://gcc.gnu.org/"
	}
	_msg | _errcat
	_verb "Log file is $log"
	exit 1
fi
_verb "found `type gcc`"

#
# check for cpp
#
_log "cpp --version || err=1"
cpp --version > $tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_notice
	_msg() {
		echo "This software requires that cpp (The C preprocessor) be in"
		echo "the current PATH."
	}
	_msg | _errcat
	_verb "Log file is $log"
	exit 1
fi
_verb "found `type cpp`"

#
# check for gputils
#
_log "gpasm --version || err=1"
gpasm --version > $tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_notice
	_msg() {
		echo "This software requires that gpasm be in"
		echo "the current PATH.  The gpasm program is part of the"
		echo "gputils softwafre package.  To get gputils try:"
		echo "      http://gputils.sourceforge.net/"
		echo "On a debian based system (e.q. Ubuntu) try:"
		echo "      apt-get install gputils"
	}
	_msg | _errcat
	_verb "Log file is $log"
	exit 1
fi
_verb "found `type gpasm`"

#
# check for X windows
#
have_glx=1
err=0
\rm -f ctst.c
echo '#include <X11/Xlib.h>' >> ctst.c
echo 'int main(){return 0;}' >> ctst.c
_log "=========== ctst.c"
cat ctst.c >> $log
_log "=========== ctst.c"
_log "gcc -o ctst ctst.c -L/usr/X11R6/lib -lX11 || err=1"
gcc -o ctst ctst.c -L/usr/X11R6/lib -lX11 >/dev/null 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_warn "This software requires X windows to build the wg program"
	have_glx=0
else
	_verb "found Xlib.h"
fi

#
# check for GL 
#
err=0
\rm ctst.c
echo '#include <GL/gl.h>' >> ctst.c
echo 'int main(){return 0;}' >> ctst.c
_log "=========== ctst.c"
cat ctst.c >> $log
_log "=========== ctst.c"
_log "gcc -o ctst ctst.c -L/usr/X11R6/lib -lGL || err=1"
gcc -o ctst ctst.c -L/usr/X11R6/lib -lGL >$tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_warn "This software requires GL to build the wg program"
	have_glx=0
else
	_verb "found gl.h"
fi

#
# check for GLX
#
err=0
\rm ctst.c
echo '#include <GL/glx.h>' >> ctst.c
echo 'int main(){return 0;}' >> ctst.c
_log "=========== ctst.c"
cat ctst.c >> $log
_log "=========== ctst.c"
_log "gcc -o ctst ctst.c -L/usr/X11R6/lib -lGL -lXext -lX11 || err=1"
gcc -o ctst ctst.c -L/usr/X11R6/lib -lGL -lXext -lX11 >$tmp 2>&1 || err=1
cat $tmp >> $log
if (( err )) ; then
	_warn "This software requires GLX to build the wg program"
	have_glx=0
else
	_verb "found glx.h"
fi

if (( have_glx )) ; then
	_c "WAND_HAVE_GLX=1"
else
	_c "WAND_HAVE_GLX=0"
fi

#
# success
#
_verb ""
_verb "###"
_verb "### SUCCESS - wand software is now configured"
_verb "###"
_verb ""
_c "WAND_CONFIGURED := 1"
_c ""

\rm -f $tmp $tmp2 ctst.c
_notice
_verb "To build this software type 'make' from the top level directory (the"
_verb "directory where the configure script is located)."
_verb ""
_verb "Log file is $log"

