cbe-install
author xlucas
Tue, 28 Apr 2009 09:49:26 +0000
changeset 41 31570364d0f1
parent 40 b92f7755aa91
child 44 9d78700fb711
permissions -rwxr-xr-x
2009-03-16 Lukas (Luc) Oboril <[email protected]> * bump pkgbuild to latest version 1.3.98.4 * add Sun Studio Express 03/09 as known compiler

#!/bin/bash
#
# Script for setting up the Desktop Build Environment on Solaris
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

IFS=' 	
'

# this is listed separately because we are using the spec file
# included in the pkgbuild tarball
PKGBUILD_VERSION=1.3.98.4

# Modules needed for building GNOME:
GNOME_MODULES="ant m4 autoconf automake libtool bison coreutils diff flex gettext make subversion"
# Modules needed for building KDE:
KDE_MODULES="ant m4 autoconf automake libtool bison coreutils diff flex gettext subversion gnuawk gnused cmake make yasm"
# Not needed for either GNOME or KDE, but useful to have:
OPT_MODULES="rsync cvs"
# SFE has the same deps as GNOME
SFE_MODULES=$GNOME_MODULES
install_kde=no
install_gnome=no
install_sfe=no
install_opt=no
interactive_install=yes
quiet_install=no
default_compiler=
orig_CC="${CC}"

# Obsolete packages to be pkgrm'd.  Mostly renamed packages.
OLD_PKGS="JDSautomake JDSautoconf JDSlibtool JDSm4 JDSgettext JDSfileutils 
          JDSmake JDSpatch CBEpatch JDSflex JDScvs SUNWpkgbuild CBEfileutils"

# default log file.  Changed to /tmp/desktop-cbe-install* if the user
# installing is not root
LOGFILE=/tmp/desktop-cbe-install-`date +%Y-%m-%d-%H:%M`.log

# packages required for building things.  cbe-install will check if they
# are installed and install them if not.

# Solaris SVr4 packages:
CBE_SVR4_DEPENDENCIES="SUNWarc SUNWgpch SUNWhea SUNWsfwhea SUNWwgetr
                       SUNWwgetu SUNWgtar SUNWxcu4 SUNWsprot SUNWxwinc
                       SUNWxorg-headers SUNWbtool SUNWlibmr SUNWlibm"

# OpenSolaris pkg(5) packages:
CBE_PKG_DEPENDENCIES="SUNWgpch SUNWhea SUNWsfwhea SUNWwget SUNWgtar
                       SUNWxcu4 SUNWsprot SUNWxwinc SUNWxorg-headers
                       SUNWbtool SUNWlibm"

# alternatives (SUNW version of tools preferred to CBE version)

# by detault install alternatives when available
install_alt=yes
# by default, install alternatives without asking
ask_alt=no

ALT_make=SUNWgmake
ALT_m4=SUNWgm4
ALT_autoconf=SUNWaconf
ALT_automake="SUNWgnu-automake-110 SUNWgnu-automake-19"
ALT_libtool=SUNWlibtool
ALT_bison=SUNWbison
ALT_coreutils=SUNWgnu-coreutils
ALT_diff=SUNWgnu-diffutils
ALT_flex=SUNWflexlex
ALT_gettext=SUNWgnu-gettext
ALT_subversion=SUNWsvn
ALT_gnused=SUNWgsed
ALT_gnuawk=SUNWgawk
# Do not use SUNWcmake, because it does not contain fix for Sun Studio optimalization. --luc
#ALT_cmake=SUNWcmake
# In SXCE build107 SUNWant = ant 1.7.1
#ALT_ant=SUNWant

# default cbe install location
cbe_prefix=${CBE_PREFIX:-/opt/dtbld}
# this should be set to a reasonable value in install_pkgbuild()
pkgbuild_topdir=/fix/me

# default value for the "who is the build user" question
logname=${LOGNAME:-`logname`}
if [ -x /usr/xpg4/bin/id ]; then
    uid=`/usr/xpg4/bin/id -u`
    username=`/usr/xpg4/bin/id -un`
else
    uid=`/bin/id | cut -f2 -d= | cut -f1 -d'('`
    username=`/bin/id |cut -f2 -d'(' | cut -f1 -d')'`
fi
export HOME="$(eval cd ~$username; pwd)"

always_uninstall_existing=
always_skip_existing=

# write a message to the log file
# not visible to the user
log () {
    for msg in "${@}"; do
	echo "`date`: $msg" >> $LOGFILE
    done
}

# print a message to the user and also write it to the log file
msg () {
    for msg in "${@}"; do
	if [ $quiet_install = no ]; then
	    echo $msg
	fi
	echo "`date`: $msg" >> $LOGFILE
    done
}

# print a message to the user and also write it to the log file
msg_err () {
    for msg in "${@}"; do
	if [ $quiet_install = no ]; then
	    echo ERROR: $msg 1>&2
	fi
	echo "`date`: ERROR: $msg" >> $LOGFILE
    done
}

