#!/bin/sh
#
#  ext2hide - a superblock reserved space file stashing utility
#      by infi/2006
#
#  Build utility for ext2hide
#
#  args:
#    all        configures, builds, and tests ext2hide
#    config     only perform systems checks, do not build
#    test       perform some tests against a scratch filesystem
#    install    install the program to /usr/local/bin (or prefix)
#    bugreport  automatic generation of useful information for bug reporting

# change these to whatever.
PREFIX=/usr/local
BINDIR=bin
MANDIR=share/man/man1
OWNER=root
GROUP=root
BINMODE=700
MANMODE=644

# --- don't change anything below this line ---
CMD=$1
ARG=$2
ARGCNFG='config'
ARGTEST='test'
ARGINST='install'
ARGBUGR='bugreport'
ARGALL='all'
PROGNAME=ext2hide

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

RETVAL=0
MINVER=135

####################
##      SUBS      ##
####################

dot () {
	echo -n "."
}

# check the ext2fs library version to make sure it is >= 1.35
vertest () {
	echo
	echo -n "Checking your ext2 library version..."
	TESTVER=Build.vt.$$
	cat << EOF > ${TESTVER}.c
#include <stdio.h>
#include <ext2fs/ext2fs.h>
int main(void){
const char *lv,*ld;
int lvn;
lvn=ext2fs_get_library_version(&lv,&ld);
printf("%s (%s)\n",lv,ld);
return lvn;
}
EOF
	gcc -lext2fs ${TESTVER}.c -o ${TESTVER} > /dev/null 2>&1
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo
		echo
		echo "Warning: I cannot determine your e2fsprogs library version.  You"
		echo "should check your version manually, and read the 'WARNING' file in"
		echo "this directory for information about potential data loss."
		echo
		rm -f ${TESTVER}.c
		return ${RETVAL}
	fi
	VERSTRING=`./${TESTVER}`
	RETVAL=$?
	if [ $RETVAL -lt ${MINVER} ]; then
		echo
		echo
		echo "************** DANGER, WILL ROBINSON! DANGER! ****************"
		echo "            Reported Version: ${VERSTRING}"
		echo "You are running a pre-1.35 version of e2fsprogs, the libraries"
		echo "that ext2 relies upon.  PLEASE read the 'WARNING' file in this"
		echo "     directory for information about potential data loss."
		echo "**************************************************************"
		echo
        echo -n "Press ENTER to continue, or Ctrl-C to abort..."
        read
        echo
    else
        echo " ${VERSTRING}"
	fi
	rm -f ${TESTVER}.c ${TESTVER}
	return ${RETVAL}
}

# test existence of ext2tools and getopt
config () {
	echo -n "Checking your set-up..."
	TESTC=Build.$$
	dot
	cat << EOF > ${TESTC}.c
#include <stdio.h>
#include <unistd.h>
#include <ext2fs/ext2fs.h>
int main(int argc,char **argv){ getopt(argc,argv,"l"); }
EOF
	dot
	gcc -lext2fs ${TESTC}.c -o ${TESTC} > /dev/null 2>&1
	RETVAL=$?
	dot
	if [ $RETVAL -ne 0 ]; then
		echo
		echo
		echo "Error locating E2fsprogs libraries or header files.  Please read the"
		echo "INSTALL file accompanying this package for further instructions."
		echo
		rm -f ${TESTC} ${TESTC}.c
		exit $RETVAL
	fi
    vertest
	RETVAL=$?
	[ $RETVAL -eq 0 ] && echo " looks good."
	rm -f ${TESTC} ${TESTC}.c
	return $RETVAL
}

# make the program itself
makeall () {
	[ ! -r Makefile ] && echo "This must be run from the build dir." && return 1
	echo "Making ${PROGNAME}..."
	make -I. -f Makefile all
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo
		echo
		echo "ERROR during MAKE process.  Please copy and paste ALL of the above"
		echo "information, run '$0 bugreport', and contact the author."
		echo "Contact details can be found in the README."
		echo
		exit $RETVAL
	fi
	return $RETVAL
}

