#
# Common things for the build_* scripts
#

# Args passed in from the Makefile on the command-line
VERSION=$1;             shift
INSTALLBIN=$1;          shift
INSTALLSITELIB=$1;      shift

PREFIX=/usr
PM=$INSTALLSITELIB/CGI/SpeedyCGI.pm
URL="http://www.daemoninc.com/SpeedyCGI/"
STD_FILES="$INSTALLBIN/speedy $INSTALLBIN/speedy_backend $PM"
PKGNM="speedycgi"
MODNM="apache"
PKGNM_PRETTY=SpeedyCGI
MODNM_PRETTY="SpeedyCGI Apache Module"
APACHE_DESC="Optional module to improve SpeedyCGI performance under Apache"
MODULE=`apxs -q LIBEXECDIR`/mod_speedycgi.so
HTTPD_CONF=`apxs -q SYSCONFDIR`/httpd.conf
OUTDIR=binaries
VENDOR="Daemon Consulting Inc."
COPYRIGHT="Copyright (C) `date +%Y` $VENDOR"

# Set up our temp directory 
TMPDIR=/tmp/`basename $0`.$$
rm_tmp() {
    rm -rf $TMPDIR
}
adios() {
    rm_tmp
    trap "" 0
    exit $*
}
trap "adios 1" 1 2 3 15
trap "adios $?" 0
rm_tmp

# Make directories
mkdir -p $TMPDIR $OUTDIR 2>/dev/null

# Get the description text out of the .pm file.
grab_description() {
    awk '
	/^=head1/ {if (doit) { exit} }
	{if (doit) {print}}
	/^=head1 DESCRIPTION/ {doit = 1}
    ' $PM
}

# Grab the summary text from the .pm file
grab_summary() {
    awk '
	/head1 NAME/ {in_name = 1}
	/^[A-z].*/ {
	    if (in_name) {
		for (i = 3; i <= NF; ++i) {
		    printf("%s ", $i);
		}
		print "";
		exit;
	    }
	}
    ' $PM
}

remove_prefix() {
    sed -e "s|^${PREFIX}||" -e 's|^/||'
}

# Create script to do apache install after package is installed
apache_install_script() {
    basedir=$1;		shift
    savesuffix=$1;	shift

    cat <<-END
	PREFIX=$PREFIX
	MODULE=$MODULE
	HTTPD_CONF=$HTTPD_CONF
	BASEDIR=$basedir
	SAVESUFFIX=$savesuffix
	END

    cat <<-'END'
	# Get relocated module
	MODULE=`echo $MODULE | sed "s|^${PREFIX}|${BASEDIR}|"`
	if test -f ${HTTPD_CONF}; then
	    (
		cat ${HTTPD_CONF}
		grep "/^LoadModule.*mod_speedycgi.so/" ${HTTPD_CONF} >/dev/null
		if test $? -ne 0; then
		    echo "LoadModule speedycgi_module $MODULE"
		fi
		grep "/^AddModule.*mod_speedycgi/" ${HTTPD_CONF} >/dev/null
		if test $? -ne 0; then
		    echo "AddModule mod_speedycgi.c"
		fi
	    ) >${HTTPD_CONF}.$$
	    cmp ${HTTPD_CONF} ${HTTPD_CONF}.$$ >/dev/null 2>&1
	    if test $? -ne 0 -a -s ${HTTPD_CONF}.$$; then
		mv -f ${HTTPD_CONF} ${HTTPD_CONF}.$SAVESUFFIX
		mv -f ${HTTPD_CONF}.$$ ${HTTPD_CONF}
		echo ${HTTPD_CONF} was updated - please restart httpd
	    else
		rm -f ${HTTPD_CONF}.$$
	    fi
	else
	    echo ${HTTPD_CONF} could not be edited.  Please update it manually.
	fi
	END
}

# Create script to do apache install after package is installed
apache_uninstall_script() {
    cat <<-END
	HTTPD_CONF=${HTTPD_CONF}
	END

    cat <<-'END'
	if test -f ${HTTPD_CONF}; then
	    sed -e '/^LoadModule.*mod_speedycgi.so/d' \
		-e '/^AddModule.*mod_speedycgi/d' \
		${HTTPD_CONF} >${HTTPD_CONF}.$$
	    if test -s ${HTTPD_CONF}.$$; then
		mv -f ${HTTPD_CONF}.$$ ${HTTPD_CONF}
		echo ${HTTPD_CONF} was updated - please restart httpd
	    else
		rm -f ${HTTPD_CONF}.$$
	    fi
	fi
	END
}

add_desc() {
    out=$1;		shift
    is_apache=$1;	shift
    os=$1;		shift
    arch=$1;		shift
    (
	echo OS=$os
	echo ARCH=$arch
	echo IS_APACHE=$is_apache
	for d in "$@"; do
	    echo DEPEND=$d
	done
    ) >${out}.desc
}