# clean up if installation is aborted
tempdirs=
remove_tempdirs () {
    IFS=:
    for tdir in $tempdirs; do
	case "$tdir" in
	    /tmp/* )
                cd /tmp
		rm -rf "$tdir"
		;;
	esac
    done
}
trap remove_tempdirs QUIT EXIT


# alternate root directory
rootdir=/

process_cl () {
    while [ $# != 0 ]; do
	case "$1" in
	    -R )
		shift
		if [ $# == 0 ]; then
		    echo "Option -R requires an argument." 1>&2
		    exit 1
		fi
		rootdir=$1
		case "$rootdir" in
		    /* )
			;;
		    * )
			echo "The argument to -R must be an absolute path name" 1>&2
			exit 1
		esac
		;;
	    -h|--help)
		echo "Usage: $0 [options]"
		echo "options:"
		echo "   -h, --help        print this usage info"
		echo "   -R rootdir        install CBE packages in alternate root directory rootdir"
		echo "   -k, --kde         install the packages needed for building KDE"
		echo "   -g, --gnome       install the packages needed for building GNOME"
		echo "   -s, --sfe         install the packages needed for building GNOME"
		echo "   -o, --optional    install the optional packages"
		echo "   -n                non-interactive installation -- assumes the default"
		echo "   -q, --quiet       quiet install, messages are printed in the log only"
		echo "                     answers to all questions"
		echo "   -p dir, --prefix dir"
                echo "                     install the Desktop CBE to dir"
		echo "   --ask-alt         ask for confirmation before installing alternative"
                echo "                     tools ([Open]Solaris tools instead of CBE tools)"
		echo "   --no-alt          do not install alternatives, only CBE tools"
		exit 0
		;;
	    --ask-alt)
		ask_alt=yes
		;;
	    -c|--compiler)
		shift
		if [ $# == 0 ]; then
		    echo "Option -R requires an argument." 1>&2
		    exit 1
		fi
		default_compiler=$1
		test -x $default_compiler || {
		    echo "Compiler $default_compiler not found" 1>&2
		    exit 1
		}
		;;
	    -q|--quiet)
		quiet_install=yes
		;;
	    --no-alt)
		install_alt=no
		;;
	    -k|--kde)
		install_kde=yes
		;;
	    -g|--gnome)
		install_gnome=yes
		;;
	    -s|--sfe)
		install_gnome=yes
		;;
	    -o|--optional)
		install_opt=yes
		;;
	    -n)
		interactive_install=no
		;;
	    -p|--prefix)
                shift
		test $# = 0 && {
		    echo "ERROR: argument required after -p or --prefix option" 1>&2
		}
		cbe_prefix="$1"
		test ${cbe_prefix:0:1} = / || {
		    echo "ERROR: the prefix must be an absolute path" 1>&2
		}
		;;
	    *)
		echo "Unknown option: $1" 1>&2
		exit 1
		;;
	esac
	shift
    done

    if [ $interactive_install = yes ]; then
	quiet_install=no
    fi
}

# check the type and name of the compiler
# set CXX to match CC
identify_compiler () {
    id_CC=$1
    id_CC_name=unknown
    id_CC_release=
    id_CC_version=
    id_CC_rev=
    id_CC_label=unknown

    id_CC_dir=$(dirname $id_CC)

    # check if it's gcc
    cc_version=$($id_CC --version 2>&1)
    echo "$cc_version" | grep 'gcc (GCC)' > /dev/null && {
	id_CC_name='GNU gcc'
	id_CC_version=$(echo "$cc_version" | grep 'gcc (GCC)' | cut -f3 -d' ')
	id_CC_release=$(echo $id_CC_version | cut -f1 -d.)
	id_CC_label=gcc${id_CC_release}
	CXX="$id_CC_dir/g++"
	if [ ! -x "$CXX" ]; then
	    msg "WARNING: $CXX not found"
	fi
	log "$id_CC is $id_CC_name release $id_CC_release"
	return
    }
    cc_version=$($id_CC -V 2>&1)
    echo "$cc_version" | grep '^cc: Sun ' > /dev/null && {
	id_CC_name='Sun Studio'
	id_CC_version=$(echo "$cc_version" | grep '^cc: Sun ' | sed -e 's/^cc: Sun.* C \([0-9][0-9.]*\) .*/\1/')
	id_CC_rev=$(echo "$cc_version" | grep '^cc: Sun ' | sed -e 's/^cc: Sun.* C [0-9][0-9.]* \(.\)*/\1/')
	id_CC_rev=$(echo "$id_CC_rev" | grep '^cc: Sun ' | sed -e 's/SunOS_[^ ]* //')
	if [ "x$id_CC_version" = x5.7 ]; then
	    id_CC_label='ss10'
	    id_CC_release='Vulcan'
	    id_CC_name='Sun Studio 10'
	elif [ "x$id_CC_version" = x5.8 ]; then
	    id_CC_label='ss11'
	    id_CC_release='Venus'
	    id_CC_name='Sun Studio 11'
	elif [ "x$id_CC_version" = x5.9 ]; then
	    id_CC_label='ss12'
	    id_CC_release='Mars'	    
	    id_CC_name='Sun Studio 12'
	elif [ "x$id_CC_version" = x5.10 ]; then
	    id_CC_label='sexp'
	    id_CC_release='Ceres'
	    id_CC_name='Sun Studio Express'
	else
	    id_CC_label="Studio_$id_CC_version"
	    id_CC_release="$id_CC_version"
	fi
	CXX="$id_CC_dir/CC"
	if [ ! -x "$CXX" ]; then
	    msg "WARNING: $CXX not found"
	fi
	log "$id_CC is $id_CC_name release $id_CC_release"
	return
    }
    msg "WARNING: could not identify $id_CC = $cc_version"
}

# create a build environent config file for the given build compiler
make_env () {
    env_compiler=$1
    envdir=$tempdir/env
    logrun mkdir -p $envdir
    identify_compiler $env_compiler
    if [ "$id_CC_name" = unknown ]; then
	log "Compiler $1 is unknown, ignoring"
	return 1
    fi
    # identify_compiler sets CXX to match CC
    # also id_CC_name, id_CC_label, id_CC_version, id_CC_release, id_CC_rev
    env_file="$envdir/$id_CC_label"
    env_nr=1
    while test -f $env_file; do
	env_file="$envdir/${id_CC_label}.${env_nr}"
	env_nr=$(($env_nr + 1))
    done
    log "creating env file for compiler $1 ($id_CC_name)"
    echo '# Build environment config file for the Desktop CBE' > "$env_file"
    echo "CC_name='$id_CC_name'"       >> "$env_file"
    echo "CC_version='$id_CC_version'" >> "$env_file"
    echo "CC_release='$id_CC_release'" >> "$env_file"
    echo "CC_rev='$id_CC_rev'"         >> "$env_file"
    echo "cc_dir='$id_CC_dir'"         >> "$env_file"
    echo "CC='$env_compiler'"          >> "$env_file"
    echo "CXX='$CXX'"                  >> "$env_file"
    return 0
}

# look for compilers in known locations
find_compilers () {
    compiler_found=no
    # FIXME: if CC = gcc, set CFLAGS to -std=c99
    msg "Locating compilers..."
    arch=$(uname -p)
    known_compilers="/sgnome/onnv-tools/$arch/SS12/bin/cc /sgnome/onnv-tools/$arch/SS11/bin/cc /ws/onnv-tools/SUNWspro/SS12/bin/cc /ws/onnv-tools/SUNWspro/SS11/bin/cc /opt/SUNWspro/bin/cc /opt/SS12/SUNWspro/bin/cc /opt/Studio12/bin/cc /opt/SSX0903/bin/cc /usr/bin/cc /opt/SunStudioExpress/bin/cc /usr/bin/gcc /usr/sfw/bin/gcc /usr/gnu/bin/gcc"
    if [ "x$orig_CC" != x ]; then
	if [ -x $orig_CC ]; then
	    echo " $known_compilers " | grep " $orig_CC " > /dev/null || \
		known_compilers="$orig_CC $known_compilers"
	fi
    fi
    for compiler in $known_compilers; do
	if [ -x $compiler -a ! -d $compiler ]; then
	    make_env $compiler
	    compiler_found=yes
	fi
    done
}

# download pkgbuild from sourceforge using wget
download_pkgbuild () {
    mkdir -p $tardir
    /usr/sfw/bin/wget -nd -nH --tries=1 -T 60 -O $tardir/pkgbuild-${PKGBUILD_VERSION}.tar.bz2 http://internode.dl.sourceforge.net/sourceforge/pkgbuild/pkgbuild-${PKGBUILD_VERSION}.tar.bz2 || {
	msg "Failed to download pkgbuild, cannot continue."
	exit 1
    }
}

ask_compiler () {
    ask 'Enter the path to the C compiler' ans_compiler
    if [ ! -x $ans_compiler ]; then
	msg "Invalid answer: $ans_compiler is not an executable"
	return 1
    fi
    if [ -d $ans_compiler ]; then
	msg "Invalid answer: $ans_compiler is a directory"
	return 1
    fi
    identify_compiler "$ans_compiler"
    if [ x$ans_compiler == xunknown ]; then
	msg "Unknown compiler: $ans_compiler"
	return 1
    fi
    compiler_found=yes
    make_env "$ans_compiler"
    return 0
}

# ask the user for additional compiler locations
ask_compilers () {
    first=$1
    ans_more_compilers=yes
    while [ $ans_more_compilers = yes ]; do
	if [ "x$first" = xfirst ]; then
	    msg "You need a C compiler in order to use the Desktop CBE"
	    ask_compiler && first=no
	else
	    ask_yes_no 'Would you like to configure more compilers for use with the Desktop CBE?' ans_more_compilers no
	    if [ $ans_more_compilers = yes ]; then
		ask_compiler
	    fi
	fi
    done
}

