#! /bin/sh

machs=""; last=""; sep0=""; sep1=""; sep2=""; sep3=""; sep4=" and ";
for fn in Mf-* ; do
  case $fn in
    Mf-install*|Mf-install.in|Mf-none)
       ;;
    *) machs=$machs$sep0$last
       last=`echo $fn | sed -e 's/Mf-//'`
       sep0=$sep1; sep1=", "; sep2=$sep3; sep3=$sep4; sep4=", and "
       ;;
  esac
done
machs=$machs$sep2$last

m=""
threads=no
bit64=no
temproot=""
relink=yes
heaps=no
help=no
installprefix=""
installman=""

case `uname` in
  Linux)
    if uname -a | egrep 'i386|i686|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3le
      m64=""
      tm32=ti3le
      tm64=""
    fi
    ;;
  FreeBSD)
    if uname -a | egrep 'i386|i686|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3fb
      m64=""
      tm32=ti3fb
      tm64=""
    fi
    ;;
  OpenBSD)
    if uname -a | egrep 'i386|i686|athlon|x86_64' > /dev/null 2>&1 ; then
      m32=i3ob
      m64=""
      tm32=ti3ob
      tm64=""
    fi
    ;;
  Darwin)
    if uname -a | grep -i power > /dev/null 2>&1 ; then
      m32=ppcosx
      m64=""
      tm32=tppcosx
      tm64=""
    elif uname -a | grep i386 > /dev/null 2>&1 ; then
      m32=i3osx
      m64=""
      tm32=ti3osx
      tm64=""
    fi
    ;;
  SunOS)
    if uname -a | grep sparc > /dev/null 2>&1 ; then
      m32=sps2
      m64=sp64
      tm32=tsps2
      tm64=tsp64
    fi
    ;;
esac

if [ -f Mf-$m32 ] ; then
  m=$m32
elif [ -f Mf-$m64 ] ; then
  m=$m64
  bit64=yes
elif [ -f Mf-$tm32 ] ; then
  m=$tm32
  threads=yes
elif [ -f Mf-$tm64 ] ; then
  m=$tm64
  threads=yes
  bit64=yes
fi

explicitm=no

while [ $# != 0 ] ; do
  case $1 in
    -m=*)
      m=`echo $1 | sed -e 's/^-m=//'`
      explicitm=yes
      ;;
    --machine=*)
      m=`echo $1 | sed -e 's/^--machine=//'`
      explicitm=yes
      ;;
    --threads)
      if [ $explicitm = yes ] ; then
        echo "warning: $1 ignored after -m or --machine"
      else
        if [ $bit64 = yes ] ; then m=$tm64 ; else m=$tm32 ; fi
        threads=yes
      fi
      ;;
    --nothreads)
      if [ $explicitm = yes ] ; then
        echo "warning: $1 ignored after -m or --machine"
      else
        if [ $bit64 = yes ] ; then m=$m64 ; else m=$m32 ; fi
        threads=no
      fi
      ;;
    --64)
      if [ $explicitm = yes ] ; then
        echo "warning: $1 ignored after -m or --machine"
      else
        if [ $threads = yes ] ; then m=$tm64 ; else m=$m64 ; fi
        bit64=yes
      fi
      ;;
    --no64)
      if [ $explicitm = yes ] ; then
        echo "warning: $1 ignored after -m or --machine"
      else
        if [ $threads = yes ] ; then m=$tm32 ; else m=$m32 ; fi
        bit64=no
      fi
      ;;
    --installprefix=*)
      installprefix=`echo $1 | sed -e 's/^--installprefix=//'`
      ;;
    --installman=*)
      installman=`echo $1 | sed -e 's/^--installman=//'`
      ;;
    --temproot=*)
      temproot=`echo $1 | sed -e 's/^--temproot=//'`
      ;;
    --force-relink)
      relink=yes
      ;;
    --noforce-relink)
      relink=no
      ;;
    --build-heaps)
      heaps=yes
      ;;
    --nobuild-heaps)
      heaps=no
      ;;
    --help)
      help=yes
      ;;
    *)
      echo "option '$1' unrecognized or missing an argument; try $0 --help"
      exit 1
      ;;
  esac
  shift
