#!/bin/sh
#
# The cris tools installer.  Will install a binary distribution or a source
# distribution based on whatever files are close-by.
#
# Stuff is stolen from the Axis "axinstall" printserver installation script
# and GNU configure, the latter found in the source of the programs to install.
#
#  arg1=target-install-dir arg2=target-bin-dir arg3=man-dir arg4=info_dir arg5=temp-dir 
#
ICT_VERSION=1.0
#
# * Version 1.0: 1998-07-18:
# A first trial release; there may be documentation bugs and bugs
# in the documentation handling (man, info) since it is largely untested.
#
# For binary distributions, there may be all sorts of bugs. 
#

archname=cris
contact=hp@axis.com

program0=install-$archname-tools
program=`echo $program0 v$ICT_VERSION`
default_lib=/usr/local/$archname/lib/gcc-lib/$archname;
default_bin=/usr/local/$archname
default_tmp=./tmpdir
dist_os_file=dist-os
default_lib_file=dist-libdir
dist_gccver_file=dist-gccver
dist_utils_file=dist-utils
default_mandir=/usr/local/man/man1
default_infodir=/usr/local/info
compiler_binaries=`echo gcc-$archname ar-$archname objdump-$archname ranlib-$archname`
infodir_file=dist-infodir
startdate=`date`;

#
# Maybe choose another shell for some reason.
#

# --- if we do have a /bin/sh5 then use that instead 

test "X$1" != "Xuse_sh5" && test "X$1" != "Xuse_ksh" && test -f /bin/sh5 && exec /bin/sh5 $0 use_sh5 $*

# --- if we're running HP-UX, use ksh instead

OSNAME=
test -f /usr/bin/uname && OSNAME=`/usr/bin/uname -sr`
test -f /bin/uname && OSNAME=`/bin/uname -sr`

OSVERSION=
test -f /usr/bin/uname && OSVERSION=`/usr/bin/uname -v`

echo $OSNAME | grep "HP-UX" >/dev/null
test $? = 0 && test "X$1" != "Xuse_sh5" && test "X$1" != "Xuse_ksh" && test -f /bin/ksh && exec /bin/ksh $0 use_ksh $*

if test "X$1" = "Xuse_sh5" || test "X$1" = "Xuse_ksh"; then
 shift;
fi;

#
# We may now, after removing "other shell" arguments, extract the "normal"
# arguments.
#
lib_arg=$1; bin_arg=$2; man_arg=$3; info_arg=$4; tmp_arg=$5;

# -----------------------------------------------------------------------------

test -f /usr/bsd/clear  && clear=/usr/bsd/clear
test -f /usr/ucb/clear  && clear=/usr/ucb/clear
test -f /bin/clear      && clear=/bin/clear
test -f /usr/bin/clear  && clear=/usr/bin/clear

test -f /usr/etc/mknod  && mknod=/usr/etc/mknod
test -f /etc/mknod      && mknod=/etc/mknod
test -f /etc/sbin/mknod && mknod=/etc/sbin/mknod

test -f /usr/bin/cut    && cut=/usr/bin/cut
test -f /bin/cut        && cut=/bin/cut

test -f /usr/bin/tee    && tee=/usr/bin/tee
test -f /bin/tee        && tee=/bin/tee

# --- echo come in many colours. ----------------------------------------------

madepaths=;

echo_n=NO
test X`echo -n` = X && echo_n=YES;

