#!/bin/sh

# $Progeny: pgi-build,v 1.53 2002/04/22 18:54:56 branden Exp $

set -e

PROGNAME=pgi-build

# default code name or "flavor"
CODENAME=custom
export CODENAME

PGI_VERSION=$(cat ${PGI_DIR:-/usr/share/pgi}/pgi_version)
export PGI_VERSION

PGI_COMMAND_LINE="$0 $*"
export PGI_COMMAND_LINE

# XXX: what about contrib?
# XXX: --update

TEMP=$(getopt --options B:COP:T:V:a:c:d:hik:m:o:p:r:s:t:u:v --longoptions \
       additional-archives:,build-mirror:,builder:,clean,codename:,complete,debian-version:,dir:,gain-root-command:,help,include-nonfree,include-source,installer-only,kernel-version:,no-clean,no-include-nonfree,no-include-source,no-post-clean,not-complete,not-official,official,outdir:,path:,pgi-http-proxy:,pgi-mirror:,post-clean,product:,release-version:,subdir:,suite:,target:,tmpdir:,username:,vendor:,version,with-debian-debootstrap,without-debian-debootstrap \
       -n "$PROGNAME" -- "$@")

if [ $? -ne 0 ]; then
    echo "$PROGNAME: error while getting options" >&2
fi

eval set -- "$TEMP"

while :; do
    case "$1" in
        -B|--builder) BUILDER="$2"; shift ;;
        -C|--complete) COMPLETE=1 ;;
        -D|--dir) PGI_DIR="$2"; shift ;;
        -M|--misctmpdir) TDIR="$2"; shift ;;
        -O|--official) OFFICIAL=1 ;;
        -P|--pgi-http-proxy) PGI_HTTP_PROXY="$2"; shift ;;
        -V|--release-version) VERSION="$2"; shift ;;
        -a|--additional-archives) OTHER_ARCHIVES="$2"; shift ;;
        -c|--codename) CODENAME="$2"; shift ;;
        -d|--debian-version) DEBVERSION="$2"; shift ;;
        -h|--help) SHOWHELP=1 EARLYEXIT=1 ;;
        -i|--installer-only) INSTALLER_ONLY=1 ;;
        -k|--kernel-version) KERNEL_VERSION="$2"; shift ;;
        -m|--pgi-mirror) PGI_MIRROR="$2"; shift ;;
        -o|--outdir) OUT="$2"; shift ;;
        -p|--path) PGI_PATH="$2"; shift ;;
        -r|--gain-root-command) SU_CMD="$2"; shift ;;
        -s|--suite) SUITE="$2"; shift ;;
        -t|--tmpdir) BASETMPDIR="$2"; shift ;;
        -u|--username) USERID="$2"; shift ;;
        -v|--version) SHOWVERSION=1 EARLYEXIT=1 ;;
        --build-mirror) PGI_BUILD_MIRROR="$2"; shift ;;
        --clean) CLEAN=1 ;;
        --include-nonfree) NONFREE=1 ;;
        --include-source) INCLUDE_SOURCE_PACKAGES=1 ;;
        --no-clean) CLEAN=0 ;;
        --no-include-nonfree) NONFREE=0 ;;
        --no-include-source) INCLUDE_SOURCE_PACKAGES=0 ;;
        --no-post-clean) POST_CLEAN=0 ;;
        --not-complete) COMPLETE=0 ;;
        --not-official) OFFICIAL=0 ;;
        --post-clean) POST_CLEAN=1 ;;
        --product) PRODUCT="$2"; shift ;;
        --subdir) SUBDIR="$2"; shift ;;
        --target) TARGET="$2"; shift ;;
        --vendor) VENDOR="$2"; shift ;;
        --with-debian-debootstrap) DEBIAN_DEBOOTSTRAP=1 ;;
        --without-debian-debootstrap) DEBIAN_DEBOOTSTRAP=0 ;;
        --) shift; break ;;
        *) echo "$PROGNAME: error while parsing options" >&2; exit 1 ;;
    esac
    shift
done

# handle the no-op options
if [ -n "$SHOWVERSION" ]; then
    echo "$PGI_VERSION"
fi

if [ -n "$SHOWHELP" ]; then
    cat << EOF
