#!/bin/sh
# libinstall - installation script for the MiNTLib (binary distributions).
# Copyright (C) 1999 Guido Flohr <gufl0000@stud.uni-sb.de>.
#
# This file is part of the MiNTLib project, and may only be used
# modified and distributed under the terms of the MiNTLib project
# license, COPYMINT.  By continuing to use, modify, or distribute
# this file you indicate that you have read the license and
# understand and accept it fully.

here=`pwd`
echo_n="/bin/echo -n"
required_programs="cp cat sed awk tr grep sort uniq chmod rm"
install=
gcc=
mkinstalldirs=./mkinstalldirs
PRIMARY_YDATA="africa antarctica asia australasia \
		europe northamerica southamerica"
YDATA="$PRIMARY_YDATA pacificnew etcetera factory backward"
NDATA="systemv"
SDATA="solar87 solar88 solar89"
TDATA="$YDATA $NDATA $SDATA"

check_environment()
{
  # Check if run from current directory.
  if test ! -x ./$0; then
    echo "Error: $0 must be run from current directory."
    exit 1
  fi  

  echo "Welcome to the MiNTLib installation script.  First we have to check"
  echo "if your system is ready to install the MiNTLib."
  sleep 3 2>/dev/null
  
  missing=""
  for p in $required_programs; do
    $echo_n "Checking for $p..."
    found=no
    IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
      if test -x $dir/$p; then
        found=yes
        echo " $dir/$p"
        break
      fi
    done
    IFS="$save_ifs"
    
    if test $found = no; then
      missing="$missing $p"
      echo "missing"
    fi
  done
  
  if test -n "$missing"; then
    echo "The following required programs are missing on your system:"
    for p in $missing; do
      echo "  $p"
    done
    
    cat <<EOF  
Either you have not set up your PATH envariable correctly or you don't
have the programs.  All of these standard programs should be widely
available for anonymous download from various ftp servers.  If you
can't find them please contact the MiNTLib maintainer (see README)
or the MiNT mailing list (currently mint@fishpool.com).

Good bye for now!
EOF
    exit 1
  fi
}

add_missing ()
{
  $echo_n "Checking for install..."
  try_install="$INSTALL install ginstall $here/install-sh"
  for install in $try_install; do
    IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
      if test -x $dir/$try; then
        if test $install != $here/install-sh; then
          install="$install -c"
        fi
        echo " $dir/$install"
        break
      fi
    done
    IFS="$save_ifs"
    break
  done

  $echo_n "Checking for gcc..."
  try_gccs="$CC gcc cc"
  gcc=""
  for try_gcc in $try_gccs; do
    IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
    for dir in $PATH; do
      if test -x $dir/$try_gcc; then
        echo " $dir/$try_gcc"
        gcc=$try_gcc
        break
      fi
    done
    IFS="$save_ifs"
    if test x"$gcc" != x; then
      break
    fi
  done
  
  if test x"$gcc" = x; then
    echo " missing"
  fi
  
  $echo_n "Checking for korn shell..."
  found=no
  try_kshs="$KSH ksh bash ksh"  # ksh is listed twice on purpose!
  for KSH in $try_kshs; do
    IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
    for dir in "/bin":$PATH; do
      if test -x $dir/$KSH; then
        echo " $dir/$KSH"
        found=yes
        break
      fi
    done
    IFS="$save_ifs"
    if test x$found != xno; then
      break
    fi
  done

  if test x$found != xyes; then
    echo " missing"
  fi

  if test "$LOCALTIME" = "Factory" -o "$POSIXRULES" = "Factory"; then
    cat <<EOF
WARNING:  It seems like you haven't edited the file \`configvars'
because either the variable \`LOCALTIME' or \`POSIXRULES' is still
set to \`Factory'.  You should exit the installation now and 
restart after you have edited \`configvars'.
You will find a list of possible time zones in the file \`tz/zonelist'.
EOF
    $echo_n "Exit the installation for now [y/N]? "
    read reply
    case $reply in
      [yY]*) exit 1 ;;
      *) ;;
    esac
    
    cat <<EOF
OK, you can later correct the settings for your local time zone by
	zic -l TIMEZONE
and change the default rules with
	zic -p TIMEZONE
Replace TIMEZONE with one of the values you find in the file
$here/zonenames.
EOF
    sleep 3 2>/dev/null
  fi
  
  if test x$UID != x0; then
    cat <<EOF
WARNING: You are not the superuser.  It is very unlikely that you
will manage to install the MiNTLib without superuser permissions.
You should become superuser (for example with the su command) and
then continue the installation).
EOF
  
    $echo_n "Exit the installation for now [Y/n]? "
    read reply
    case $reply in
      [nN]*) ;;
      *) exit 1 ;;
    esac
  fi
}

install_headers ()
{
  cat <<EOF
Everything seems to be set up alright.
Installing system include files.
EOF

  if test -n "$gcc"; then
    $echo_n "First we will clean up your old system include files..."
    MINTLIB_VERSION=$VERSION CC=$gcc include/clean-include
    echo " done"
  else
    cat include/00README
    echo
    echo "You can reread this message in the file include/00README."
    sleep 3 2>/dev/null
  fi
  
  # Evaluate include/MISCFILES.
  headers=`cat include/MISCFILES | \
      sed -e 's,#.*,,g' -e 's,MISCFILES  *=  *,,g' | \
      tr '[ \t]' '\n' | \
      grep '\.h$'`
  headers="00README COPYMINT COPYING COPYING.LIB $headers stab.def"
  $mkinstalldirs $includedir
  for header in $headers; do
    echo "  $install -m 644 include/$header $includedir"
    $install -m 644 include/$header $includedir || exit 1
  done
  # Evaluate include/mint/MISCFILES.
  headers=`cat include/mint/MISCFILES | \
      sed -e 's,#.*,,g' -e 's,MISCFILES  *=  *,,g' | \
      tr '[ \t]' '\n' | \
      grep '\.h$'`
  $mkinstalldirs $includedir/mint
  for header in $headers; do
    echo "  $install -m 644 include/mint/$header $includedir/mint"
    $install -m 644 include/mint/$header $includedir/mint || exit 1
  done
  # Evaluate include/sys/MISCFILES.
  headers=`cat include/sys/MISCFILES | \
      sed -e 's,#.*,,g' -e 's,MISCFILES  *=  *,,g' | \
      tr '[ \t]' '\n' | \
      grep '\.h$'`
  $mkinstalldirs $includedir/sys
  for header in $headers; do
    echo "  $install -m 644 include/sys/$header $includedir/sys"
    $install -m 644 include/sys/$header $includedir/sys || exit 1
  done
}

install_startup ()
{
  echo "Installing startup files."
  files="crt0.o gcrt0.o bcrt0.o bgcrt0.o"
  $mkinstalldirs $libdir
  startupdir=startup
  test -d $startupdir || startupdir=lib  # Binary distribution!
  for file in $files; do
    echo "  $install -m 644 $startupdir/$file $libdir"
    $install -m 644 $startupdir/$file $libdir || exit 1
  done
}

install_libs ()
{
  echo "Installing libraries."
  $mkinstalldirs $libdir
  for lib in "c" "iio"; do
    for baserel in "%" "%b"; do
      for size in "%" "%16"; do
        baserel=`echo $baserel | tr -d '%'`
        size=`echo $size | tr -d '%'`
        if test ! -d startup; then # Must be a binary distribution.
          libheredir=lib
          libherename=lib$baserel$lib$size.a
        else
          libheredir=lib$baserel$size
          libherename=$lib_name_prefix$baserel$lib$size$lib_name_extender
        fi
        libname=$lib_name_prefix$baserel$lib$size$lib_name_extender
        echo "  $install -m 644 $libheredir/$libherename $libdir/$libname"
        $install -m 644 $libheredir/$libherename $libdir/$libname || exit 1
      done
    done
  done
}

install_tz ()
{
  echo "Installing time zone database."
  $echo_n "  Creating tz/yearistype..."
  rm -f tz/yearistype
  cp tz/yearistype.sh tz/yearistype && chmod +x tz/yearistype || exit 1
  echo " done"
  $echo_n "  Creating tz/tzselect..."
  rm -f tz/tzselect
  sed \
    -e 's|AWK=[^}]*|AWK=awl|g' \
    -e 's|TZDIR=[^}]*|TZDIR=$TZDIR|' \
    <tz/tzselect.ksh >tz/tzselect
    chmod +x tz/tzselect || exit 1
  echo " done"
  echo "  Deleting stale zoneinfo files..."
  echo "    rm -f $TZDIR/localtime"
  rm -f $TZDIR/localtime
  echo "    rm -f $TZDIR/posixrules"
  rm -f $TZDIR/posixrules
  echo "  Installing manual pages."
  MANS="tz/tzfile.5 tz/tzselect.8 tz/zic.8 tz/zdump.8 tz/tzinit.8"
  sections=""
  for manpage in $MANS; do
    sections="$sections $TZMANDIR/man`echo $manpage | sed 's,^.*\\.,,'`"
  done
  mandirs=`echo $sections | sort | uniq`
  for mandir in $mandirs; do
    $mkinstalldirs $mandir
  done
  for manpage in $MANS; do
    section=`echo $manpage | sed 's,^.*\\.,,'`
    echo "    $install -m 644 $manpage $TZMANDIR/man$section"
    $install -m 644 $manpage $TZMANDIR/man$section || exit 1
  done

  cat <<EOF
Now compiling the timezone database.  This may require some minutes.  In
the meantime you can say hello to your family if they still recognize you.
EOF
  
  # Poor man's make rules.
  case $REDO in
    posix_right)  
      REDO="posix_only posix_spare right_spare"
      ;;
    right_posix)
      REDO="right_only posix_spare right_spare"
      ;;
    posix_only|right_only)
      ;;
    *)
      cat <<EOF
error: You must choose one of the values
	right_only
	posix_only
	right_posix
	posix_right
for the variable REDO in \`configvars'.  Please edit \`configvars' 
and re-install the database.
EOF
    exit 1
    ;;
  esac

  tdata=""
  for srcfile in $TDATA; do
    tdata="$tdata tz/$srcfile"
  done

  $mkinstalldirs $TZDIR  
  for target in $REDO; do
    case $target in
      posix_only|posix_spare)
        tzdir=$TZDIR
        test $target = posix_spare && tzdir=$tzdir/posix
        cat <<EOF
  Installing posixly correct (not accounting leapseconds) database in 
  $tzdir.
    tz/zic -y tz/yearistype -d $tzdir -L /dev/null $tdata
EOF
        tz/zic -y tz/yearistype -d $tzdir -L /dev/null $tdata || exit 1
        ;;
      right_only|right_spare)
        tzdir=$TZDIR
        test $target = right_spare && tzdir=$tzdir/right
        cat <<EOF
  Installing time zone database (with leapsecond calculation) in 
  $tzdir.
    tz/zic -y tz/yearistype -d $tzdir -L tz/leapseconds $tdata
EOF
        tz/zic -y tz/yearistype -d $tzdir -L /dev/null $tdata || exit 1
        ;;
    esac
  done
  
  echo "  Installing system-wide default time zone and tabular data."
  echo "    $install -m 644 tz/iso3166.tab $TZDIR"
  $install -m 644 tz/iso3166.tab $TZDIR || exit 1
  echo "    $install -m 644 tz/zone.tab $TZDIR"
  $install -m 644 tz/zone.tab $TZDIR || exit 1
  echo "    tz/zic -y tz/yearistype -d $TZDIR -l $LOCALTIME -p $POSIXRULES"
  tz/zic -y tz/yearistype -d $TZDIR -l $LOCALTIME -p $POSIXRULES || exit 1
  
  echo "  Installing time zone tools in $TZETCDIR."
  progs="zic zdump tzselect tzinit"
  $mkinstalldirs $TZETCDIR
  for prog in $progs; do
    echo "    $install -m 755 tz/$prog $TZETCDIR"
    $install -m 755 tz/$prog $TZETCDIR || exit 1
  done
}

# Include Makefile fragment and ignore errors.
. configvars 2>/dev/null

check_environment
add_missing
install_headers
install_startup
install_libs
if test x$CROSS = x
  install_tz
else
  echo "Not installing time zone database for cross compilation."
fi

cat <<EOF

Installation completed.  Good luck and have fun when writing free
software that you contribute to the FreeMiNT community or the
MiNTLib project.
EOF