cleanup()
{
#
# Remove all made directories unless it is one on the paths to $*
#
  for removedir in `echo $madepaths`; do
    keep=false;
    for keepdir in $*; do
      case $keepdir in
        $removedir/*) keep=true; break;;
        *) ;;
      esac;
    done;
    if not $keep; then
      #echo *** Removing $removedir and below.
      rm -rf $removedir 2>/dev/null;
    # else echo Keeping $removedir;
    fi;
  done;
}

myecho()
{
   if test X"$1" = "X-n"; then
      shift
      test $echo_n = YES && echo -n "$*"
      test $echo_n = NO  && echo "$*" '\c'
   else
      echo $*
   fi
}

echodo()
{
 # Eval used to make an argument "more than one" expand into multiple
 # arguments for visibility, while retaining the one-ness for command
 # execution. 

 eval echo $*;
 eval "$*";
}

# --- Prompt with default for a yes or no answer. -----------------------------
# --- Leave answer in yes_or_no.

bug_report()
{
  echo; echo "Please email a bug report to $contact";
  echo "The bug report should have a subject containing the text '$program0-bug'";
  echo "and the file '$instalog'";
  echo "in the message body";
  
  touch $errfile || echo "*** Could not mark error by creating $errfile";

  echo "*** The installation is aborted, the date is:" `date`;

  cleanup $instalog $errfile;
}

yes_or_no=
yes_or_noP()
   # $1 is prompt string.
   # $2 is default answer (yes/no).
{
   yes_or_no=
   while true; do
      myecho -n "$1 [yn] ? (default $2): "; read yes_or_no

      test "X$yes_or_no" = "X" && yes_or_no=$2

      case $yes_or_no in
      y | Y | yes | Yes | YES)
         yes_or_no=YES
         break # out of the while loop
         ;;
      n | N | no | No | NO)
         yes_or_no=NO
         break # out of the while loop
         ;;
      *)
         yes_or_no=
         ;;
      esac
   done
}

# --- Simple prompt with default for a text string answer.
# --- Leave answer in variable answer.

answer=
answerP()
   # $1 is prompt string.
   # $2 is default answer.
{
   answer=
   if test "X$2" !=  "X"; then
      myecho -n "$1 (default $2): "; read answer
   else
      myecho -n "$1":; read answer
   fi
   test "X$answer" = "X" && answer=$2
}

# --- Change relative paths tp absolute paths
# --- Leave answer in variable abs_path
abs_path=
pwdify()
   # $1 is a path, perhaps "./"-prefixed or "."
{
 abs_path=$1
 case $1 in
  /*) ;;
  .)   abs_path=`pwd`;;
  ./*) abs_path=`pwd`/`echo $1 | $cut -c3-`;;
  *)   abs_path=`pwd`/$1;;
 esac
}

# --- Simple prompt with default for a file path and name.
# --- The path MUST begin with a / or "./" or be "."
# --- Leave answer in variable path_answer.

path_answer=
path_answerP()
   # $1 is prompt string.
   # $2 is default answer.
{
   path_answer=
   while true; do
      if test "X$2" !=  "X"; then
         myecho -n "$1 (default $2): "; read path_answer
      else
         myecho -n "$1":; read path_answer
      fi
      test "X$path_answer" = "X" && path_answer=$2;
      pwdify $path_answer;
      path_answer=$abs_path;
      if has_permission $path_answer; then
       break;
      else
       echo "Wrong answer: \"$path_answer\" is not writable by you.";
      fi;
   done
}

# --- Prompt for one character of a given character class.
# --- Leave answer in variable range_answer

range_answer=
range_answerP()
   # $1 is prompt string.
   # $2 is allowed answer characters class, in range [1-X], X>9. 
   # $3 is default answer (must belong to class in $2).
{
   range_answer=
   while true; do
      if test "X$3" != "X"; then
         myecho -n "$1" "$2" "(default $3): "; read range_answer
      else
         myecho -n "$1 $2: "; read range_answer
      fi

      test "X$range_answer" = "X" && range_answer=$3

      low=`echo $2 | $cut -c2`
      hi=`echo $2 | $cut -c4,5`

      if test $low -le $range_answer -a $range_answer -le $hi; then
         break # out of the while loop.
      else
         range_answer=
      fi
   done
}


# --- Prompt for one character of a given character class.
# --- Leave answer in variable class_answer

class_answer=
class_answerP()
   # $1 is prompt string.
   # $2 is allowed answer characters class, e.g. [0-9] for one of the digits.
   # $3 is default answer (must belong to class in $2).
{
   class_answer=
   while true; do
      if test "X$3" != "X"; then
         myecho -n "$1" "$2" "(default $3): "; read class_answer
      else
         myecho -n "$1 $2: "; read class_answer
      fi

      test "X$class_answer" = "X" && class_answer=$3

      case $class_answer in
      $2)
         break # out of the while loop.
         ;;
      *)
         class_answer=
         ;;
      esac
   done
}

# --- Say not if the parameter = NO

say_not()
{
   test "X$1" = "XNO" && echo " NOT"
}

# --- Say y or n when $1 is YES or NO.

say_y_or_n()
{
   test "X$1" = "XYES" && echo y
   test "X$1" = "XNO"  && echo n
}

# --- Say i or o when $1 is if or of.

say_io()
{
   test "X$1" = "Xif" && echo i
   test "X$1" = "Xof" && echo o
}

# --- Say in or out when $1 is if or of.

say_in_or_out()
{
   test "X$1" = "Xif" && echo input
   test "X$1" = "Xof" && echo output
}

# --- Test if we hawe write permission in the directory.

has_permission()
   # $1 is path to test
{
   if test ! -d $1; then
      has_permission `dirname $1`; return
   elif test -w $1; then
      true; return
   else
      false; return
   fi
}

# --- Create the directory and its path if it does not exist.

mkdir_path()
   # $1 is path to test
{
   if test ! -d $1; then
     mkdir_path `dirname $1` && mkdir $1 && madepaths=`echo $madepaths $1`;
   else
    test -w $1; return
   fi
}

# --- not <command> do boolean negation on return value of <command>.

not()
{
   if $*; then
      false; return
   else
      true; return
   fi
}

# --- Welcome.  The main program starts here. ------------------------------------

# Remove write permissions for g o
umask 022

#
# Try to find things
#
srcpath=`echo $0 | sed 's|//|/|' | sed 's|/[^/]*$||'`
if test "x$srcpath" = "x$0";
then
srcpath=.
fi;

case $srcpath in
 /*) srcpath=$srcpath;;
 *) srcpath=`pwd`/$srcpath;;
esac;

#
# Check the type of installation now.  For a binary installation, we need no
# temporary files.  We can go right away.
#
if test -f $srcpath/*lib-bin.tar; then
 bindist=YES;
else
 bindist=NO;
fi

#
# Check for standard OS:es we have tested.
#
osfound=NO
altlist="SunOS4-sparczSolaris2-sparczAIXzHP-UXzFreeBSD-intelzLinux-intel"
greplist='SunOS 4[^ ]* sun4zSunOS 5[^ ]* sun4zAIXzHP-UXzFreeBSD [^ ]* i[3-9]86zLinux 2[^ ]* i[3-9]86'
num_alts=6
oscnt=1
OSNAME=

test -f /usr/bin/uname && OSNAME=`/usr/bin/uname -srm` && OSTOT=`/usr/bin/uname -a`
test -f /bin/uname && OSNAME=`/bin/uname -srm` && OSTOT=`/bin/uname -a`

while test $osfound != YES -a $oscnt -le $num_alts; do
   sys_type=`echo $altlist | $cut -f$oscnt -dz`
   sys_grep=`echo $greplist | $cut -f$oscnt -dz`

   echo $OSNAME | grep "`echo $sys_grep`" >/dev/null
   if test $? = 0; then
      osfound=YES
      ostype=$sys_type;
   else
      oscnt=`expr $oscnt + 1`
   fi
done

#foo=`echo "'"$greplist"'"`
#echo $foo
#echo $srcpath $bindist "'"$OSTOT"'" "'"$OSNAME"'" $osfound "'"$ostype"'";
#exit 0;

cat << WELCOME_END

================================================================================
  This is the $archname-tools installation script.  This program will lead you
 through a procedure to install the $archname (Etrax) compilation tools on your
 system.
  You can stop this program with the interrupt key at any time, and an
 installation log will be saved to a file, after the installation starts.

WELCOME_END

if test $bindist = YES; then
 echo "   The tools are in a precompiled binary distribution for" `cat $srcpath/$dist_os_file`;
 if test $bindist = YES -a "X$ostype" != "X`cat $srcpath/$dist_os_file`"; then
  echo " However, the system, type $OSNAME, is not a system where the binary can run";
  if test $osfound = NO; then
   echo " In fact, *no* binary distribution exists for $OSNAME";
  else
   echo " You should get the *correct binary distribution*"
   echo " Try the file with " `echo $ostype | tr ' ' '-'` "in the name";
  fi;
  echo "*** Installation aborted";
  exit 1;
 fi;
 echo " which matches this system, of type $OSNAME";
 default_lib=`cat $srcpath/$default_lib_file`;
 compiler_binaries=`cat $srcpath/dist_utils_file`;
 gccver=`cat $srcpath/$dist_gccver_file`;
else
 echo " The tools are in a source distribution, the tools will be compiled.";
 echo " (Now is a good time to exit or renice this script; it will take a long time."
 echo "  The renice command may be: \"renice 10 $$\" depending on the type of this system)";
 #
 # If the system is not "supported" then let the user stop here.
 #
 if test $osfound = NO; then
   echo "Your system has not been tested for installation of this distribution"
   echo "Proceed at own risk"
   yes_or_noP "Do you want to quit now" y
   if test $yes_or_no = YES; then echo "*** Installation aborted"; exit 0; fi
 fi;
 #
 # Find out the version of the compiler
 #
 srcpoth=`echo $srcpath | tr '/' ':'`;
 gccver=`echo $srcpath/gcc-* | tr '/' ':' | sed -e s/$srcpoth:gcc-'\(.*\)/\1/;'`;
 default_lib=$default_lib/$gccver;
fi;

#
# Make sure the user is on the right track in the first place.
#
yes_or_noP "Do you want to continue" y
test $yes_or_no = NO && exit 0

echo; echo;

#
# Make the user confirm the arguments or default-values.
# Check the permissions.
#

# Loop until user gets tired or accepts.
while true; do

# Compiler libraries

if test "X$lib_arg" != "X"; then
 pwdify $lib_arg;
 specify="You have specified that";
 libpath=$abs_path;
else
 specify="The default value";
 libpath=$default_lib;
fi;

echo "  $specify" '"'$libpath'"';
echo " is the directory where the compiler-libraries will be installed.  It has to";
echo " end in" '"'$archname/$gccver'"' as '"'$default_lib'"' "does.  (I will add it.)";
echo " I want it to be empty, so remove any previous installation now.  If you plan";
echo " to move these files in the future, you should consider to re-install instead.";
echo;

while true; do
 path_answerP "Choose directory" $libpath;

 case $path_answer in
 */$archname/$gccver) ;;
 *)  path_answer=$path_answer/$archname/$gccver; echo; echo "(I added $archname/$gccver to make $path_answer)";;
 esac

 if test -f $path_answer/cpp || test -d $path_answer/include; then
  echo "The directory $path_answer is not empty.  Please remove any previous installation.";
 else
  break;
 fi;