# install gcc and/or Sun Studio
install_compilers_pkg () {
    # ask if gcc should be installed
    ask_yes_no "Would you like to install gcc (GNU C compiler)?" \
	ans_install_gcc yes
    test "$ans_install_gcc" = yes && pkg_try_install SUNWgcc
    # ask if sun studio express should be installed
    ask_yes_no "Would you like to install Sun Studio Express (cc 5.10)?" \
	ans_install_ssexp yes
    test "$ans_install_ssexp" = yes && pkg_try_install sunstudioexpress
}

check_compilers_kde () {
    :
}

# print the details of a compiler specified by its env config file
display_compiler () {
    . $1
    msg "$2 - $CC_name $CC_version ($CC_release) in $cc_dir"
}

# select the default compiler configuration
select_default_compiler () {
    if [ "x$default_compiler" != x ]; then
	grep "^CC='$default_compiler'\$" $envdir/* > /dev/null || {
	    make_env $default_compiler && compiler_found=yes
	}
    fi
    found_default_compiler=
    found_CC=
    if [ $compiler_found = yes ]; then
	msg ""
	msg "The following compilers were found on your system:"
	i=1
	for env in $envdir/*; do
	    display_compiler $env $i
	    test "x$default_compiler" != x && \
		grep "^CC='$default_compiler'\$" $env > /dev/null && \
		found_default_compiler=$i
	    test "x$CC" != x && \
		grep "^CC='$orig_CC'\$" $env > /dev/null && \
		found_CC=$i
	    eval env_$i="$env"
	    i=$((i + 1))
	done
	i=$(($i - 1))
	if [ "x$1" != xdont_ask -a "x$found_default_compiler" = x ]; then
	    ask_compilers
	    # don't ask again if the user whats to configure more compilers
	    select_default_compiler dont_ask
	    return
	fi
    else
	msg "No compilers were found on your system."
	if [ $PKG_SYSTEM = pkg ]; then
	    ask_yes_no "Would you like the to install compilers now?" \
		ans_install_compiler yes
	    if [ x$ans_install_compiler = yes ]; then
		install_compilers_pkg && {
		    find_compilers
	            # don't ask again if the user whats
		    # to configure more compilers
		    select_default_compiler dont_ask
		    return
		}
	    fi
	fi
	ask_compilers first
	select_default_compiler dont_ask
	return
    fi

    if [ $i = 1 ]; then
	# there's only one compiler no need to ask which one's the default
	ans_default_compiler=1
    elif [ "x$found_default_compiler" != x ]; then
	# found the compiler specified with the --compiler or -c option
	ans_default_compiler=$found_default_compiler
	msg "Selected #${found_default_compiler} $default_compiler"
    else
	if [ "x$found_CC" != x ]; then
	    # found the compiler in the CC env var
	    default_default_compiler=$found_CC
	else
	    default_default_compiler=$i
	fi
	ans_default_compiler=9999
	while [ $ans_default_compiler -gt $i ]; do
	    ask 'Please select the default compiler' ans_default_compiler \
		$default_default_compiler
	    is_number "$ans_default_compiler" || {
		msg "Please enter a number from 1 to $i"
		ans_default_compiler=9999
	    }
	done
    fi
    eval default_env="\$env_$ans_default_compiler"
    if [ ! -f $default_env ]; then
	msg "Internal error in cbe-install, please report to desktop-discuss"
    fi
    . $default_env
    export CC
    export CXX
    log "Default environment: `basename $default_env` (CC=$CC, CXX=$CXX)"
}

# verify that the running system is suitable for the Desktop CBE
# and set up some variables
init () {
    myos=`uname -s`
    if [ "x$myos" != xSunOS ]; then
	echo "Run this script on a Solaris system." 1>&2
	exit 1
    fi
    
    umask 0022
    the_cpu=`uname -p`
    if [ "x$the_cpu" != xsparc ]; then
	the_cpu=x86
    fi

    myos_rel=`uname -r`
    if [ "x$myos_rel" = "x5.10" -o "x$myos_rel" = "x5.10.1" -o "x$myos_rel" = "x5.11" ]; then
	:
    else
	echo "Solaris 10, Solaris Nevada or OpenSolaris required." 1>&2
	exit 1
    fi

    if [ "x$myos_rel" = "x5.10" ]; then
	solaris_pkg_dir=/cdrom/cdrom0/Solaris_10/Product
    elif [ "x$myos_rel" = "x5.11" ]; then
	solaris_pkg_dir=/cdrom/cdrom0/Solaris_11/Product
    fi

    mydir=$(cd $(dirname $0); pwd);
    tardir=${mydir}/tarballs

    CBE_VERSION=`cat $mydir/CBE_VERSION`
    if [ $quiet_install = no ]; then
	echo "Desktop Common Build Environment (CBE) Installer version $CBE_VERSION"
    fi

    my_id=`/usr/xpg4/bin/id -u`
    prof_sw_inst=`/bin/profiles | nl -s: | grep 'Software Installation' | cut -f1 -d:`
    prof_pri_adm=`/bin/profiles | nl -s: | grep 'Primary Administrator' | cut -f1 -d:`
    prof_basic_usr=`/bin/profiles | nl -s: | grep 'Basic Solaris User' | cut -f1 -d:`
    if [ "x$prof_sw_inst" = x -a "x$prof_pri_adm" = x -o "x$my_id" = x0 ]; then
	echo 'Run this script as a user with the "Software Installation"' 1>&2
	echo 'or "Primary Administrator" profile.' 1>&2
	test -x /usr/bin/users-admin && {
	    echo 1>&2
	    echo 'You can also use the users-admin program (System -> Administration ->' 1>&2
	    echo 'Users and Groups in the GNOME menu) to assign profiles.' 1>&2
	    echo 1>&2
	}
	echo 'See the user_attr(4) and profiles(1) manual pages for further details' 1>&2
	exit 1
    else
	if [ "x$prof_basic_usr" != x ]; then
	    if [ "x$prof_sw_inst" != x ]; then
		if [ $prof_sw_inst -gt $prof_basic_usr ]; then
		    echo 'The "Software Installation" profile in /etc/user_attr should appear' 1>&2
		    echo 'before Basic Solaris User.' 1>&2
		    exit 1
		fi
	    fi
	    if [ "x$prof_pri_adm" != x ]; then
		if [ $prof_pri_adm -gt $prof_basic_usr ]; then
		    echo 'The "Primary Administrator" profile in /etc/user_attr should appear' 1>&2
		    echo 'before Basic Solaris User.' 1>&2
		    exit 1
		fi
	    fi
	fi
    fi

    # svr4 or pkg(5)
    check_pkg_system
    msg "Using \"$PKG_SYSTEM\" packaging system"

    msg "Checking for required packages..."
    if [ $PKG_SYSTEM = pkg ]; then
	check_cbe_dependencies_pkg $CBE_PKG_DEPENDENCIES
    else
	check_cbe_dependencies_svr4 $CBE_SVR4_DEPENDENCIES
    fi
    if [ $DEPS_OK = yes ]; then
	msg "All required packages are installed."
    else
	if [ $DEPS_INSTALLED = yes ]; then
	    msg "Successfully installed all required packages"
	else
	    msg "WARNING: some required packages are missing.  Expect build issues..."
	fi
    fi

    need_tarballs=no
    if [ ! -f $mydir/pkgs-${the_cpu}/CBElibtool/pkginfo ]; then
	# no packages found, will need to build from source
	test ! -f $tardir/pkgbuild-${PKGBUILD_VERSION}.tar.bz2 && {
	    download_pkgbuild
	    need_tarballs=yes
	}
    fi

    if [ -f /usr/sfw/include/glib.h ]; then
	echo "WARNING: /usr/sfw/include/glib.h is in conflict with the Desktop headers." 1>&2
	echo "WARNING: Please remove this file or SUNWGlib to avoid build problems." 1>&2
	echo 1>&2
	if [ $interactive_install = yes ]; then
	    echo "Press Enter to continue"
	    read dummy
	else
	    echo "Aborting installation" 1>&2
	    exit 1
	fi
    fi

    log "Running on `uname -a`"
    log "My directory is $mydir"

    ENV_SET=`env | grep '^[a-zA-Z0-9_]*=' | cut -f1 -d=`
    for var in $ENV_SET; do
	case $var in
	    PS1|PS2|HZ|TERM|SHELL|OLDPWD|PATH|MAIL|PWD|TZ|SHLVL|HOME|LOGNAME|PRINTER|HOSTNAME|_|CC|CXX|EDITOR|SSH_*|DISPLAY|LESS*|LS_COLORS|LS_OPTIONS|TERMINFO|PAGER|MANPATH|VISUAL)
		;;
	    *)
		val=`eval echo "\\\$$var"`
		log "Unsetting $var (=\"$val\")"
		unset $var
		;;
	esac
    done

    tempdir=`mktemp -d /tmp/cbe-install-XXXX`
    tempdirs="$tempdirs:$tempdir"
    if [ ! -d $tempdir ]; then
	msg_err "Failed to create temporary directory in /tmp"
	exit 1
    fi

    # if binary packages are found in the pkgs-*/ subdir then this is a
    # binary install
    if [ -f $mydir/pkgs-${the_cpu}/CBElibtool/pkginfo ]; then
	binary_install=yes
	pkg_arch=`grep '^ARCH=' $mydir/pkgs-${the_cpu}/CBElibtool/pkginfo | cut -f2 -d=`
	arch=`uname -p`
	if [ "x$arch" != "x$pkg_arch" ]; then
	    echo "ERROR: wrong architecture" 1>&2
	    echo "ERROR: these Desktop CBE packages must be installed on a $pkg_arch system" 1>&2
	    exit 1
	fi
	cbe_prefix=/opt/dtbld
    else
	binary_install=no
    fi
}

