#!/bin/bash

#For use on Solaris 10, CBE 1.7.0-RC1 is assumed to be installed and
#the below symlinks created
# ln -s /opt/dtbld /opt/jdsbld
# ln -s /opt/dtbld /opt/pkgbuild

# Build IM pkgs
usage="$(basename $0) [-d <topdir>] [-p] [-b] [-n] [spec files...]"
usage="$usage\nDefault build type is 'build-only'"
usage="$usage\n    -d <topdir>: specify another top dir."
usage="$usage\n    -p         : source prep only."
usage="$usage\n    -a         : uninstall/build/install."
usage="$usage\n    -n         : NOT compress/copy to $HOME/packages\n"

# check /opt/jdsbld
if [ ! -d /opt/jdsbld ];then
    echo "Need jds build tools in /opt/jdsbld"
    exit 1
fi

date

IPS_VENDORVER=0.${L10N_BUILDNUM:-"155"}
PUBLISHER=${L10N_PUBLISHER:-"l10n-nightly"}

rm -f /tmp/.build_env.$$
[ -z "$PKGDEST" ] 	|| echo "export PKGDEST=$PKGDEST" >> /tmp/.build_env.$$
[ -z "$IPS_VENDORVER" ]	|| echo "export IPS_VENDORVER=$IPS_VENDORVER" >> /tmp/.build_env.$$
[ -z "$PUBLISHER" ] 	|| echo "export PUBLISHER=$PUBLISHER" >> /tmp/.build_env.$$
[ -z "$DOWNLOADS" ] 	|| echo "export DOWNLOADS=$DOWNLOADS" >> /tmp/.build_env.$$

source /opt/jdsbld/bin/env.sh

. /tmp/.build_env.$$

while getopts pad:n option
do
    case $option in
        p)  FLAGPREP=1;;
        a)  FLAGALL=1;;
        d)  TOPDIR="$OPTARG";;
        n)  NOTCPFLAG=1;;
        ?)  printf "$usage" ; exit ;;
    esac
done
shift $(expr $OPTIND - 1)

if [ X$TOPDIR = X ]; then
    if echo $0 | egrep -s '^/'; then
        TOPDIR=$(dirname $0)
    else
        TOPDIR="`pwd`/$(dirname $0)"
    fi
fi

MACH="${MACH:-`uname -p`}"
BLDDIR="$TOPDIR/build.$MACH"
mkdir -p $BLDDIR/log

# set the http and ftp proxies if you are behind a firewall
# export http_proxy=
# export ftp_proxy=

if [ -z $1 ]; then
    SPECFILES="icu.spec idnkit.spec cmdassist.spec"
else
    SPECFILES=$@
fi

if [ X$FLAGPREP = 'X1' ]; then
    CMD='prep'
elif [ X$FLAGALL = 'X1' ]; then
    CMD='build'
else
    CMD='build-only'
fi

# Explicitly specifying location to enable to use pkgtool (pkgbuild)
# not yet in jdsbld.
PKGTOOL="/opt/pkgbuild/bin/pkgtool"

#no support for IPS in Solaris 10
if $(test `uname -r` != 5.10); then
    # IPS repos are created in $PKGDEST if defined in environment,
    # otherwise in $BLDDIR (build.$MACH).
    IPS_REPODIR="${PKGDEST:-$BLDDIR}/repo.spec"
    SRC_IPS_REPODIR="${PKGDEST:-$BLDDIR}/repoS.spec"

    export PKGBUILD_IPS_SERVER="file://$IPS_REPODIR/"
    export PKGBUILD_SRC_IPS_SERVER="file://$SRC_IPS_REPODIR/"

    rm -rf "$IPS_REPODIR"
    pkgrepo create --version 3 "$PKGBUILD_IPS_SERVER"
    pkgrepo set -s "$PKGBUILD_IPS_SERVER" publisher/prefix=${PUBLISHER}
    rm -rf "$SRC_IPS_REPODIR"
    pkgrepo create --version 3 "$PKGBUILD_SRC_IPS_SERVER"
    pkgrepo set -s "$PKGBUILD_SRC_IPS_SERVER" publisher/prefix=${PUBLISHER}
fi

for spec in $SPECFILES
do
    case $spec in
    cmdassist.spec )
        cd $TOPDIR
        cp cmdassist/COPYING cmdassist/cmdassist.copyright
        VERSION=`egrep "^Version:" cmdassist/cmdassist.spec | awk '{print $2}'`
        mv cmdassist cmdassist-${VERSION}
        /usr/bin/tar cf - cmdassist-${VERSION} | /usr/bin/gzip > cmdassist-${VERSION}.tar.gz
        mv cmdassist-${VERSION}.tar.gz cmdassist-${VERSION}/
        dir=cmdassist-${VERSION}
        ;;
    icu.spec )
        cd $TOPDIR/icu
        dir=icu
        ;;
    idnkit.spec )
        cd $TOPDIR/idn
        /usr/bin/tar cf - sunothers | /usr/bin/gzip > sunothers.tar.gz
        /usr/bin/tar cf - idnconv | /usr/bin/gzip > idnconv.tar.gz
        dir=idn
        ;;
    * )
        dir=${spec%.spec}
        ;;
    esac

    SPECDIRS=$TOPDIR/$dir:$TOPDIR/include
    PATCHDIRS=$TOPDIR/$dir/patches
    SRCDIRS=$TOPDIR/$dir

    #no support for IPS in Solaris 10 and some advanced pkgtool features 
    #not present on CBE 1.7
    if $(test `uname -r` != 5.10); then
        PKGUNINSTALLARGS="uninstall-pkgs --nonotify --specdirs=$SPECDIRS"
        PKGBUILDARGS="$CMD --nonotify --download --topdir=$BLDDIR --specdirs=$SPECDIRS "
        PKGBUILDARGS="$PKGBUILDARGS --pkgformat=ips"
    else
        PKGUNINSTALLARGS="uninstall-pkgs --specdirs=$SPECDIRS"
        PKGBUILDARGS="$CMD --download --topdir=$BLDDIR --specdirs=$SPECDIRS "
        PKGBUILDARGS="$PKGBUILDARGS --pkgformat=filesystem"
    fi
    PKGBUILDARGS="$PKGBUILDARGS --sourcedirs=$SRCDIRS --patchdirs=$PATCHDIRS"
    PKGBUILDARGS="$PKGBUILDARGS --logdir=$BLDDIR/log"
    PKGBUILDARGS="$PKGBUILDARGS --halt-on-errors"

    [[ -z "$DOWNLOADS" ]] || PKGBUILDARGS+=" --tar=$DOWNLOADS"

    if [ $CMD = 'build' ]; then
        echo "pkgtool $PKGUNINSTALLARGS $spec"
        $PKGTOOL $PKGUNINSTALLARGS $spec
    fi
    echo "$PKGTOOL $PKGBUILDARGS --define \"ipsvendorver $IPS_VENDORVER\" --define '_tmppath %{_topdir}/TMP' $spec"
    $PKGTOOL $PKGBUILDARGS --define "ipsvendorver $IPS_VENDORVER" --define '_tmppath %{_topdir}/TMP' $spec || exit 1

    # tentative workaround: should eliminate the previous mv instead,
    # after verifying that directory name does not affect build result
    if [ "$spec" = "cmdassist.spec" ]
    then
        mv "$dir" cmdassist
    fi
done

date