done;

libpath=$path_answer;
lib_arg=$libpath;

echo; echo;

if test "X$bin_arg" != "X"; then
 specify="You have specified that";
 pwdify $bin_arg;
 binpath=$abs_path;
else
 specify="The default value";
 binpath=$default_bin;
fi;

# Compilation tools

echo "  $specify $binpath";
echo " is the directory where the compiler tools, with the names";
echo  " " $compiler_binaries;
echo " will be installed.";
echo;

while true; do
 path_answerP "Choose directory" $binpath;
 if not has_permission $path_answer; then
  echo "You do not have permissions to create or write in $path_answer";
 elif test -f $path_answer/gcc-$archname || test -d $path_answer/objdump-$archname; then
  echo "There is a previous installation in $path_answer:";
  ls -l $path_answer/*-$archname;
  echo "Please remove the previous installation.";
 else
  break;
 fi;
done;

binpath=$path_answer;
bin_arg=$binpath;

echo; echo;

# Manpath

if test "X$man_arg" != "X"; then
 pwdify $man_arg;
 specify="You have specified that";
 manpath=$abs_path;
else
 specify="The default value";
 manpath=$default_mandir;
fi;

new_man=NO;
echo "  $specify $manpath";
echo " is the directory for the" '(Unix-type)' "manuals.";
echo " If you do not want to install the manuals, specify /dev/null";
echo " The specified path must end in" '"/man1"';
echo;

while true; do
 path_answerP "Choose directory" $manpath;
 if test "X$path_answer" = "X/dev/null"; then
  break;
 else
  case $path_answer in
   */man1) ;;
   *) path_answer=$path_answer/man1; echo; echo "(I added /man1 to make $path_answer)";;
  esac
 fi;

 if  not has_permission $path_answer; then
  echo "You do not have permissions to create or write in $path_answer";
 elif test -f $path_answer/gcc.1; then
  echo "There is a previous installation of manuals there:";
  ls -l $path_answer/*.1;
 elif test -d $path_answer; then
  break;
 else
   echo "There was no directory called $path_answer.";
   echo "You'll also need to modify your \$MANPATH";
   yes_or_noP "Is it ok to create $path_answer anyway" y;
   if test $yes_or_no = YES; then
    new_man=YES;
    break;
   fi;
 fi;
done;

manpath=$path_answer;
man_arg=$manpath;

echo; echo;

# Info files

if test "X$info_arg" != "X"; then
 pwdify $info_arg;
 specify="You have specified that";
 infopath=$abs_path;
else
 specify="The default value";
 infopath=$default_infodir;
fi;

ro_infodir=NO;
echo "  $specify $infopath";
echo " will be the directory for the GNU info-files.";
echo " If you do not want to install the info-files, specify /dev/null";
echo;

while true; do
 path_answerP "Choose directory" $infopath;
 if test "X$path_answer" = "X/dev/null"; then
  break;
 fi;

 if not has_permission $path_answer; then  echo "You do not have permissions to create or write in that directory.";
 elif test -f $path_answer/gcc.info || test -f $path_answer/binutils.info || test -f $path_answer/bfd.info; then
  echo "There is a previous overlapping installation of info files in $path_answer:";
  ls -l $path_answer/gcc.info* $path_answer/binutils.info*  $path_answer/bfd.info*;
  echo "Please remove the installation, or choose another directory.";
 else
   if not has_permission $path_answer/dir; then
    echo "I do not have permissions to modify the index-file $path_answer/dir";
    if not has_permission $path_answer/dir2; then
     echo "I need at least to modify dir2.  Give me another info-path";
    else
     echo "I will remind you to remedy this at the end of the installation";
     ro_infodir=YES;
     break;
    fi;
   else
    break;
   fi;
 fi;
done;

infopath=$path_answer;
info_arg=$infopath;

echo; echo;

# Tempdir

if test "X$tmp_arg" != "X"; then
 pwdify $tmp_arg;
 specify="You have specified that";
 tmppath=$abs_path;
else
 specify="The default value";
 tmppath=$default_tmp;
fi;

echo "  $specify $tmppath";
echo " is the directory for temporary files during the installation";
echo;

while true; do
 path_answerP "Choose directory" $tmppath;
 if  not has_permission $path_answer; then
  echo "You do not have permissions to create or write in $path_answer.";
 else
  break;
 fi;
done

tmppath=$path_answer;
tmp_arg=$tmppath;

echo; echo; echo;
echo " Please confirm";
echo " Compiler tools libraries:        $libpath";
echo " Compiler tools binaries:         $binpath";
echo " Manuals:                         $manpath";
echo " Info-files:                      $infopath";
echo " Temporaries during installation: $tmppath";

yes_or_noP "Use these directories" y;

if test $yes_or_no = YES; then
 break;
else
echo;
echo "Here we go again."; echo;
fi;

done;

echo;

cat << WELCOME_2_END
  Invoke an equivalent session as:
  $0 $libpath $binpath $manpath $infopath $tmppath
================================================================================

WELCOME_2_END

# Go down one level, create a session-specific temporary directory.
tmppath=$tmppath/$program0.$$;

#
# A logfile is good to have.
#

mytmpdir=$tmppath;
if not mkdir_path $mytmpdir; then
 echo "Could not create temporary directory $mytmpdir.  Why?  I just checked!"
 exit 1;
fi;

instalog=$mytmpdir/$program0"_log"
errfile=$mytmpdir/$program0"_log.err"

rm -f $errfile 2>/dev/null;

cat << START_MSG_END

--------------------------------------------------------------------------------

                              $program
                              
       An installation log will be saved in the file
       $instalog
       
--------------------------------------------------------------------------------
START_MSG_END

supported=`say_not $osfound`;
cat > $instalog << INSTALOG_HEAD_END
===================== $program installation log ======================

The program run is:     $0
The log file is:        $instalog
Sources were in:        $srcpath
Compiler libraries in:  $libpath
Compiler binaries in:   $binpath
Installation temps:     $tmppath
Manual files:           $manpath
New mandir:             $new_man
Info files:             $infopath
Non-writable dir:       $ro_infodir
The system is a:        $OSNAME
The total system id is: $OSTOT
It is$supported officially supported.
The installation date:  $startdate
INSTALOG_HEAD_END

echo 
if mkdir_path $libpath; then echo "Libraries $libpath ok" >>$instalog;
else
  echo "*** Libraries path $libpath could not be created: $?" | $tee -a $instalog;
  bug_report;
  exit 1;
fi

if mkdir_path $binpath; then echo "Binaries $binpath ok" >>$instalog;
else
  echo "*** Binaries path $binpath could not be created: $?" | $tee -a $instalog;
  bug_report;
  exit 1;
fi

if test $manpath != /dev/null; then
 if mkdir_path $manpath; then echo "Manuals $manpath ok" >>$instalog;
 else
   echo "*** Manpath $manpath could not be created: $?" | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;
fi

if test $infopath != /dev/null; then
 if mkdir_path $infopath; then echo "Info-files $infopath ok" >> $instalog;
 else
   echo "*** Info path $infopath could not be created: $?" | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;
fi

if test $bindist = YES; then
 # A binary installation is simple.  Just untar it where we want it.
 # I dunno how to check that there is room there, it's up to the user.

 echo "*** Starting extraction from binary distribution " *lib-bin.tar "..." | $tee -a $instalog;
 if (cd $libpath; tar fx *lib-bin.tar) >>$instalog 2>&1; then
   echo "... Succeeded." >>$instalog;
   sanepath=$libpath/$archname/`cat $srcpath/$dist_gccver_file`;
   if test -x $sanepath/cpp ; then
     echo "*** Sanity check ok." >>$instalog;
   else
     echo "*** Can't find the preprocessor.  It is supposed to be in" | $tee -a $instalog;
     echo $sanepath/cpp "but ls shows:" | $tee -a $instalog;
     ls -l $sanepath | $tee -a $instalog;
     bug_report;
     exit 1;
   fi
 else
   echo "*** Extraction of the compiler libraries failed." | $tee -a $instalog;
   bug_report;
   exit 1;
 fi

 echo "*** Starting extraction of programs " *prog-bin.tar "..." | $tee -a $instalog;
 if (cd $binpath; tar fx *prog-bin.tar) >>$instalog 2>&1; then
   true;
 else
   echo "*** Extraction of the compiler and utilities programs failed." | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;
 echo "... Succeeded." >>$instalog;

 # Do some sanity checks.  Make scripts setting GCC_EXEC_PREFIX before
 # calling the binary (unless default path is used).  Make links to utility
 # programs.  Testcompile hello world (with simulator library) and check magic.
 if test -x $binpath/gcc-$archname.real ; then
   echo "*** Can't find the compiler shell program.  It is supposed to be in" | $tee -a $instalog;
   echo $sanepath/cpp "but ls shows:" | $tee -a $instalog;
   ls -l $sanepath | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;

 echo "*** Sanity check 1 ok." >>$instalog;

 distlib=`cat $srcpath/$default_lib_file`;
 if test $libpath = $distlib; then
   mv $binpath/gcc-$archname.real $binpath/gcc-$archname;
   echo "*** Lib-path $libpath equals $distlib, renamed gcc-$archname.real" >>$instalog;
 else
   echo "#!/bin/sh" >$binpath/gcc-$archname;
   echo "GCC_EXEC_PREFIX=$libpath gcc-$archname.real" '$*' >$binpath/gcc-$archname;
   chmod a+x $binpath/gcc-$archname;
   echo "*** Lib-path $libpath != $distlib, shelled gcc-$archname.real" >>$instalog;
 fi;
 for file in `cat $srcpath/$dist_utils_file`; do
   if ln -s $sanepath/`basename $file -$archname` $binpath/$file; then 
     echo "*** Linked $file" >>$instalog;
   else
    echo "*** Symlinking $file into $binpath failed" | $tee -a $instalog;
    bug_report;
    exit 1;
   fi;
 done;

 echo "*** Testing the host compiler ..." | $tee -a $instalog;
 echo 'main() { printf("\nHello, world!\n"); return 0;}' >$tmppath/test.c
 if ($binpath/gcc-$archname -sim -o $tmppath/testit $tmppath/test.c && $binpath/magic-$archname  $tmppath/testit) >>$instalog 2>&1; 
   then echo "*** Sanity check 2 ok." >>$instalog;
 else 
   echo "*** Installation problem: Compiling and checking did not produce the expected results" | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;

 if test $manpath != /dev/null; then
   echo "*** Starting extraction of manuals " *man-bin.tar "..." | $tee -a $instalog;
   if (cd $manpath; tar fx *man-bin.tar) >>$instalog 2>&1; then
    true;
   else
    echo "*** Could not install the manuals: $?" | $tee -a $instalog;
    bug_report;
    exit 1;
   fi;
 fi;

 if test $infopath != /dev/null; then
   echo "*** Starting extraction of info-files " *info-bin.tar "..." | $tee -a $instalog;
   if (cd $infopath; tar fx *info-bin.tar) >>$instalog 2>&1; then true; else
    echo "*** Could not install the info-files: $?" | $tee -a $instalog;
    bug_report;
    exit 1;
   fi;

   if $ro_infodir = NO; then
     if not cat $srcpath/$infodir_file >>$infopath/dir 2>>$instalog; then
       echo "*** Could not update the info-dir-file: $?" | $tee -a $instalog;
       bug_report;
       exit 1;
     fi;
   else
     if not cp $srcpath/$infodir_file $infopath/dir2 2>>$instalog; then
       echo "*** Could not copy the dir-file to $infopath/dir2: $?" | $tee -a $instalog;
       bug_report;
       exit 1;
     fi;
   fi;
 fi;

else
 # Source distribution.

 #
 # Prepare for compiling the four parts: utilities, binutils, compiler, libraries.

 #
 # Set up environment variables; some are shortnames for long paths.
 #

 gccobj=$tmppath/gcc; binuobj=$tmppath/binutils; csupobj=$tmppath/csup; csupobj2=$tmppath/rom;
 libsubdir=$libpath;
 bob=$binuobj/binutils;
 ngcc=$binpath/gcc-$archname;
 nar=$binpath/ar-$archname;
 nranlib=$binpath/ranlib-$archname;

 #
 # Make sure that libgccdir (standard_exec_prefix) has $archname/$gccver removed
 # if specified, so it can be used with -V and -b options.
 # Anyway, libsubdir should be exactly as specified (it normally has
 # $archname/$gccver) as the final components.
 #
 case $libpath in
  */$archname/$gccver) libgccdir=`dirname $libpath`; libgccdir=`dirname $libgccdir`;;
  *) echo "*** $program: Internal inconsistency: $libpath does not end in $archname/$gccver" | $tee -a $instalog; exit 1;;
 esac

 #
 # Make directories from some of those variables
 #
 if { mkdir_path $gccobj && mkdir_path $binuobj \
      && mkdir_path $csupobj && mkdir_path $csupobj2; } >>$instalog 2>&1; then true; else
   echo "*** Could not create temporary files in $tmppath" | $tee -a $instalog;
   bug_report;
   exit 1;
 fi;

 echo "*** Created object directories $gccobj $binuobj $csupobj and $csupobj2" >> $instalog;

 # First, we need to determine if gcc is installed on this system.  Do it by
 # test-compiling hello world and checking the output and return code.
 #  If there is a gcc and it is not the same as cc, then ask the user to
 # confirm using gcc instead of cc.
 echo "*** Checking your system: \$CC=\"$CC\" ..." | $tee -a $instalog;
 echo 'main() { printf("\nHello, world!\n"); return 0;}' >$tmppath/test.c
 mycc=$CC;
 if test "X$mycc" = "X"; then
   mycc=cc;
   echo "*** No explicit compiler specified in \$CC, checking \"cc\"" | $tee -a $instalog;
 fi;

 if $mycc -O -o $tmppath/testit $tmppath/test.c 2>/dev/null 1>&2 \
     && $tmppath/testit >$tmppath/testout \
     && grep -c '^Hello, world!$' $tmppath/testout >/dev/null;
 then
   rm $tmppath/testit $tmppath/testout;
   echo "*** $mycc works." | $tee -a $instalog;

   if test "X$CC" = "X" ; then

      (echo "*** \$CC is empty, so we check gcc as well."; echo) | $tee -a $instalog;
      if gcc -O2 -o $tmppath/testit $tmppath/test.c 2>/dev/null 1>&2 \
          && $tmppath/testit >$tmppath/testout \
          && grep -c '^Hello, world!$' $tmppath/testout >/dev/null; then

       echo "*** There seems to be at least two valid compilers installed on this system: gcc and $mycc" | $tee -a $instalog;
       while true; do
         rm $tmppath/testit $tmppath/testout;
         answerP "With which compiler do you want to compile the $archname compilation tools" gcc;
         echo;
       if $answer -O -o $tmppath/testit $tmppath/test.c 2>/dev/null \
             && $tmppath/testit >$tmppath/testout && grep -c '^Hello, world!$' $tmppath/testout >/dev/null;
          then
          mycc=$answer;
          if test "X$mycc" = "Xgcc"; then CC=`echo $mycc -O2`; else CC=`echo $mycc -O`; fi;
          break;
        else
          echo "*** $answer is not a working compiler";
        fi;
      done;
    fi;
   fi;
 else
   rm $tmppath/testit $tmppath/testout 2>/dev/null;
   echo "*** $mycc does not work.  Checking gcc -O2" | $tee -a $instalog;
   if gcc -O2 -o $tmppath/testit $tmppath/test.c 2>/dev/null \
        && $tmppath/testit >$tmppath/testout \
        && grep -c '^Hello, world!$' $tmppath/testout >/dev/null;
   then CC=`echo gcc -O2`;
   else
    echo "*** Cannot find a working compiler myself (will let the tools decide)" | $tee -a $instalog;
    echo "*** Note that this may be the cause if the build does not succeed." | $tee -a $instalog;
   fi;
 fi;