is_number () {
    if [ "x$1" = xOK ]; then
	return 1
    fi
    temp=$(echo "$1" | sed -e 's/[1-9][0-9]*/OK/')
    if [ "x$temp" == xOK ]; then
	return 0
    fi
    return 1
}

# ask "question" variable_name "default answer"
#
# answer is stored in variable variable_name
ask () {
    if [ $quiet_install = no ]; then
	echo -n "$1"
    fi
    log "Question: $1"
    if [ $interactive_install = no ]; then
	if [ ! -z $3 ]; then
	    if [ $quiet_install = no ]; then
		echo ' => '$3
	    fi
	    eval "$2=\"$3\""
	    log "Answer (non-interactive): $3"
	    return
	else
	    msg ""
	    msg_err "No default answer found for this question"
	    msg_err "Cannot continue with non-interactive installation"
	    log "No default answer, aborting"
	    exit 1
	fi
    fi
 
    if [ ! -z $3 ]; then
	echo -n " [$3]: "
    else
	echo -n ": "
    fi

    read -e val
    log "Answer: $val"
    if [ "x$val" = x ]; then
	eval "$2=\"$3\""
    else
	eval "$2=\"$val\""
    fi
}

# ask_yes_no "question" variable_name "default answer"
#
# Answer ("yes" or "no") is stored in variable variable_name.
# Keep asking the user until the answer is yes or no.
ask_yes_no () {
    yes_no_repeat=yes
    while [ $yes_no_repeat = yes ]; do
	yes_no_repeat=no
	ask "${@}"
	eval "the_ans=\"\$$2\""
	case "$the_ans" in
	    [yY]|[yY][eE][sS] )
                eval "$2=yes"
		;;
 	    [nN]|[nN][oO] )
		eval "$2=no"
		;;
	    * )
	        echo "Please answer yes or no"
		yes_no_repeat=yes
	esac
    done
}

# get_dir "question" "answer_var" "default answer" "owner"
#
# Ask for a directory name and make sure it exists or create it
# for the user
get_dir () {
    repeat=yes
    while [ $repeat = yes ]; do
	repeat=no
	ask "$1" new_dir "$3"
	case "$new_dir" in
	    /* )
                # absolute path name, nothing to do
                ;;
	    ~* )
	        if [ "x$rootdir" != x/ ]; then
		    msg "The Desktop CBE is being installed to an alternate root directory."
		    msg "You need to specify the full path to directories within the alternate root."
		    repeat=yes
		    continue
	        fi
		d1=`echo "$new_dir" | cut -f1 -d/`
		d2=`echo "$new_dir/" | cut -f2- -d/`
		d1n=$(eval echo "$d1")
		test -d "$d1n" || {
		    repeat=yes
		    msg "Directory not found: $d1"
		    continue
                }
		d1n=$(cd "$d1n" 2>/dev/null && pwd) || {
		    repeat=yes
		    msg "Failed to access directory: $d1"
		    continue
		}
		msg "Directory $new_dir expanded to $d1n/$d2"
		new_dir="$d1n/$d2"
		;;
	    * )
	        if [ "x$rootdir" != x/ ]; then
		    msg "The Desktop CBE is being installed to an alternate root directory."
		    msg "You need to specify the full path to directories within the alternate root."
		    repeat=yes
		    continue
	        fi
		d1=`echo "$new_dir" | cut -f1 -d/`
		d2=`echo "$new_dir/" | cut -f2- -d/`
		d1n=$(eval echo "$d1")
		test -d "$d1n" || {
		    repeat=yes
		    msg "Directory not found: $d1"
		    continue
                }
		d1n=$(cd "$d1n" 2>/dev/null && pwd) || {
		    repeat=yes
		    msg "Failed to access directory: $d1"
		    continue
		}
		msg "Directory $new_dir expanded to $d1n/$d2"
		new_dir="$d1n/$d2"
		;;
	esac
    done
    if [ "x$new_dir" != "x/" ]; then
	new_dir=`echo "$new_dir" | sed -e 's%/*$%%'`
    fi
    eval "$2=\"$new_dir\""
}

# gather info for the installation from the user
ask_defaults () {
    if [ $binary_install != yes ]; then
	repeat=yes
	while [ $repeat = yes ]; do
	    repeat=no
	    get_dir "Enter the base directory (prefix) of the cbe" \
		cbe_prefix $cbe_prefix root
	    if [ "$cbe_prefix" = "/" -o "$cbe_prefix" = "/tmp" ]; then
		msg "This directory is not allowed."
		msg "Please choose a different one as the cbe prefix"
		repeat=yes
	    fi
	done
    fi

    if [ ! -f "$HOME/.pkgbuildmacros" -a ! -d "$HOME/packages" ]; then
	# the user probably hasn't used pkgbuild >= 1.1.0 yet
	# so let's ask if they want to use a specific topdir
	# instead of $HOME/packages
	msg "Starting from pkgbuild 1.1.0 (CBE 1.5) there is no system-wide"
        msg "build directory (%_topdir), instead, each user has their own."
	msg "The default directory is \$HOME/packages."
	msg "If you wish to use a different build directory, you can define it"
	msg "in \$HOME/.pkgbuildmacros as follows:"
	msg ""
	msg "       %_topdir    /path/to/my/build/area"
	msg ""
	if [ "$uid" = 0 ]; then
	    if [ $interactive_install = yes ]; then
		echo
		echo Press Enter to continue
		read dummy
	    fi
	else
	    ask_yes_no "Would you like to do this now?" \
		set_topdir no
	    if [ $set_topdir = no ]; then
		log "the user chose not to change the default topdir"
		msg "The default topdir ($HOME/packages) will be used"
	    else
		get_dir "Enter your build directory" default_topdir \
		    "$cbe_prefix/packages" $logname
		log "the user selected $default_topdir for topdir"
		echo "%_topdir $default_topdir" > $HOME/.pkgbuildmacros
		log "created $HOME/.pkgbuildmacros"
	    fi
	fi
    else
	log "the user already has a .pkgbuildmacros file or ~/packages dir"
    fi

    find_compilers
    select_default_compiler

    if [ x$install_gnome = xno -a x$install_kde = xno \
	    -a x$install_opt = xno -a x$install_sfe = xno ]; then
	# no components selected using the CLI (--gnome, etc.)

	msg ""
	msg "The Desktop CBE includes tools for building GNOME, KDE and/or SFE packages"
	msg "It also includes some optional tools.  Please select the tools you wish"
	msg "to install."
	msg ""
	ask_yes_no "Would you like install the tools for GNOME?" install_gnome yes
	ask_yes_no "Would you like install the tools for KDE?" install_kde yes
	ask_yes_no "Would you like install the tools for SFE?" install_sfe yes
	msg ""
	msg "The following tools are optional: $OPT_MODULES"
	ask_yes_no "Would you like install the optional tools?" install_opt yes
    else
	log "Install tools for GNOME? $install_gnome"
	log "Install tools for KDE? $install_kde"
	log "Install tools for SFE? $install_sfe"
	log "Install tools for OPTINAL? $install_opt"
    fi
}

# create an admin file for pkgadd/pkgrm
make_admin_file () {
    echo "mail="                 > $1
    echo "instance=unique" 	>> $1
    echo "conflict=nocheck"     >> $1
    echo "conflict=quit"        >> $1
    echo "setuid=nocheck"       >> $1
    echo "action=nocheck"       >> $1
    echo "partial=quit"         >> $1
    echo "space=quit"           >> $1
    if [ "$2" = "pkgrm" ]; then
	echo "idepend=nocheck"      >> $1
	echo "rdepend=nocheck"      >> $1
    else
	echo "idepend=quit"         >> $1
	echo "rdepend=quit"         >> $1
    fi
}

# run a command, redirect the output to a log file and also print it
# on stdout
logrun () {
    status_file=/tmp/.command_status.$$
    /usr/bin/rm -f $status_file
    log "running $*"
    log "*** command output follows ***"
    if [ $quiet_install = yes ]; then
	"${@}" >>$LOGFILE 2>&1
	status=$?
    else
	( "${@}" 2>&1 ; echo $? > $status_file ) | tee -a $LOGFILE
	status=`cat $status_file`
	/usr/bin/rm -f $status_file
    fi
    log "*** command output ends ***"
    return $status
}

# run a command, redirect the output to a log file and also print it
# on stdout
logrun_quiet () {
    log "running $*"
    log "*** command output follows ***"
    "${@}" >>$LOGFILE 2>&1
    status=$?
    log "*** command output ends ***"
    return $status
}

# check if a package is installed and if so, offer to uninstall it
check_if_installed () {
    for f in $*; do
	skip_package=no

	pkginfo -R $rootdir -q "$f"'.*' || return 0

	if [ x$always_uninstall_existing != xyes -a x$always_skip_existing != xyes ]; then
	    msg "Package $f is already installed."
	    ask_yes_no "Would you like uninstall it?" do_uninstall yes
	    if [ $do_uninstall = yes ]; then
		if [ x$always_uninstall_existing != xno ]; then
		    ask_yes_no "Always uninstall previously installed packages?" \
			always_uninstall_existing yes
		    log "always_uninstall_existing = $always_uninstall_existing"
		fi
	    fi
	else
	    if [ x$always_skip_existing = xyes ]; then
		msg "Skipping package $f -- already installed"
		skip_package=yes
		return 0
	    else
		do_uninstall=yes
	    fi
	fi
	if [ $do_uninstall = yes ]; then
	    log "User chose to uninstall $f"
	    make_admin_file /tmp/pkgrm.admin.$$ pkgrm || return 1
	    pkgrm_failed=no
	    logrun pfexec /usr/sbin/pkgrm -R $rootdir -n -a /tmp/pkgrm.admin.$$ "$f.*" || pkgrm_failed=yes
	    if [ $pkgrm_failed = yes ]; then
		ask_yes_no "pkgrm failed. would you like to continue anyway?" pkgrm_cont no
		if [ $pkgrm_cont = yes ]; then
		    log "User chose to continue anyway"
		    return 0
		fi
		return 1
	    fi
	    return 0
	else
	    log "User chose not to uninstall $f"
	    if [ x$always_skip_existing != xyes ]; then
		ask_yes_no "Would you like skip to the next package?
(\"no\" means the installer will try to build and install a duplicate package)" skip_package yes
		if [ $skip_package = yes ]; then
		    log "User chose to skip package $f"
		    if [ x$always_skip_existing != xno ]; then
			ask_yes_no "Always skip packages that are already installed?" always_skip_existing yes
			log "always_skip_existing = $always_skip_existing"
		    fi
		    return 0
		fi
	    else
		skip_package=yes
		return 0
	    fi
	    msg "Not uninstalling $f."
	    msg "NOTE that this may cause problems later"
	    ask "Press Enter to continue" dummy ""
	    return 0
	fi
    done
    return 0
}

# install a list of svr4 packages given its directory, altroot and the
# names of the packages
install_pkgs () {
    pdir=$1; shift
    rdir=$1; shift
    make_admin_file $tempdir/pkgadd.admin.$$ pkgadd

    for pkg in $*; do
	msg "Installing package $pkg"
	logrun pfexec /usr/sbin/pkgadd -R "$rdir" -n -a $tempdir/pkgadd.admin.$$ -d "$pdir" $pkg || \
	    return 1
    done
    /usr/bin/rm -f $tempdir/pkgadd.admin.$$
    return 0
}

# check if the system is pkg(5) based or SVr4 based
# To identify, we check which system was used to install SUNWcs/SUNWcsu
# Output: set the PKG_SYSTEM variable with either pkg or svr4
check_pkg_system () {
    if [ -x /usr/bin/pkg -a -d /var/pkg/catalog ]; then
	pkg -R $rootdir list SUNWcs > /dev/null 2>&1 && {
	    PKG_SYSTEM=pkg
	    return
	}
    fi

    pkginfo -R $rootdir -q SUNWcsu && {
	PKG_SYSTEM=svr4
	return
    }

    msg_err "Could not identify the packaging system"
    exit 1
}

pkg_refreshed=no
pkg_refresh () {
    if [ $pkg_refreshed = no ]; then
	msg "Refeshing the package catalogs..."
	logrun_quiet pfexec pkg -R $rootdir refresh
	pkg_refreshed=yes
    fi
}

pkg_try_install () {
    pkg_refresh
    pfexec pkg -R $rootdir install -q $1 && {
	msg "$1 installed successfully"
	return 0
    } || {
	msg "Failed to install package $1"
	msg "Retrying in verbose mode:"
	pfexec pkg -R $rootdir install -v $1 && {
	    msg "$1 installed successfully"
	    return 0
	} || {
	    msg "Installation of $1 failed again."
	    ask_yes_no "Would you like to continue? (no means exit)" ans_continue no
	    if [ $ans_continue = no ]; then
		exit 1
	    fi
	    DEPS_INSTALLED=no
	    return 1
	}
    }		
}

install_all_dependencies=
skip_all_dependencies=
check_cbe_dependencies_pkg () {
    DEPS_OK=yes
    DEPS_INSTALLED=no
    for pkg in $*; do
	pkg -R $rootdir list $pkg > /dev/null 2>&1 && {
	    log "$pkg is installed"
	} || {
	    log "$pkg is not installed"
	    DEPS_OK=no
	    if [ x$skip_all_dependencies = xyes ]; then
		msg "   $pkg is not installed (skipping)"
	    elif [ x$install_all_dependencies = xyes ]; then
		pkg_try_install $pkg
	    elif [ x$install_all_dependencies = x ]; then
		msg "Package $pkg is not installed"
		ask_yes_no "Would you like the CBE installer to install this package for you?" ans_install_dep "yes"
		if [ $ans_install_dep = yes ]; then
		    ask_yes_no "Would you like the CBE installer to install all required packages?" install_all_dependencies yes
		    pkg_try_install $pkg && {
			DEPS_INSTALLED=yes
		    }
		else
		    # user chose not to install this dep
		    DEPS_INSTALLED=no
		    log "The user chose not to install $pkg"
		    if [ x$skip_all_dependencies = x ]; then
			ask_yes_no "Would you like to skip all missing packages?" skip_all_dependencies no
		    fi
		fi
	    elif [ x$install_all_dependencies = xno ]; then
		msg "Package $pkg is not installed"
		ask_yes_no "Install this package?" ans_install_dep "yes"
		if [ $ans_install_dep = yes ]; then
		    pkg_try_install $pkg
		fi
	    fi
	}
    done
}

install_all_dependencies=
skip_all_dependencies=
# check if the pkgs GNOME or KDE requires are installed; if not install them
check_cbe_dependencies_svr4 () {
    DEPS_OK=yes
    DEPS_INSTALLED=yes
    # we're going to cp solaris pkgs from the media to this dir
    tmp_pkg_dir=`mktemp -d /tmp/cbe-pkgs-XXXX`
    # schedule this dir for removal if the installation is aborted
    tempdirs="$tempdirs:$tmp_pkg_dir"
    if [ ! -d $tmp_pkg_dir ]; then
	msg_err "Failed to create temporary directory in /tmp"
	exit 1
    fi
    for pkg in $*; do
	pkginfo -R $rootdir -q "$pkg."'*' && continue
	DEPS_OK=no
	if [ x$skip_all_dependencies = xyes ]; then
	    msg "   $pkg is not installed (skipping)"
	    continue
	elif [ x$install_all_dependencies = x ]; then
	    msg "Package $pkg is not installed"
	    ask_yes_no "Would you like the CBE installer to install this package for you?" ans_install_dep "yes"
	    if [ $ans_install_dep = yes ]; then
		ask_yes_no "Would you like the CBE installer to install all required packages?" install_all_dependencies yes
		while /bin/true; do
		    ask "Enter the path to the Solaris packages" \
			solaris_pkg_dir "$solaris_pkg_dir"
		    # Copy all packages found in $solaris_pkg_dir to a temporary
		    # dir so that people installing from CDs don't need to
		    # swap CDs all the time...
		    for p0 in $CBE_SVR4_DEPENDENCIES; do
			# already copied to the tmp dir
			test -d $tmp_pkg_dir/$p0 && continue
			# already installed, no need to copy
			pkginfo -q $p0 && continue
			test -d "$solaris_pkg_dir/$p0" && \
			    /bin/cp -pr "$solaris_pkg_dir/$p0" $tmp_pkg_dir && \
			    /bin/chmod -R u+w $tmp_pkg_dir/$p0
		    done
		    test -d "$solaris_pkg_dir/$pkg" && break
		    msg "$solaris_pkg_dir/$pkg not found"
		done
	    else
		log "The user chose not to install $pkg"
		DEPS_INSTALLED=no
		if [ x$skip_all_dependencies = x ]; then
		    ask_yes_no "Would you like to skip all missing packages?" skip_all_dependencies no
		fi
		continue
	    fi
	elif [ x$install_all_dependencies = xno ]; then
	    msg "Package $pkg is not installed"
	    ask_yes_no "Install this package?" ans_install_dep "yes"
	    if [ $ans_install_dep = no ]; then
		DEPS_INSTALLED=no
		continue
	    fi
	fi
	while /bin/true; do
	    msg "Installing package $pkg"
	    make_admin_file /tmp/pkgadd.admin.$$ pkgadd
	    if [ -d $tmp_pkg_dir/$pkg ]; then
		logrun pfexec /usr/sbin/pkgadd -R $rootdir -n -a /tmp/pkgadd.admin.$$ -d "$tmp_pkg_dir" $pkg
		if [ $? != 0 ]; then
		    msg "WARNING: failed to install required package $pkg"
		    ask_yes_no "Would you like to retry?" "do_retry" "yes"
		    if [ $do_retry = no ]; then
			DEPS_INSTALLED=no
		    fi
		else
		    do_retry=no_need_to
		fi
	    else
		msg "Package $pkg was not found in $solaris_pkg_dir"
		do_retry=yes
		/bin/true
	    fi
	    if [ $do_retry = yes ]; then
		while /bin/true; do
		    ask "Enter the path to the Solaris packages" \
			solaris_pkg_dir "$solaris_pkg_dir"
		    test -d "$solaris_pkg_dir/$pkg" && break
		    msg "$solaris_pkg_dir/$pkg not found"
		done
		# Copy all packages found in $solaris_pkg_dir to a temporary
		# dir so that people installing from CDs don't need to
		# swap CDs all the time...
		for p0 in $CBE_SVR4_DEPENDENCIES; do
		    test -d "$solaris_pkg_dir/$p0" && \
			/bin/cp -pr "$solaris_pkg_dir/$p0" $tmp_pkg_dir && \
			/bin/chmod -R u+w $tmp_pkg_dir/$p0
		done
	    elif [ $do_retry = no ]; then
		ask_yes_no "Continue installation anyway?" \
		    do_continue no
		if [ $do_continue = no ]; then
		    /bin/rm -f /tmp/pkgadd.admin.$$
		    /bin/rm -rf $tmp_pkg_dir
		    msg "Installation aborted."
		    exit 1
		else
		    DEPS_INSTALLED=no
		    break
		fi
		break
	    else
		/bin/rm -rf $tmp_pkg_dir/$pkg
		/bin/rm -f /tmp/pkgadd.admin.$$
		break
	    fi
	done
    done
    /bin/rm -rf $tmp_pkg_dir
}

# install pkgbuild from tarball to a temp location and then build a
# pkgbuild pkg using itself
install_pkgbuild () {
    check_if_installed SFpkgbuild || {
	pkgbuild_topdir=`$cbe_prefix/bin/pkgbuild -bp $cbe_prefix/lib/pkgbuild-$PKGBUILD_VERSION/get_rpm_topdir 2>&1 | grep '^TOPDIR=' | cut -f2- -d=`
	return 1
    }
    if [ $skip_package = yes ]; then
	return 0
    fi
    export PATH=/usr/ccs/bin:/usr/gnu/bin:/usr/bin:/usr/sbin:/usr/sfw/bin
    # we're going to build pkgbuild in this directory
    tmp_build_dir=`mktemp -d /tmp/cbe-build-XXXX`
    # schedule this dir for removal if the installation is aborted
    tempdirs="$tempdirs:$tmp_build_dir"
    if [ ! -d $tmp_build_dir ]; then
	msg_err "Failed to create temporary directory in /tmp"
	exit 1
    fi
    cd $tmp_build_dir || return 1
    logrun mkdir -p src || return 1
    cd src || return 1
    logrun bash -c "bzcat $tardir/pkgbuild-$PKGBUILD_VERSION.tar.bz2 | tar xf -" || return 1
    cd pkgbuild-$PKGBUILD_VERSION || return 1
    msg "Building pkgbuild from source..."
    if [ x"`basename $CC`" = xgcc ]; then
	export CFLAGS='-std=c99'
    fi
    logrun_quiet ./configure --prefix="$tmp_build_dir" || return 1
    logrun_quiet make install || return 1
    msg "Now rebuilding pkgbuild using itself..."
    logrun mkdir -p $tmp_build_dir/packages/SOURCES || return 1
    logrun ln -s $tardir/pkgbuild-$PKGBUILD_VERSION.tar.bz2 $tmp_build_dir/packages/SOURCES/ || return 1
    logrun cp -pr $tmp_build_dir/src/pkgbuild-$PKGBUILD_VERSION/COPYING $tmp_build_dir/packages/SOURCES/gpl-2.0.txt || return 1
    logrun_quiet bash -c "$tmp_build_dir/bin/pkgbuild --define \"pkgbuild_prefix $cbe_prefix\" --define \"_topdir $tmp_build_dir/packages\" -ba $tmp_build_dir/src/pkgbuild-$PKGBUILD_VERSION/pkgbuild.spec" || return 1
    make_admin_file $tempdir/pkgadd.admin.$$ pkgadd
    logrun pfexec /usr/sbin/pkgadd -R $rootdir -n -a $tempdir/pkgadd.admin.$$ -d "$tmp_build_dir/packages/PKGS" SFpkgbuild || return 1
    pkgbuild_topdir=`$cbe_prefix/bin/pkgbuild -bp $cbe_prefix/lib/pkgbuild-$PKGBUILD_VERSION/get_rpm_topdir 2>&1 | grep '^TOPDIR=' | cut -f2- -d=`
    logrun mkdir -p "$rootdir/$pkgbuild_topdir/PKGS" "$rootdir/$pkgbuild_topdir/SPKGS"
    logrun cp -pr "$tmp_build_dir/packages/SPKGS/SFpkgbuild-src" "$rootdir/$pkgbuild_topdir/SPKGS"
    logrun cp -pr "$tmp_build_dir/packages/PKGS/SFpkgbuild" "$rootdir/$pkgbuild_topdir/PKGS"
    cd /tmp || return 1
    logrun /usr/bin/rm -rf $tmp_build_dir || return 1
    unset CFLAGS

    return 0
}

# build and install a package
build_pkg () {
    install_pkgs "$rootdir$pkgbuild_topdir/PKGS" "$rootdir" "$3" || return 1
}

# older versions of the JDS CBE included packages that were renamed or
# no longer included.  These older JDS CBEs only supported SVr4.
uninstall_old_cbe () {
    pkg_info=
    for pkg in $OLD_PKGS; do
	pkginfo -R $rootdir -q "$pkg."'*' && pkg_info="$pkg_info
`pkginfo -R $rootdir $pkg.'*'`"
    done
    if [ ! -z "$pkg_info" ]; then
	msg "The following packages belong to an obsolete version of the JDS CBE."
	msg "$pkg_info"
	msg "" 
	ask_yes_no "Would you like to uninstall them?" do_uninst yes
	if [ $do_uninst = yes ]; then
	    log "User chose to uninstall obsolete CBE pkgs"
	    make_admin_file $tempdir/pkgrm.admin.$$ pkgrm
	    pkgrm_failed=no
	    for pkg in $OLD_PKGS; do
		if pkginfo -R $rootdir -q "$pkg."'*'; then
		    logrun pfexec /usr/sbin/pkgrm -R $rootdir -n -a $tempdir/pkgrm.admin.$$ "$pkg."'*' || \
			pkgrm_failed=yes
		fi
	    done
	    rm -f $tempdir/pkgrm.admin.$$
	    if [ $pkgrm_failed = yes ]; then
		msg "Failed to remove one or more packages"
		ask_yes_no "Would you like to continue anyway?" do_cont no
		if [ $do_cont = no ]; then
		    return 1
		else
		    log "User chose to continue anyway"
		    return 0
		fi
	    fi
	fi
    fi
    return 0
}

main () {
    init
    uninstall_old_cbe || return 1
    ask_defaults
    test -d $
    pkginfo -q -R $rootdir CBEenv && {
	msg "Uninstalling CBEenv"
	make_admin_file /tmp/pkgrm.admin.$$ pkgrm || return 1
	logrun pfexec /usr/sbin/pkgrm -R $rootdir -n -a /tmp/pkgrm.admin.$$ "CBEenv.*"
	rm -f /tmp/pkgrm.admin.$$
    }

    ALL_MODULES=
    if [ $install_gnome = yes ]; then
	ALL_MODULES="$ALL_MODULES $GNOME_MODULES"
    fi
    if [ $install_kde = yes ]; then
	ALL_MODULES="$ALL_MODULES $KDE_MODULES"
    fi
    if [ $install_sfe = yes ]; then
	ALL_MODULES="$ALL_MODULES $SFE_MODULES"
    fi
    if [ $install_opt = yes ]; then
	ALL_MODULES="$ALL_MODULES $OPT_MODULES"
    fi
    ALL_MODULES=$(echo $ALL_MODULES | sed -e 's/ /\
/g' | sort -u)

    if [ $binary_install = no ]; then
	install_pkgbuild || return 1
    else
	check_if_installed SFpkgbuild || return 1
	if [ $skip_package != yes ]; then
	    install_pkgs "$mydir/pkgs-${the_cpu}" "$rootdir" SFpkgbuild || return 1
	fi
    fi
    pkgbuild_topdir=`$cbe_prefix/bin/pkgbuild -bp $cbe_prefix/lib/pkgbuild-$PKGBUILD_VERSION/get_rpm_topdir 2>&1 | grep '^TOPDIR=' | cut -f2- -d=`

    cd $mydir

    # identify the CBE packages to be built/installed
    ALL_SPECS=
    ALL_CBE_PKGS=
    for mod in $ALL_MODULES; do
	pkgs=$($cbe_prefix/bin/spectool get_packages CBE${mod}.spec 2>/dev/null)
	check_if_installed $pkgs || return 1
        # skip_package is set by check_if_installed
	if [ $skip_package = yes ]; then
	    continue
	fi
	# On Nevada and OpenSolaris use alternatives if available
	if [ $install_alt = yes -a "x$myos_rel" = x5.11 ]; then
	    eval ALT=\"$(eval echo \$ALT_$mod)\"
	    if [ "x$ALT" != x ]; then
		if [ $ask_alt = yes ]; then
		    ask_yes_no "Use $ALT ($mod) instead of CBE $mod?" \
			use_alt yes
		    if [ $use_alt = no ]; then
			ALL_CBE_PKGS="$ALL_CBE_PKGS $pkgs"
			ALL_SPECS="$ALL_SPECS CBE${mod}.spec"
			continue
		    fi
		fi
		msg "Looking for package $ALT"
		check_cbe_dependencies_$PKG_SYSTEM $(eval echo $ALT)
		if [ $DEPS_OK = yes ]; then
		    msg "     ... already installed"
		    continue
		elif [ $DEPS_INSTALLED = yes ]; then
		    msg "Successfully installed $ALT"
		    continue
		else
		    msg "Using the CBE version of $mod"
		    ALL_SPECS="$ALL_SPECS CBE${mod}.spec"
		    ALL_CBE_PKGS="$ALL_CBE_PKGS $pkgs"
		fi
	    else
		# no alternative exists
		ALL_SPECS="$ALL_SPECS CBE${mod}.spec"
		ALL_CBE_PKGS="$ALL_CBE_PKGS $pkgs"		
	    fi
	else
	    # alternatives should not be used
	    ALL_SPECS="$ALL_SPECS CBE${mod}.spec"
	    ALL_CBE_PKGS="$ALL_CBE_PKGS $pkgs"
	fi
    done

    if [ $binary_install = no ]; then
	export "PATH=$rootdir$cbe_prefix/bin:/usr/ccs/bin:/usr/gnu/bin:/usr/bin:/usr/sbin:/usr/sfw/bin"
	if [ "x$rootdir" != x/ ]; then
	    export AC_MACRODIR="$rootdir$cbe_prefix/share/autoconf"
	    sed -e "s%--prepend-include %--prepend-include $rootdir%" "$rootdir$cbe_prefix/share/autoconf/autom4te.cfg" > $tempdir/autom4te.cfg
	    export AUTOM4TE_CFG="$tempdir/autom4te.cfg"
	    export autom4te_perllibdir="$rootdir$cbe_prefix/share/autoconf"
	    export AUTOM4TE="$rootdir$cbe_prefix/bin/autom4te"
	fi

	ALL_SPECS=$($cbe_prefix/bin/pkgtool -q build-order $ALL_SPECS)
	for spec in $ALL_SPECS; do
	    pkgs=$($cbe_prefix/bin/spectool get_packages $spec 2>/dev/null)
	    if [ $need_tarballs = yes ]; then
		msg "Downloading source tarball for $pkgs"
		$cbe_prefix/bin/pkgtool download --download-to $tardir $spec || return 1
	    fi

	    # build the package
	    logrun $cbe_prefix/bin/pkgtool --download --download-to "$tardir" --define "cbe_prefix $cbe_prefix" --define "cbe_version $CBE_VERSION" build-only $spec || return 1
	    for pkg in $pkgs; do
		install_pkgs "$rootdir$pkgbuild_topdir/PKGS" "$rootdir" "$pkg" || return 1
	    done
	done
    else
	# binary install
	for pkg in $ALL_CBE_PKGS; do
	    if [ -d "$mydir/pkgs-${the_cpu}/${myos_rel}/$pkg" ]; then
		install_pkgs "$mydir/pkgs-${the_cpu}/${myos_rel}" \
		    "$rootdir" $pkg || return 1
	    else
		install_pkgs "$mydir/pkgs-${the_cpu}" \
		    "$rootdir" $pkg || return 1
	    fi
	done
	pkgbuild_topdir=`$cbe_prefix/bin/pkgbuild -bp $cbe_prefix/lib/pkgbuild-$PKGBUILD_VERSION/get_rpm_topdir 2>&1 | grep '^TOPDIR=' | cut -f2- -d=`
    fi
    log "Creating env.sh"
    logrun mkdir -p $rootdir$pkgbuild_topdir/SOURCES || return 1
    build_info="built on `date +%Y-%m-%d` by ${logname}@`hostname`"
    logrun_quiet bash -c "sed -e \"s%@CBE_PREFIX@%$cbe_prefix%\" \
        -e \"s%@JDS_PREFIX@%$JDS_PREFIX%\" \
	-e \"s%@CC32@%$cc32dir/$cc_name%\" \
	-e \"s%@CXX32@%$cc32dir/$cxx_name%\" \
	-e \"s%@CC64@%$cc64dir/$cc_name%\" \
	-e \"s%@CXX64@%$cc64dir/$cxx_name%\" \
	-e \"s%@CBE_VERSION@%$CBE_VERSION%\" \
        -e \"s%@BUILD_INFO@%$build_info%\" \
	$mydir/ext-sources/env.sh > $rootdir$pkgbuild_topdir/SOURCES/env.sh" || \
	return 1
    log "Creating env_include.sh"
    build_info="built on `date +%Y-%m-%d` by ${logname}@`hostname`"
    logrun_quiet bash -c "sed -e \"s%@CBE_PREFIX@%$cbe_prefix%\" \
        -e \"s%@JDS_PREFIX@%$JDS_PREFIX%\" \
	-e \"s%@CC32@%$cc32dir/$cc_name%\" \
	-e \"s%@CXX32@%$cc32dir/$cxx_name%\" \
	-e \"s%@CC64@%$cc64dir/$cc_name%\" \
	-e \"s%@CXX64@%$cc64dir/$cxx_name%\" \
	-e \"s%@CBE_VERSION@%$CBE_VERSION%\" \
        -e \"s%@BUILD_INFO@%$build_info%\" \
	$mydir/ext-sources/env_include.sh > $rootdir$pkgbuild_topdir/SOURCES/env_include.sh" || \
	return 1
    log "Creating env.csh"
    logrun_quiet bash -c "sed -e \"s%@CBE_PREFIX@%$cbe_prefix%\" \
        -e \"s%@JDS_PREFIX@%$JDS_PREFIX%\" \
	-e \"s%@CC32@%$cc32dir/$cc_name%\" \
	-e \"s%@CXX32@%$cc32dir/$cxx_name%\" \
	-e \"s%@CC64@%$cc64dir/$cc_name%\" \
	-e \"s%@CXX64@%$cc64dir/$cxx_name%\" \
	-e \"s%@CBE_VERSION@%$CBE_VERSION%\" \
        -e \"s%@BUILD_INFO@%$build_info%\" \
	$mydir/ext-sources/env.csh > $rootdir$pkgbuild_topdir/SOURCES/env.csh" || \
	return 1
    log "Copying ld-wrapper, gendiff"
    logrun bash -c "cp $mydir/ext-sources/ld-wrapper $rootdir$pkgbuild_topdir/SOURCES" || \
	return 1
    logrun bash -c "cp $mydir/ext-sources/gendiff $rootdir$pkgbuild_topdir/SOURCES" || \
	return 1
    logrun bash -c "mkdir -p $rootdir$pkgbuild_topdir/SPECS" || \
	return 1
    logrun bash -c "cp $mydir/include/CBE.inc $rootdir$pkgbuild_topdir/SPECS/" || \
	return 1
    logrun bash -c "cp $mydir/include/default-depend.inc $rootdir$pkgbuild_topdir/SPECS/" || \
	return 1
    msg "Building CBEenv"
    logrun_quiet $cbe_prefix/bin/pkgbuild --define "cbe_prefix $cbe_prefix" --define "cbe_version $CBE_VERSION" --define "build_user $logname" -ba CBEenv.spec || return 1
    logrun $cbe_prefix/bin/pkgtool install-pkgs --define "build_user $logname" CBEenv.spec || return 1
    logrun cp ${envdir}/* $cbe_prefix/lib/cbe-env/
    def_env_name=$(basename $default_env)
    cd $cbe_prefix/lib/cbe-env; ln -s $def_env_name default
    cd $mydir
    msg "CBE setup complete"
    msg "Run"
    msg "  . $cbe_prefix/bin/env.sh [environment]"
    msg "or"
    msg "  source $cbe_prefix/bin/env.csh [environment]"
    msg "depending on your shell, to use this build environment."
    msg "You can also run $cbe_prefix/bin/env.sh [environment] to start subshell."
}

process_cl "${@}"
main || {
    msg "CBE setup FAILED"
    msg "Installation log saved in $LOGFILE"
    exit 1
}
msg "Installation log saved in $LOGFILE"