# run a build test to ensure we build correctly and work on this system
tst () {
    echo
    echo "Spawning test script..."
    echo
    if [ ! -x Test.pl ]; then
        echo
        echo "**** Cannot find Test.pl; is your distribution complete? ****"
        echo
        return 2
    fi
    ./Test.pl
    RETVAL=$?
    if [ $RETVAL -eq 255 ]; then
        echo
        echo "Tests skipped; hope this is ok!"
        echo
    elif [ $RETVAL -ne 0 ]; then
        echo
		echo "Test script returned a non-zero errorlevel ($RETVAL), hope this is ok!"
        echo
    else
        echo "Continuing build script..."
        echo
    fi
    return $RETVAL
}

# install bin and man page
inst () {
	echo "Installing ${PROGNAME} to $1... "
	PREFIX=$1
	BINDIR=${PREFIX}/${BINDIR}
	MANDIR=${PREFIX}/${MANDIR}
	install -s -o ${OWNER} -g ${GROUP} -m ${BINMODE} ${PROGNAME} ${BINDIR}
	install -o ${OWNER} -g ${GROUP} -m ${MANMODE} ${PROGNAME}.1 ${MANDIR}
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo
		echo "Installation FAILED!"
		echo
		echo "Are you sure you have adequate write privileges?"
		echo
    else
        echo
        echo "All set!  There are usage examples in the man page, under EXAMPLES."
        echo "man ext2hide for more information."
        echo
	fi
	return $RETVAL
}

# take some system details and write a pretty bug report
bugreport () {
	echo "Generating bug report... "
	DATE=`date +%F`
	HOST=`hostname`
	REPORT=${PROGNAME}-BUG-${DATE}-${HOST}.txt
	echo "EXT2HIDE BUG REPORT"                      > ${REPORT}
	echo "----------------------------------"      >> ${REPORT}
	echo "Date     : ${DATE} "`date +"%T %Z"`      >> ${REPORT}
	echo "Hostname : "`hostname -f`                >> ${REPORT}
	echo "Version  : "`egrep '^VERSION=' Makefile|cut -f2 -d=` >> ${REPORT}
	echo "GCC      : "`gcc --version|head -1`      >> ${REPORT}
	echo "make     : "`make -v|head -1`            >> ${REPORT}
	echo "uname    : "`uname -a`                   >> ${REPORT}
	echo "Distro   : "`cat /etc/*version 2>/dev/null||cat /etc/*release 2>/dev/null` >> ${REPORT}
	echo -n "E2FSTools: "                          >> ${REPORT}
    dumpe2fs -V                                    >> ${REPORT} 2>&1
	echo
	echo "Enter a description of your problem (. on a blank line to end):"
	PS2=""
	DESC=`cat << .`
	echo "User Description:"                       >> ${REPORT}
	echo "-----------------"                       >> ${REPORT}
	echo ${DESC}                                   >> ${REPORT}
	echo                                           >> ${REPORT}
	echo "Current directory:"                      >> ${REPORT}
	ls -l                                          >> ${REPORT}
	echo "LibC version:"                           >> ${REPORT}
	ls -l /lib/libc-*                              >> ${REPORT} 2>&1
	echo "/usr/include/ext2fs:"                    >> ${REPORT}
	ls -l /usr/include/ext2fs                      >> ${REPORT} 2>&1
	echo "Ext2 libraries::"                        >> ${REPORT}
	ls -l /lib/libext2*                            >> ${REPORT} 2>&1
	echo
	echo "Bug report generated and saved to '${REPORT}'"
	echo
	echo "Please mail this to the author's address, as found in the README."
	echo
	return $RETVAL
}

###################
##      MAIN     ##
###################

if [ -z "${CMD}" ]; then
	config
	makeall
	tst
	echo "Now just do a './Build install' to install the ${PROGNAME} program."
	echo
else
	case "${CMD}" in
		${ARGCNFG})
			config
			;;
		${ARGTEST})
			tst ${ARG}
			;;
		${ARGINST})
			if [ ! -z "${ARG}" ]; then
				PREFIX=${ARG}
			fi
			inst ${PREFIX}
			;;
		${ARGBUGR})
			bugreport
			;;
		*)
			echo $"Usage: $0 (${ARGALL}|${ARGCNFG}|${ARGTEST}|${ARGINST}|${ARGBUGR})"
			echo
			echo "You are probably looking for $0 ${ARGALL}."
			echo
			exit 1
	esac
fi

exit $?

# END Build