done

if [ "$installprefix" = "" ] ; then
  case $m in
    i3le|ti3le)
      installprefix=/usr
      ;;
    i3fb|ti3fb|i3ob|ti3ob)
      installprefix=/usr/local
      ;;
    ppcosx|tppcosx|i3osx|ti3osx)
      installprefix=/usr
      ;;
    sps2|tsps2|sp64|tsp64)
      installprefix=/usr/local
      ;;
    *)
      installprefix=/usr
      ;;
  esac
fi

if [ "$installman" = "" ] ; then
  case $m in
    i3le|ti3le)
      installman=$installprefix/man
      ;;
    i3fb|ti3fb|i3ob|ti3ob|ppcosx|tppcosx|i3osx|ti3osx|sps2|tsps2|sp64|tsp64)
      if [ "$installprefix" = "/usr" ] ; then
        installman=/usr/share/man
      else
        installman=$installprefix/man
      fi
      ;;
    *)
      installman=$installprefix/man
      ;;
  esac
fi

if [ "$help" = "yes" ]; then
  echo "Purpose:"
  echo "  $0 determines the machine type and constructs a custom Makefile"
  echo "  and Mf-install, taking into account the options below."
  echo ""
  echo "Options (defaults shown in parens):"
  echo "  --machine=<machine type>    explicitly specify machine type ($m)"
  echo "  -m=<machine type>           same as --machine <machine type> ($m)"
  echo "  --[no]threads               specify threaded version, if supported ($threads)"
  echo "  --[no]64                    specify 64-bit version, if supported ($bit64)"
  echo "  --[no]force-relink          recreate executable on this machine ($relink)"
  echo "  --[no]build-heaps           create saved heap images ($heaps)"
  echo "  --installprefix=<pathname>  final installation root ($installprefix)"
  echo "  --installman=<pathname>     manpage directory ($installman)"
  echo "  --temproot=<pathname>       staging root ($temproot)"
  echo ""
  echo "Available machine types: $machs"
  echo ""
  echo "Examples:"
  echo "  $0 --machine=i3le"
  echo ""
  echo "  set machine-type to i3le rather than to determined type"
  echo ""
  echo "  $0 --threads --installprefix=/usr/local"
  echo ""
  echo "  specify threaded version and set installation directory to /usr/local."
  echo ""
  echo "  $0 --installprefix=/usr/local --temproot=/tmp"
  echo ""
  echo "  declare the final destination to be /usr/local but staging area"
  echo "  to be /tmp/usr/local.  Make will record the final destination in"
  echo "  the installed manual page but actually install the system in the"
  echo "  staging area."
  echo ""
  exit 0
fi

if [ "$m" = "" -o ! -e Mf-$m ] ; then
  echo "no suitable machine type found"
  echo "try rerunning as $0 -m=<machine type>"
  echo "available machine types: $machs"
  exit 1
fi

if [ -f Makefile ] ; then /bin/mv -f Makefile Makefile.bak ; fi
if [ -f Mf-install ] ; then /bin/mv -f Mf-install Mf-install.bak ; fi

sed -e "s/^m=none\$/m=$m/"\
    -e "s/ForceRelink=no/ForceRelink=$relink/"\
    -e "s/BuildHeaps=no/BuildHeaps=$heaps/"\
    Makefile.in > Makefile

sed -e "s;^InstallPrefix=.*\$;InstallPrefix=$installprefix;"\
    -e "s;^InstallMan=.*\$;InstallMan=$installman/man1;"\
    -e "s;^TempRoot=$;TempRoot=$temproot;"\
    Mf-install.in > Mf-install