Usage: $PROGNAME [options ...]
Options:
  -B, --builder=<builder's email address>
  -C, --complete
  -D, --dir=<location of PGI build system>
  -M, --misctmpdir=<miscellanous temporary directory>
  -O, --official
  -P, --pgi-http-proxy=<HTTP proxy URL>
  -V, --release-version=<release version>
  -a, --additional-archives=<additional archives>
  -c, --codename=<PGI configuration code name>
  -d, --debian-version=<Debian version>
  -h, --help
  -i, --installer-only
  -k, --kernel-version=<kernel version>
  -m, --pgi-mirror=<mirror URI>
  -o, --outdir=<output directory>
  -p, --path=<execution path>
  -r, --gain-root-command=<command>
  -s, --suite=<distribution suite name>
  -t, --tmpdir=<temporary directory>
  -u, --username=<username>
  -v, --version
  --build-mirror=<mirror directory>
  --clean
  --include-nonfree
  --include-source
  --no-clean
  --no-include-nonfree
  --no-include-source
  --no-post-clean
  --not-complete
  --not-official
  --post-clean
  --product=<name of PGI-based product>
  --subdir=<PGI subdirectory> (advanced option, influences --target)
  --target=<PGI Makefile target> (advanced option, depends on --subdir)
  --vendor=<name of vendor/distributor>
  --with-debian-debootstrap
  --without-debian-debootstrap
This help message is intended only as a quick reference.  For a description of
the usage of $PROGNAME, see the $PROGNAME(1) manual page.
EOF
fi

if [ -n "$EARLYEXIT" ]; then
    exit 0
fi

# Constrain CODENAME to [_a-zA-Z][-_a-zA-Z0-9]*
if [ -n "$CODENAME" ] && ! expr "$CODENAME" : "^[_[:alpha:]][-_[:alnum:]]*$" > /dev/null 2>&1; then
    echo "Codename \"$CODENAME\" is not legal."
    echo "Please use a string matching the regular expression \"[_a-zA-Z][-_a-zA-Z0-9]*\"."
    exit -1
fi

# load default values from /etc/pgi/options if CODENAME is not specified.
. /etc/pgi/$CODENAME/options

# Determine whether support for this architecture is experimental or not.
case "$ARCH" in
i386|ia64)
    ;;
*)
    EXPERIMENTAL=1
    export EXPERIMENTAL
    ;;
esac

for DIR in BASETMPDIR TDIR OUT APTTMP; do
  OLDDIR="$(eval echo \$$(echo $DIR))"
  if ! expr "$OLDDIR" : '/.*' > /dev/null 2>&1; then
    NEWDIR="$(pwd)/$(eval echo \$$(echo $DIR))"
    echo "Converting relative path specification \"$OLDDIR\" to absolute path \"$NEWDIR\"." >&2
    eval $DIR=\"$NEWDIR\"
  fi
done

if [ $CLEAN -eq 1 ]; then
    $SU_CMD make -C "${PGI_DIR}/${SUBDIR}" clean
fi

# set up temporary directories
for DIR in "$BASETMPDIR" "$TDIR" "$OUT" "$APPTTMP"; do
    if [ -n "$DIR" ]; then
        if ! mkdir -m 755 -p "$DIR"; then
            echo "Unable to create temporary directory \"$DIR\".  Aborting." >&2
            exit 1
        fi
    fi
done

# checks to make sure all of the directories mentioned in here actually do exist
# ($OTHER_ARCHIVES may not be defined but $PGI_DIR certainly will)
for DIR in "$OTHER_ARCHIVES" "$PGI_DIR"; do
    if [ -n "$DIR" ]; then
        if [ ! -d "$DIR" ]; then
            echo "\"$DIR\" either does not exist or is not a directory.  Aborting." >&2
            exit 1
        fi
    fi
done

# make it so, Number One
# tee stomps all over the exit status of the previous command in the pipeline
pgistatusfile="$BASETMPDIR/exitstatus"
export pgistatusfile
# Here's your barf bag:
#
# |^v^v^|
# |     |
# |     |
# |_____|
#
(set +e; $SU_CMD make -C "${PGI_DIR}/${SUBDIR}" ${TARGET}; echo $? > $pgistatusfile) 2>&1 \
    | tee "$BASETMPDIR/pgi-build.log"

pgiexitstatus=$(cat "$pgistatusfile")
rm "$pgistatusfile"

# clean up
if [ $pgiexitstatus -eq 0 -a $CLEAN -eq 1 -a $POST_CLEAN -ne 0 ]; then
    $SU_CMD make -C "${PGI_DIR}/${SUBDIR}" clean
fi

# get rid of (possibly) empty directories
for DIR in "$APTTMP" "$TDIR" "$BASETMPDIR" ; do
    rmdir "$DIR" 2> /dev/null || true
done

exit $pgiexitstatus

# vim:ai:et:sts=4:sw=4:tw=80:
