#!/bin/sh
### Copyright 1991, 1992, 1993, 1994, 1995, 1997 Cygnus Solutions
### All Rights Reserved.

##############
###
### This really needs to nestle up snuggly to the Installation notes.  If
### you change this script, please be sure the installation notes get
### coordinated too.
###
##############

### this is our release version, usually the basename of the installation
### directory, eg is 'progressive-XXXXXX' for /usr/cygnus/progressive-XXXXXX
RELEASE_TAG=aix43-010414

### put these here for convenience in editing
HOST=powerpc-ibm-aix5.1.0.0
TARGET=powerpc-ibm-aix5.1.0.0
GCCvn=2.9-aix43-010414

### What bundle is this?  Usually something like 'Emacs' or 'comp-tools'
BUNDLE=comp-tools

### this is the default installation repository. We use this to test whether or
### not GCC_EXEC_PREFIX should be set when doing "Install test comp-tools".
ROOTING_REAL=/usr/cygnus
ROOTING_DEFAULT=${ROOTING_REAL}

### this is where we install into.  (useful for testing).
INSTALLDIR=${ROOTING_DEFAULT}

### FIXINCDIR will be set in comp-tools-fix and cross-tools-fix,
### but clear it initially
FIXINCDIR=""

### where to write the log files
RELEASE_ROOT=${INSTALLDIR}/${RELEASE_TAG}

LOGDIR=${RELEASE_ROOT}

### who to call in bad situations
HOTLINE="the Cygnus Solutions Hotline at +1 800 CYGNUS1"

### what the release doc is called and a few sections thereof
NOTES="the Installation Notes"
CHANGING_PATHS="\"Changing the Paths\""
NO_ACCESS="\"No Access to ${INSTALLDIR}\?\""
MIGHT_WRONG="\"Some Things that Might go Wrong\""
WHY_FIXINCLUDES="Why Convert System Header Files\?"
ANOTHER_TAPE="\"Steps to install with another machine\'s tape drive\""

### for debugging
#PATH=/bin:/usr/bin ; export PATH

PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:$PATH ; export PATH

### each system has a different default tape device, and a different scheme
### for the non-rewinding device name of that tape drive.  This value needs
### to be a NON-rewinding tape drive.
TAPE=${TAPE-/dev/tape}

### clear and/or initialize some variables
ACTIONS=

INSTALLHOST=default
ERROR=

install_src=false
install_bin=false

### some tool defaults
DD=dd
DDOPTS="ibs=124b"   # some systems can't handle this (eg AIX)

TAR=tar
TARFLAGS="xvf -"

SED=sed

#### end of all variable initializations -- NO substitutions should be used
#### past this section

### now start some function declarations

guess_system()
{
    ### next try uname
    if (uname > /dev/null) 2>&1 ; then
        UNAME="`uname -s``uname -m``uname -r`"
    else
        UNAME="machine without a uname command (type of machine unknown)"
    fi # (uname)

    ### map into names we recognize
    case "${UNAME}" in
        AIX*)            /usr/bin/oslevel > /dev/null 2>&1
                         OSLEVEL=`/usr/bin/oslevel`
                         case "$OSLEVEL:$HOST" in
                         *325*) rev=3.2.5; arch=rs6000; DDOPTS="";;
                         4*:powerpc*) rev=$OSLEVEL; arch=powerpc;;
                         4*) rev=$OSLEVEL; arch=rs6000;;
			 # For 5.0, we are installing the 4.3.3.0 images
			 # so we override the OSLEVEL here.
                         5*:powerpc*) rev="5.1.0.0"; arch=powerpc;;
                         esac
                         INSTALLHOST="${arch}-ibm-aix${rev}" ;;
        SunOSsun4*5.5*)  case $HOST in
			 sparc-sun-solaris2)
        		 	INSTALLHOST=sparc-sun-solaris2; NOWARN=1 ;;
			 *)
        		 	INSTALLHOST=sparc-sun-solaris2.5 ;;
			 esac
			 ;;
        SunOSsun4*5*)    INSTALLHOST=sparc-sun-solaris2 ;;
        SunOSsun4*)      INSTALLHOST=sparc-sun-sunos4.1 ;;
        IRIX*)           INSTALLHOST=mips-sgi-irix5 ;;
        HP-UX9000/7*|HP-UX9000/8?[1679]*) 
		 case `uname -r` in
		 *.10.20*)
		 	case $HOST in
		 	hppa1.1-hp-hpux10)
				INSTALLHOST=hppa1.1-hp-hpux10; NOWARN=1 ;;
		 	*)
				INSTALLHOST=hppa1.1-hp-hpux10.20 ;;
			esac
			;;
		 *)
			INSTALLHOST=hppa1.1-hp-hpux`uname -r | sed 's/[^.]*.[0B]*\(.*\)\..*/\1/'`;;
		 esac
		 ;;
        OSF1alphaV3.2*)  INSTALLHOST=alpha-dec-osf3.2C ;;
        OSF1alpha[VX]4.*) INSTALLHOST=alpha-dec-osf4.0 ;;
	Linuxi[3-6]861.2.13) INSTALLHOST=i486-linux ;;
	Linux*)          INSTALLHOST=unsupported-linux ;;
        *)               INSTALLHOST=unknown ;;
    esac # ${UNAME}
}


print_help_message()
{
     cat <<EOF

You may supply at least one format on the command line.  Recognized formats are:
    "binaries"      for binaries only
    "source"        for source code only
The default is both source and binaries.

You may also supply actions.  Recognized actions are:
    "extract"       read the appropriate files from tape/file
    "fixincludes"   build a directory of corrected header files for comp-tools 
    "test"          run a very brief verification of your installation

There are three other options:
    --tape=DEVICE   asks Install to read the necessary files from DEVICE. The
                    default DEVICE is ${TAPE}.
    --file=FILE     asks Install to read the necessary files from FILE. The
                    default FILE is ${TAPE}.
    --installdir=DIRECTORY
		    asks Install to install files into DIRECTORY.
                    The default DIRECTORY is ${ROOTING_REAL}.
    --fixincludesdir=DIRECTORY
                    asks Install to fix the files from DIRECTORY.
                    The default DIRECTORY is /usr/include.
EOF
}


print_version_message()
{
     echo "Install, version 2.1"
}

show_exec_prefix_msg()
{
    echo '***' If you want to use the compilation tools in an alternate
    echo '***   ' directory, several environment variables will need
    echo '***   ' to be set.  You probably want to set them with:
    echo ""
    echo "GCC_EXEC_PREFIX=${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/ ; export GCC_EXEC_PREFIX"
    GCC_EXEC_PREFIX=${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/ ; export GCC_EXEC_PREFIX
    echo ""
    echo '***   ' This is what I will use for the installation tests.
    echo '***   ' For further help, please refer to ${CHANGING_PATHS}
    echo '***   ' in ${NOTES}.
}

show_emacs_alternate_msg()
{
    echo '***' Emacs cannot be installed in an alternate location.  Please
    echo '***' contact Cygnus Support for instructions on how to rebuild
    echo '***' emacs in a different prefix location.
}


#### The Install script is customized by inserting package dependant
#### scripts containing various shell functions when it is generated.
#### There are two functions defined at this time:
####	fix
####		The fix function is called after the package
####		has been loaded.  For example, this function is used
####		by the comp-tools package to run fixincludes and
####		fixproto.
####	verify
####		The verify function is called after the package has
####		been installed.  For example, this function may be
####		used to check that the software is configured
####		properly.
####
#### The control scripts from the POSIX.7.2 draft suggest functions that
#### may be useful for use for future packages.  They are checkinstall,
#### preinstall, postinstall, checkremove, preremove, and postremove
####
####
#### Note that many other install facilities store their package
#### dependant scripts with each distribution.  It would be difficult
#### for us to do the same efficiently.  Their installers are usually
#### programs with built in tar/cpio readers that have the ability to
#### "stop" after control scripts have been extracted.  Since ours is a
#### shell script that must run on many different systems, we would have
#### to either extract the tape contents to a temporary directory and
#### then move it to its final destination; or we would have to perform
#### two passes for each compressed tar image.

#### various functions used to perform post-install fixups (fixincludes, etc.)
fix()
{

    if [ "${INSTALLHOST}" != "${HOST}" -a "${NOWARN}" != "1" ]; then
	cat << EOF
***	This machine appears to be for \"${INSTALLHOST}\", while the
***	software on this tape is meant for \"${HOST}\"  
***
***	You may still run \"$0 --extract ...\" on this machine and then
***	run the other steps on the machine on which you wish to install.
***	You will need to do these steps separately.
***	For more information, please refer to
***     ${ANOTHER_TAPE} in ${NOTES}.
EOF
    else
        fixincludes
    fi
}


fixincludes()
{

    ITOOLS=${RELEASE_ROOT}/${EXECDIR}/${TARGET}/install-tools
    if [ ! -d ${ITOOLS} ]; then
        echo "No install-tools for this target.  Skipping fixincludes."
        return
    fi

    LOGFILE=${LOGDIR}/fixincludes.log
    FIXINCDIR=${FIXINDCIR:="/usr/include"}

    # Hand waving for new fixincludes, shouldn't hurt old fixincludes
    CURDIR=`pwd`
    # needed to find the fixincl program
    cd ${RELEASE_ROOT}/${EXECDIR}/${TARGET}/install-tools

    echo Running fixincludes for ${INSTALLHOST}.
    echo Verbose output will be stored in ${LOGFILE}.
    echo This will take some time.

    # this used to be exported -- does it still need to be? 
    LIB=${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/${TARGET}/${GCCvn}/include 

    CPP="${RELEASE_ROOT}/${EXECDIR}/bin/gcc -E"
    export CPP		# needed by fixincludes and fixproto
    GCC_EXEC_PREFIX=${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/
    export GCC_EXEC_PREFIX

    if [ -f ${ITOOLS}/fix-header ]; then
        FIX_HEADER=${ITOOLS}/fix-header
        export FIX_HEADER   # fixproto needs this
    fi

    if [ -f ${ITOOLS}/fixproto-defines ]; then
        FIXPROTO_DEFINES="`${ITOOLS}/fixproto-defines`"
        export FIXPROTO_DEFINES   # fixproto needs this
    fi

    # Save unfixed *.h files so we can rerun fixincludes.
    UNFIXED=${LIB}/../.unfixed
    if [ ! -d ${UNFIXED} ]; then
	if mkdir ${UNFIXED} && cp ${LIB}/*.h ${UNFIXED} ; then
	    true
	else

	    cat << EOF
*** Unable to create temporary directory ${LIB}/../tmpdir
***     Please ensure that write permissions for
***     ${LIB} are allowed.
***     Or call ${HOTLINE} for more information.
EOF
            exit 1
	fi
    fi

    # now see if we're new fixincludes or old fixincludes
    if [ ! -f ${ITOOLS}/fixincl ]; then
	# classic fixincludes
        # See below.  We remove limits.h to see if fixincludes
        # fixed /usr/include/limits.h.
        rm -f ${LIB}/limits.h
    fi

    # fixincl variables we need set
    srcdir=${UNFIXED}
    TARGET_MACHINE=${TARGET}
    # OK, there are exceptions, but not for any natives today
    INSTALL_ASSERT_H=install_assert_h
    # fixincl won't work with csh
    SHELL=/bin/sh
    export srccir TARGET_MACHINE INSTALL_ASSERT_H SHELL
    
    if [ -f ${ITOOLS}/fixincludes ]; then
        ${ITOOLS}/fixincludes ${LIB} ${FIXINCDIR} > ${LOGFILE} 2>&1
        if [ $? -ne 0 ]; then
	    cat << EOF
*** There seems to have been a problem with fixincludes.
***	The verbose output from the fixincludes script has been logged in
***     ${LOGFILE}.
***     I do not know how to continue until this problem has been
***     corrected.  If you do not know how to fix it either, please
***     call ${HOTLINE}.
EOF
	    cp ${UNFIXED}/* ${LIB}
	    exit 1
	fi
        echo '   ' system header files now ANSI compatible 
    fi

    if [ -f ${ITOOLS}/fixproto ]; then
        ${ITOOLS}/fixproto ${LIB} ${LIB} ${FIXINCDIR} >> ${LOGFILE} 2>&1
        if [ $? -ne 0 ]; then
	    cat << EOF
*** There seems to have been a problem with fixproto.
***     The verbose output from the fixproto script has been logged in
***     ${LOGFILE}.
***     I do not know how to continue until this problem has been
***     corrected.  If you do not know how to fix it either, please
***     call ${HOTLINE}.
EOF
	    cp ${UNFIXED} ${LIB}
	    exit 1
	fi
        echo '   ' ansi/posix prototypes added to include files
    fi	

    # if we're using classic fixincludes, do old-style limits.h handling
    if [ ! -f ${ITOOLS}/fixincl ]; then
        # See the GCC Makefile, target stmp-fixinc, for the details on
        # limits.h handling.  If fixincludes fixed /usr/include/limits.h,
        # it gets installed as ${LIB}/syslimits.h.
        if [ -f ${LIB}/limits.h ]; then
          rm -f ${LIB}/limits.h.sys
          mv ${LIB}/limits.h ${LIB}/limits.h.sys
        fi

        cp ${UNFIXED}/* ${LIB}

        if [ -f ${LIB}/limits.h.sys ]; then
          rm -f ${LIB}/syslimits.h
          mv ${LIB}/limits.h.sys ${LIB}/syslimits.h
        fi
    else
        cp ${UNFIXED}/* ${LIB}
    fi

    echo Fixed include files installed!

    # go back to whence we came
    cd $CURDIR
}

#### various functions used to verify release 
verify()
{
    if [ "${INSTALLHOST}" != "${HOST}" -a "${NOWARN}" != "1" ] ; then
	cat << EOF
***	This machine appears to be for \"${INSTALLHOST}\", while the
***	software on this tape is meant for \"${HOST}\"  
***
***	You may still run \"$0 --extract ...\" on this machine and then
***	run the other steps on the machine on which you wish to install.
***	You will need to do these steps separately.
***	For more information, please refer to
***     ${ANOTHER_TAPE} in ${NOTES}.
EOF
    else
	popdir=`pwd`
	cd ${LOGDIR}
	STATIC_LINKING="-static"

	# everyone except HP uses LD_LIBRARY_PATH so we set both
	LD_LIBRARY_PATH="${RELEASE_ROOT}/${EXECDIR}/lib"
	SHLIB_PATH="${RELEASE_ROOT}/${EXECDIR}/lib"
	export LD_LIBRARY_PATH SHLIB_PATH

        case ${HOST} in
        mips-sgi-irix5*)
            GPLUSOPTIONS="-rpath ${RELEASE_ROOT}/${EXECDIR}/lib"
            ;;
	powerpc-ibm-aix4.3*)
	    STATIC_LINKING=""
	    ;;
        esac

	verify_c_hello_world
	verify_c_check_fixincludes
	verify_cxx_hello_world
	verify_cxx_initializers

	rm -f hello* test-ioctl* static-init* Hello*
	cd $popdir
    fi
}


#### C hello world

verify_c_hello_world()
{
if [ -f ${RELEASE_ROOT}/${EXECDIR}/bin/gcc ] ; then
    cat > ${LOGDIR}/hello.c <<'EOF'
#include <stdio.h>

extern int printf();

int main(int argc, char **argv) {
        (void) printf("hello: gcc appears to be installed correctly for %s.\n",
                MACHINE);
        return(0);
} /* main() */
EOF

    if ${RELEASE_ROOT}/${EXECDIR}/bin/gcc -Wall -O -g \
	-DMACHINE=\"${INSTALLHOST}\" ${LOGDIR}/hello.c -o ${LOGDIR}/hello ; then
	if ${LOGDIR}/hello ; then
	    echo This is good.
	else
	    echo '***' Hello.c fails to run.  This is a problem.
	    echo '***   ' Please call ${HOTLINE}
	    exit 1
	fi  # ./hello
    else
	echo '***' Hello.c fails to compile.  This is a problem.
	echo '***   ' Please call ${HOTLINE}
	exit 1
    fi  # run gcc

fi # gcc exists
}

### C++ static constructors/destructors

verify_cxx_initializers()
{
if [ -f ${RELEASE_ROOT}/${EXECDIR}/bin/c++ ] ; then
    cat > ${LOGDIR}/static-init.C << 'EOF'
#include <iostream.h>
int Three;

struct Foo {
    Foo(int i) { Three = i; }
};

Foo foo(3);

int main()
{
  if (Three != 3) {
        cout << "static-init: static initialization fails.\n";
        return(1);
  }

  cout << "static-init: static initialization appears to work.\n";
  return(0);
}
EOF

    if ${RELEASE_ROOT}/${EXECDIR}/bin/c++ ${STATIC_LINKING} ${GPLUSOPTIONS} -Wall -O -g \
            -DMACHINE=\"${INSTALLHOST}\" ${LOGDIR}/static-init.C -o ${LOGDIR}/static-init ; then
        if ${LOGDIR}/static-init ; then
            echo This is good.
        else
            echo '***' Static initialization is not working.  This is a problem.
            echo '***   ' Please call ${HOTLINE}
            exit 1
        fi  # run static-init
    else
        echo '***' static-init.c fails to compile.  This is a problem.
        echo '***   ' Please call ${HOTLINE}
        exit 1
    fi  # run g++
fi  #  no g++
}

#### C++ hello world

verify_cxx_hello_world()
{
if [ -f ${RELEASE_ROOT}/${EXECDIR}/bin/c++ ] ; then
    cat > ${LOGDIR}/hello.C << 'EOF'
#include <iostream.h>

int main()
{
  char *machine = MACHINE;
  cout << "hello: g++ appears to be installed correctly for "
        << machine << ".\n";
  return(0);
}
EOF

    if ${RELEASE_ROOT}/${EXECDIR}/bin/c++ ${STATIC_LINKING} ${GPLUSOPTIONS} -Wall -O -g \
            -DMACHINE=\"${INSTALLHOST}\" ${LOGDIR}/hello.C -o ${LOGDIR}/Hello ; then
        if ${LOGDIR}/Hello ; then
            echo This is good.
        else
            echo '***' hello.C fails to run.  This is a problem.
            echo '***   ' Please call ${HOTLINE}
            exit 1
        fi  # ./Hello
    else
        ### this presupposes that hello.c DID compile.
        echo '***' hello.C fails to compile.  This is a problem.
        echo '***   ' Please call ${HOTLINE}
        exit 1
    fi  # run g++
fi
}


#### does the given include file exist under the GCC include file subdir?

check_fixed_file()
{
    if [ ! -f ${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/${HOST}/${GCCvn}/include/$1 ] ; then
        echo '***' You seem to be missing ${RELEASE_ROOT}/${EXECDIR}/lib/gcc-lib/${HOST}/${GCCvn}/include/$1.
        echo '***   ' Most likely you need to run \"Install fixincludes\".
        echo '***   ' Please run \"Install fixincludes\" and
        echo '***   ' then try this test again.  For an explanation see
        echo '***   ' ${WHY_FIXINCLUDES} in ${NOTES}.
        exit 1
    fi  # test fixincluded file
}
  

#### did fixincludes get run correctly?

verify_c_check_fixincludes()
{
if [ -f ${RELEASE_ROOT}/${EXECDIR}/bin/gcc ] ; then
    cat > ${LOGDIR}/test-ioctl.c <<'EOF'
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>

double atof(const char *);

#ifdef __sun__
#include <sys/filio.h>

extern int ioctl();
extern int perror();
#endif /* __sun__ */

extern int printf();

int main(int argc, char **argv) {
#ifdef __sun__
        long l;
        int fd;
        int retval;

        if ((fd = open("/dev/tty", O_RDONLY)) == -1) {
                (void) perror("test-ioctl: can't open /dev/null");
                return(1);
        } else if ((retval = ioctl(fd, FIONREAD, &l)) != 0) {
                (void) perror("test-ioctl: ioctl failed");
                (void) printf("test-ioctl: Most likely, you have not run \"Install fixincludes\" on this machine.\n");
                return(2);
        }
#endif /* __sun__ */

        printf("test-ioctl: ANSI versions of the system header files, (the fixed includes),\n");
        printf("test-ioctl: appear to be installed correctly.\n");

/* that's right.  If closing /dev/tty fails, I don't want to know about it. */

        return(0);
}
EOF

    case "${HOST}" in
	*-sun-sunos4* | mips-dec-ultrix*)
            if ${RELEASE_ROOT}/${EXECDIR}/bin/gcc -Wall -O -g \
                -DMACHINE=\"${INSTALLHOST}\" ${LOGDIR}/test-ioctl.c -o ${LOGDIR}/test-ioctl ; then
                if ${LOGDIR}/test-ioctl ; then
                    echo This is good.
                else
                    case "$?" in
                        1)
                            echo '***' test-ioctl.c fails to read /dev/tty.  If you were running Install
                            echo '***   ' in batch or from a daemon, please try running it again interactively.
                            echo '***   ' If not, please call ${HOTLINE}
                            exit 1
                            ;;
                        2)
                            echo '***' test-ioctl.c fails to ioctl.
                            echo '***   ' Most likely you need to run \"Install fixincludes\".
                            echo '***   ' Please run \"Install fixincludes\" and
                            echo '***   ' then try this test again.  For an explanation see
                            echo '***   ' ${WHY_FIXINCLUDES} in ${NOTES}.
                            exit 1
                            ;;
                        *)
                            echo '***' test-ioctl.c fails to run.  This is a problem.
                            echo '***   ' Please call ${HOTLINE}
                            exit 1
                            ;;
                    esac  # $?
                    exit 1
                fi  # test-ioctl
            else
                ### this presupposes that hello.c DID compile.
                echo '***' test-ioctl.c fails to compile.  This is a problem.
                echo '***   ' Please call ${HOTLINE}
                exit 1
            fi  # run gcc
            ;;
    esac
fi
}


###
### do tape stuff, rewind, etc.
###
### problem is that Unixware uses ``tapdev rewind TAPE'' and mt
### uses ``mt -f TAPE rewind''.  pre-defining TAPE_REWIND and
### TAPE_FSF commands is tricky 'cause you need to make sure that
### you get the user's ``-tape=foo'' command line arg set first.
###
### this was a lot easier.
###
### it takes three command line arguemnts, the canonical host type,
### the mt command you want, and the tape device name.
###

do_mt()
{
    do_mtMTCMD="$1"

    case "$INSTALLHOST" in
        i386-sco3.2v4|*sysv4.2)                       # SVR4 peoples
                         do_mtMTCMD="`echo $do_mtMTCMD | sed s/fsf/rfm/`"
                         tape $do_mtMTCMD $TAPE ;;
        hppa1.1-hp-hpux*|m68k-hp-hpux*|mips-sgi-irix4)  # SVR3 peoples
                         mt -t $TAPE $do_mtMTCMD ;;
        *)                                            # normal peoples
                         mt -f $TAPE $do_mtMTCMD ;;
    esac
    return $?
}

do_mkdir()
{
    DIR="$1"	

    echo Trying to create \"${DIR}\"...
    mkdir ${DIR}
    if [ $? -ne 0 ]; then
	echo '***' I can not make directory, \"${DIR}\".

	### attempt to diagnose...

	DIR=`echo ${DIR} | ${SED} -e 's:/[^/]*$::'`
	if [ ! -d "${DIR}" ] ; then
	    echo '***   ' It looks as though \"${DIR}\" does not even exist.
	    exit 1
	fi

	if [ ! -w "${DIR}" ] ; then
	    echo '***   ' It looks as though you do not have write access to \"${DIR}\".
	    echo '***   ' Please fix this and try running Install again.  For more information
	    echo '***   ' please refer to ${NO_ACCESS} in ${NOTES}.
	    exit 1
	fi

	echo '***   ' I do not know why I can not create \"${DIR}\".  This is a problem.
	echo '***   ' Please call ${HOTLINE}
	exit 1
    fi

    echo Created \"${DIR}\".
}

### trigger words on the command line are:
###     --tape=/dev/device
###     --installdir=/foo
###     --fixincludesdir=/bar
###     bin, src
###     extract, fix, test-installation

while :
do

# Break out if there are no more args
    case $# in
    0)
            break
            ;;
    esac

# Get the first arg, and shuffle
    option=$1
    shift

# Make all options have two hyphens
    orig_option=$option # Save original for error messages
    case $option in
    --*)        ;;
    -*) option=-$option ;;
    esac

# Split out the argument for options that take them
    case $option in
    --*=*)
            optarg=`echo $option | sed -e 's/^[^=]*=//'`
            ;;
    esac

# Now, process the options
    case $option in

    --tape*|--file*)
	TAPE=$optarg;
	### in case this is a tape-image file, make sure that 
	### it's an absolute pathname
	case ${TAPE} in
	/*) ;;
	*)  TAPE=`pwd`/${TAPE} ;;
	esac
	;;

    --installdir*)
        INSTALLDIR=$optarg
        ;;

    --fixincludesdir*)
        FIXINCDIR=$optarg
        ;;

    --with-*)
	case "$option" in
	*=*)    ;;
	*)      optarg=yes ;;
	esac

	withopt=`echo ${option} | sed 's:^--::;s:=.*$::;s:-:_:g'`
	eval $withopt="$optarg"
	withoptions="$withoptions $option"
	;;

    --without-*)
	withopt=`echo ${option} | sed 's:^--::;s:out::;s:-:_:g'`
	eval $withopt=no
	withoutoptions="$withoutoptions $option"
	;;

    ### actions
    *extract*)      ACTIONS="${ACTIONS} extract" ;;
    *fix*)          ACTIONS="${ACTIONS} fix" ;;
    *test*)         ACTIONS="${ACTIONS} test" ;;

    ### subsets of the distribution
    *src* | *sour*) 
        install_src=true
        ;;
    *bin*)
        install_bin=true
        ;;

    --version)
        print_version_message
        exit 0
        ;;
    --help)     
        print_help_message
	exit 1
        ;;

    --*)
        echo "Install: Unrecognized option: \"$orig_option\"; use --help for usage." >&2
        exit 1
        ;;
    esac
done


###
###
### check for a myriad of sins
###
###

if [ ! -d "${INSTALLDIR}" ] ; then
    do_mkdir "$INSTALLDIR"
fi

cd "${INSTALLDIR}"
if [ $? -ne 0 ]; then
    echo '*** ' Can not cd to \"${INSTALLDIR}\" because I do not have execute permission.
    ls -lad ${INSTALLDIR}
    echo '*** ' Please fix this and then try running Install again.
    exit 1
fi   # ! -d ${INSTALLDIR}

# Make INSTALLDIR absolute (parts below might need it that way).
# FIXME, do they really?
# FIXME, handle pwd failure from upper dir permission
INSTALLDIR=`cd ${INSTALLDIR} ; pwd`
RELEASE_ROOT=${INSTALLDIR}/${RELEASE_TAG}
LOGDIR=${RELEASE_ROOT}

eval guess_system

### where the binaries are in the release
EXECDIR=
#EXECDIR=

### if neither binaries nor source were specified, assume 
### user meant for both to be installed.
if $install_bin || $install_src; then
	:
else
	install_bin=true
	install_src=true
fi

### If no actions are specified on the command line, then use the
### default actions.
if [ -z "${ACTIONS}" ] ; then
    ACTIONS="extract fix test"
fi # ${ACTIONS}


### if binaries are intended for this machine
if [ "${INSTALLHOST}" = "${HOST}" ] ; then
    for action in ${ACTIONS} ; do
        case ${action} in
        test)
            OURWD=`(cd ${INSTALLDIR}; pwd) 2>/dev/null`
            if [ -d ${ROOTING_REAL} ] ; then
                DEFAULTWD=`(cd ${ROOTING_REAL}; pwd) 2>/dev/null`
            else
                DEFAULTWD=${ROOTING_REAL}
            fi

            case ${BUNDLE} in
            comp-tools | cross-tools)
                if [ "${OURWD}" != "${DEFAULTWD}" ] ; then
                    eval show_exec_prefix_msg
                fi
                ;;
            emacs)
                if [ "${OURWD}" != "${DEFAULTWD}" ] ; then
                    eval show_emacs_alternate_msg
		    exit 1
                fi
                ;;
            esac
            ;;
        esac
    done
fi


### if we can't read the tape drive...
popdir=`pwd`

if [ ! -d "${RELEASE_ROOT}" ] ; then
    if mkdir ${RELEASE_ROOT} ; then
        true
    else
        do_mkdir $RELEASE_ROOT
    fi
else
    if [ ! -w "${RELEASE_ROOT}" ] ; then
        echo '*** ' Cannot write to \"${RELEASE_ROOT}\" because I do not have write permission.
        ls -lad ${RELEASE_ROOT}
        echo '*** ' Please fix this and then try running Install again.
        exit 1
    fi   # ! -r ${RELEASE_ROOT}
fi   # ! -d ${RELEASE_ROOT}


### There is a window here that I don't know how to handle gracefully from
### shell.  If the directory DID exist and WAS executable but dissappeared
### before we could cd into it, we're kinda hosed.  If you get a call about
### this, make sure the directory exists, is readable, is executable, is the
### either the default installation directory or the -installdir=directory
### given on the command line.  Then ask them to just try again.  A few times.
### If you get the same error every time, we've got a very strange bug.  I
### can't help you.

cd ${INSTALLDIR}
if [ $? -ne 0 ] ; then
    echo '***' Cannot cd to \"${INSTALLDIR}\".  This is a problem.
    echo '***   ' For further help, please refer to
    echo '***   ' ${NO_ACCESS} in ${NOTES} or call
    echo '***   ' ${HOTLINE}
    exit 1
fi

### Make sure that the tape device is readable.  This could also be
### triggered when the "tape" is really a compressed tar file image
### which is not readable.  In that case, calling it a "tape" is a
### misnomer.

if echo ${ACTIONS} | grep extract > /dev/null ; then
    if [ ! -r "${TAPE}" ] ; then
        echo '***' Can not read from TAPE or FILE, \"${TAPE}\".
        cd ${popdir} ; ls -lasd ${TAPE}
        echo '***   ' Try using \"./Install --tape=/dev/something ...\" or
        echo '***   '           \"./Install --file=/some/file ...\" to name your tape
        echo '***   ' drive/file or refer to ${MIGHT_WRONG} in
        echo '***   ' ${NOTES}.
        exit 1
    fi  # ! -r ${popdir}/${TAPE}
fi   # extract in ${ACTIONS}

cd ${popdir}

###
###
### extraction
###
###

if echo ${ACTIONS} | grep extract > /dev/null 2>&1 ; then
    if [ ! -d "${INSTALLDIR}" ] ; then
	do_mkdir "${INSTALLDIR}"
    fi

    if [ ! -d "${RELEASE_ROOT}" ] ; then
	do_mkdir "${RELEASE_ROOT}"
    fi

    cd ${RELEASE_ROOT}
    if [ $? -ne 0 ] ; then
        echo '***' I can not cd to \"${RELEASE_ROOT}\".

        ### attempt to diagnose...

        if [ ! -r "${RELEASE_ROOT}" ] ; then
            echo '***   ' It looks as though you do not have execute permission to \"${RELEASE_ROOT}\".
        fi  # ! -r ${RELEASE_ROOT}

        echo '***   ' For further help, please refer to
        echo '***   ' ${NO_ACCESS} in ${NOTES}.
        exit 1
    fi  # cd status is zero

    for trigger in ${TRIGGERS} ; do
	if [ -r "${trigger}" ] ; then
	    echo '***' \"${RELEASE_ROOT}/${trigger}\" exists and I will not overwrite it.
	    echo '***   ' If you really want to install it again, please remove it first.
	    exit 1
        fi  # -r ${trigger}
    done  # for trigger

    TARLOG=${LOGDIR}/extract.log
    echo It will take some time for me to read the tape.
    echo Verbose output will be saved in ${TARLOG}.
    echo Extracting into \"${LOGDIR}\"... 

    ### make sure things come off the tape as they were written, but minus
    ### what could be a security hole.
    umask 0

    if $install_bin; then
	### ${TAPE} might be a disk image of the tape, so don't try to do tape
	### operations on a file
	if [ -b ${TAPE} -o -c ${TAPE} ]; then

	    ### using the no rewind device, so rewind and fsf to be sure.
	    eval do_mt rewind
	    if [ $? -ne 0 ]; then
		echo '***' Could not rewind tape device \"${TAPE}\".
		exit 1
	    fi

	    eval do_mt fsf
	    if [ $? -ne 0 ]; then
		echo '***' Could not forward space tape device \"${TAPE}\".
		exit 1
	    fi  
	fi

	${DD} ${DDOPTS} if=${TAPE} | \
		compress -d | ${TAR} ${TARFLAGS} 2>> ${TARLOG}

	if [ $? -ne 0 ]; then
	    cat << EOF
*** There appears to have been an extraction error.
***   The output from the tar command has been logged in ${TARLOG}
***   I do not know how to continue until this problem has been
***   corrected.  If you do not know how to fix it either, please
***   call ${HOTLINE}.
EOF
	    exit 1
	fi
	# The filenames in gcc-lib/<target> are too darn long, so
	# here's a workaround
	if [ -f ${EXECDIR}/lib/gcc-lib/${TARGET}.tar ] ; then
	    (cd ${EXECDIR}/lib/gcc-lib
	    ${TAR} ${TARFLAGS} < ${TARGET}.tar 2>> ${TARLOG})
	    if [ $? -ne 0 ]; then
	        cat << EOF
*** There appears to have been an extraction error.
***   The output from the tar command has been logged in ${TARLOG}
***   I do not know how to continue until this problem has been
***   corrected.  If you do not know how to fix it either, please
***   call ${HOTLINE}.
EOF
	        exit 1
	    else
	        (cd ${EXECDIR}/lib/gcc-lib; rm -f ${TARGET}.tar 2>> ${TARLOG})
	    fi
	fi
    fi

    if $install_src; then
	### ${TAPE} might be a disk image of the tape, so don't try to do tape
	### operations on a file
	if [ -b ${TAPE} -o -c ${TAPE} ]; then

	    ### using the no rewind device, so rewind and fsf to be sure.
	    eval do_mt rewind
	    if [ $? -ne 0 ]; then
		echo '***' Could not rewind tape device \"${TAPE}\".
		exit 1
	    fi

	    eval do_mt fsf
	    if [ $? -ne 0 ]; then
		echo '***' Could not forward space tape device \"${TAPE}\".
		exit 1
	    fi  

	    eval do_mt fsf
	    if [ $? -ne 0 ]; then
		echo '***' Could not forward space tape device \"${TAPE}\".
		exit 1
	    fi  
	fi

	${DD} ${DDOPTS} if=${TAPE} | \
		compress -d | ${TAR} ${TARFLAGS} 2>> ${TARLOG}

	if [ $? -ne 0 ]; then
	    cat << EOF
*** There appears to have been an extraction error.
***   The output from the tar command has been logged in ${TARLOG}
***   I do not know how to continue until this problem has been
***   corrected.  If you do not know how to fix it either, please
***   call ${HOTLINE}.
EOF
	    exit 1
	fi
    fi

    echo Cygnus Support software distribution extracted!
fi


###
###
### fix (header files)
###
###

if echo ${ACTIONS} | grep fix > /dev/null 2>&1 ; then
    fix
fi


###
###
### verify the installation
###
###

if echo ${ACTIONS} | grep test > /dev/null 2>&1 && $install_bin ; then
    verify
    echo Cygnus Support software distribution tested!
fi

echo Done.
exit 0

###
### Local Variables:
### comment-column: 0
### fill-column: 131
### End:
###

### End of Install