#
# Check for a strip utility, but accept its abscence if we don't find it right away.
#

 if test "X$CC" != "X" && strip $tmppath/testit && \
    $tmppath/testit >$tmppath/testout && grep -c '^Hello, world!$' $tmppath/testout >/dev/null;
 then
  strip=strip;
 else
  strip="echo >/dev/null";
 fi;

 if test "X$CC" != "X" ; then
  the_cc='"'CC=$CC'"';
  echo "*** Will use \$CC=\"$the_cc\" when compiling the tools." | $tee -a $instalog;
 else
   echo "*** Using unspecified \$CC (probably cc) for compiling the tools." | $tee -a $instalog;
   the_cc=;
 fi;

 # First, install our own tools
 (echo; echo "*** Installing simple tools from $srcpath/$archname-romtools-*") | $tee -a $instalog;

 if (echodo cd $csupobj2;
     echodo 'CC=${CC-cc}' 'strip=$strip' make -f $srcpath/$archname-romtools-*/Makefile \
              clean install binpath=$binpath srcdir=`echo $srcpath/$archname-romtools-*` 2>&1); then true;
 else
  echo; echo "*** $program: Could not install $archname romtools";
  bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** Installation of simple tools succeeded") | $tee -a $instalog;

 # Then configure the binary tools
 (echo;
  echo "*** $program: Starting configuration of binutils (please be patient):";
  echo) | $tee -a $instalog;

 if (echodo cd $binuobj; echodo $srcpath/binutils-*/configure --target=$archname 2>&1); then true; else
   echo; echo "*** $program: Could not configure binutils for $archname: configure failed";
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Configuration of binutils succeeded") | $tee -a $instalog;

 # Then make the binutils
 (echo;
  echo "*** $program: Making binutils (please be patient):";
  echo) | $tee -a $instalog;

 if (echodo cd $binuobj; echodo make all "$the_cc" 2>&1); then true; else
   echo; echo "*** $program: Make all failed for binutils.";
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Make of binutils succeeded.  Now moving the binutils binaries."; echo) | $tee -a $instalog;

 if { mkdir_path $libsubdir/include \
      && $strip $bob/ar && mv $bob/ar $libsubdir \
      && $strip $binuobj/gas/as.new && mv $binuobj/gas/as.new $libsubdir/as \
      && $strip $binuobj/ld/ld.new && mv $binuobj/ld/ld.new $libsubdir/ld \
      && $strip $bob/nm.new && mv $bob/nm.new $libsubdir/nm \
      && $strip $bob/ranlib && mv $bob/ranlib $libsubdir \
      && $strip $bob/objcopy && mv $bob/objcopy $libsubdir \
      && $strip $bob/objdump && mv $bob/objdump $libsubdir \
      && $strip $bob/strings && mv $bob/strings $libsubdir \
      && $strip $bob/strip.new && mv $bob/strip.new $libsubdir/strip; } 2>&1;
  then
    for tool in $compiler_binaries; do
      case $tool in
        gcc-$archname) ;;
        *) if ln -s $libsubdir/`basename $tool -$archname` $binpath/$tool; then true;
           else
             echo; echo "*** $program: Could not link binutils tools into $binpath";
             echo "An 'ls' of $binpath:" && ls -l $binpath;
             bug_report;
             exit 1;
           fi;;
      esac;
    done;

    ln -s $libsubdir/objdump $binpath/dis$archname;

 else
   echo; echo "*** $program: Could not move the binutils binaries into $libsubdir";
   echo "An 'ls' of $libsubdir:" && ls -l $libsubdir;
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Moving the binutils binaries succeeded.  Now adding standard headers."; echo) | $tee -a $instalog;
 # make headers for the C runtime support
 if (echodo cd $srcpath/${archname}-libc-*;
     echodo make CCVER=$gccver installdir=$libsubdir headers 2>&1); then true; else
    echo; echo "*** $program: Could not install header files:" make CCVER=$gccver installdir=$libsubdir headers failed.;
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Header files installed, now configuring the compiler."; echo) | $tee -a $instalog;

 # Then configure the compiler.  Specify everything possible (which is not much),
 # and prepare to add more specifications when running make.

 if (echodo cd $gccobj; echodo $srcpath/gcc-*/configure --target=$archname --exec-prefix=$binpath 2>&1); then true; else
   echo; echo "*** $program: Could not configure:" $srcpath/gcc-*/configure --target=$archname failed.;
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Configuration of compiler succeeded.";
  echo "*** Now making the compiler (please be patient):"; echo) | $tee -a $instalog;

 # Then make the compiler, all of it.
 if (echodo cd $gccobj;
     echodo make all "$the_cc" AR_FOR_TARGET=$binpath/ar-$archname \
              RANLIB_FOR_TARGET=$binpath/ranlib-$archname \
              RANLIB_TEST_FOR_TARGET=true libgccdir=$libgccdir \
              libsubdir=$libsubdir bindir=$binpath tooldir=$libsubdir \
              TARGET_TOOLPREFIX=$libsubdir/ 2>&1); then true; else
   echo; echo "*** $program: Could not make gcc for $archname";
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Made the compiler, now moving the binaries."; echo)  | $tee -a $instalog;

 # Ok, I should use the t-$archname fragment and make install, but this gives me a
 # better overview for the moment.
 if ($strip $gccobj/xgcc && mv $gccobj/xgcc $binpath/gcc-$archname \
     && $strip $gccobj/c++filt && mv $gccobj/c++filt $binpath/c++filt-$archname \
     && $strip $gccobj/cc1 && mv $gccobj/cc1 $libsubdir \
     && $strip $gccobj/cc1plus && mv $gccobj/cc1plus $libsubdir \
     && $strip $gccobj/cpp && mv $gccobj/cpp $libsubdir \
     && mv $gccobj/libgcc.a $libsubdir && mv $gccobj/specs $libsubdir) 2>&1;
  then true;
 else
   echo; echo "*** $program: Could not move the gcc binaries into $libsubdir";
   echo "An 'ls' of $libsubdir:" && ls -l $libsubdir;
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: Compiler binaries moved, now making and installing C runtime library"; echo)  | $tee -a $instalog;

# It might seem like an unnecessary step to copy the source code and make
# there, but it's not that much overhead, and it *really* helps to be able to
# run several test installations from the same source dir in parallel.
 if (echodo cd $srcpath/${archname}-libc-*;
     echo 'tar fc - . | (cd '$csupobj'; tar fx -)';
     tar fc - . | (cd $csupobj; tar fx -);
     echodo cd $csupobj;
     echodo make CCVER=$gccver installdir=$libsubdir \
             CC=$ngcc AR=$nar RANLIB=$nranlib clean install 2>&1); 
 then true; else
   echo; echo "*** $program: Could not install C runtime library: installation failed.";
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 (echo; echo "*** $program: C runtime library installed"; echo)  | $tee -a $instalog;

#
# Do a simple sanity check; compile hello, world and check it by grepping for
# that string in the final binary.
#
 (echo; echo "*** $program: Now checking the installation"; echo) | $tee -a $instalog;
 if (echodo cd $tmppath && echodo $binpath/gcc-$archname -sim $tmppath/test.c -o mytst 2>&1); then
   echo "*** It seems the compiler in $binpath/gcc-$archname works ...";
 else
   echo; echo "*** The compiler in $binpath/gcc-$archname does NOT work!";
   bug_report;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

#
# And we should be able to do a binary from that.  Take the opportunity to
# tell the installer about what works to get a binary image.
#
 if (cd $tmppath \
     && echodo $binpath/$archname-burn -w1 -b0 -o mytst2.bin mytst 2>&1 \
     && grep -c '^Hello, world!$' mytst2.bin >/dev/null) 2>&1; then
   echo "*** ... $archname-burn (cburn) works";
 else
   rm $binpath/$archname-burn;
   rm $binpath/cburn;
   echo "*** ... BUT $archname-burn (cburn) does NOT work!";
 fi | $tee -a $instalog;

 if (echodo cd $tmppath \
     && echodo $binpath/bin-$archname -w1 -b0 -o mytst.bin mytst 2>&1 \
     && grep -c '^Hello, world!$' mytst.bin >/dev/null) 2>&1; then
   echo "*** ... bin-$archname works";
 else
   rm $binpath/bin-$archname;
   echo "*** ... BUT bin-$archname does NOT work!";
 fi | $tee -a $instalog;

 if not test -x $binpath/$archname-burn || not test -x $binpath/$archname-burn; then 
   if not test -x $binpath/$archname-burn && not test -x $binpath/$archname-burn; then
     echo "*** No a.out-to-bin tool, unless you roll your own (and modify this script)";
     bug_report;
   else
     if not test -x $binpath/$archname-burn; then
       echo "*** You have to do without $archname-burn (cburn)";
     else
       echo "*** You have to do without bin-$archname";
     fi;
   fi;
 else
   echo "*** (Suggest you choose bin-$archname)"; echo;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 if test "X$manpath" != "X/dev/null"; then
  echo; echo "*** $program: Now installing manual pages"; echo;
  bsm=$srcpath/binutils-*; gcm=$srcpath/gcc-*;
  for f in $bsm/binutils/ar $bsm/binutils/nm $bsm/binutils/objcopy $bsm/binutils/objdump $bsm/binutils/ranlib $bsm/binutils/size $bsm/binutils/strings $bsm/binutils/strip $bsm/gas/doc/as $bsm/ld/ld $bsm/gprof/gprof $gcm/cccp $gcm/cpp $gcm/gcc $gcm/cp/g++; do
   if cp -p $f.1 $manpath && ln -s `basename $f`.1 $manpath/`basename $f`-$archname.1 2>&1; then true;
   else
     echo; echo "*** $program: Could not install manual pages: failed on $f.1";
     bug_report;
     exit 1;
   fi;
  done;
  echo; echo "*** $program: Manual pages installed"; echo;
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi;

 infodir=$tmppath/info-dir; rm $infodir 2>/dev/null;
 if test "X$infopath" != "X/dev/null"; then
  echo; echo "*** $program: Now installing info pages"; echo;
  bsi=$srcpath/binutils-*; gci=$srcpath/gcc-*;
  if not test -s $infopath/dir; then
    if
    (echo '-*- Text -*-' && \
     echo 'This is the file .../info/dir, which contains the topmost node of the' && \
     echo 'Info hierarchy.  The first time you invoke Info you start off' && \
     echo 'looking at that node, which is (dir)Top.' && \
     echo '' && \
     echo 'File: dir	Node: Top	This is the top of the INFO tree' && \
     echo '  This (the Directory node) gives a menu of major topics. ' && \
     echo '  Typing "d" returns here, "q" exits, "?" lists all INFO commands, "h" ' && \
     echo '  gives a primer for first-timers, "mTexinfo<Return>" visits Texinfo topic,' && \
     echo '  etc.' && \
     echo '  Or click mouse button 2 on a menu item or cross reference to select it.' && \
     echo '  --- PLEASE ADD DOCUMENTATION TO THIS TREE. (See INFO topic first.) ---' && \
     echo && \
     echo '* Menu: The list of major topics begins on the next line.') >>$infodir;
    then true;
    else
     echo; echo "*** $program: Could not install info pages, failed on \"dir\": $?";
     bug_report;
     exit 1;
    fi;
  fi;

  if 
  (echo && \
   echo && \
   echo "* gcc-$archname: (gcc).              The GNU C/C++ compiler for $archname." && \
   echo "                                This is generic information, although you" && \
   echo "                                will find $archname-specific information" && \
   echo '                                under *Note GCC target-specifics: (gcc)Submodel Options.' && \
   echo "* cpp: (cpp).                   The GNU C Preprocessor.") >>$infodir; then true;
  else
   echo; echo "*** $program: Could not make cpp, gcc entries: $?";
   bug_report;
   exit 1;
  fi;

  for f in $gci/cpp.info* $gci/gcc.info* $bsi/binutils/binutils.info $bsi/gas/doc/as.info* $bsi/ld/ld.info*; do
   if cp -p $f $infopath && awk <$f '/END-INFO-DIR-ENTRY/ { prt = 0;} { if (prt) { print; } } /START-INFO-DIR-ENTRY/ { prt=1;}' >>$infodir; then true;
   else
     echo; echo "*** $program: Could not install info pages, failed on $f: $?";
     bug_report;
     exit 1;
   fi;
  done;
  if test ro_infodir = YES; then
   if mv $infodir $infopath/dir2; then true;
   else
     echo; echo "*** $program: Could not install info 'dir2'-file";
     bug_report;
     exit 1;
   fi;
  else
   if cat $infodir >>$infopath/dir; then true; else
     echo; echo "*** $program: Could not add to info 'dir'-file";
     bug_report;
     exit 1;
   fi;
  fi;
  echo; echo "*** $program: Info pages installed";
 fi | $tee -a $instalog;

 if test -f $errfile; then exit 1; fi

fi;

#
# All must be ok since we got here.
#
(echo; echo "*** $program: The installation of tools for $archname went well. ***") | $tee -a $instalog;

if test $manpath != /dev/null -a $new_man = YES; then
  echo "*** Now do not forget to update your \$MANPATH environment variable with:";
  echo "    "`dirname $manpath`;
  echo "*** A suitable line to add in your .profile or shell resource file";
  echo "    (.bashrc .cshrc .zshrc .tcshrc etc) after other settings, may be:";
  echo "    MANPATH="`dirname $manpath`":\$MANPATH"; echo;
fi | $tee -a $instalog;

if test $infopath != /dev/null -a $ro_infodir = YES; then
  echo "*** Do not forget to append $infopath/dir2 to $infopath/dir ***";
  echo;
fi | $tee -a $instalog;

(echo "*** Do not forget to update your \$PATH environment variable with:";
 echo "    $binpath";
 echo "*** A suitable line to add in your .profile or shell resource file";
 echo "    (.bashrc .cshrc .zshrc .tcshrc etc) after other settings, may be:";
 echo "    PATH=$binpath:\$PATH"; echo;
 echo "*** The installation is finished, the date is:" `date` ) | $tee -a $instalog;

echo;
yes_or_noP "Do you want to remove the log and the temporary files" y
echo;
test $yes_or_no = NO && exit 0

# Remove any directory and such that was created but *NOT* on the path to the
# files that were installed.
cleanup $binpath/gcc-$archname $libpath/include/stdio.h $manpath/gcc-$archname.1 $infopath/dir;
exit 0;
