#!/bin/sh
#-------------------------------------------------------------------------
# Install [install_prefix_directory] [more-configure-options]
#
# A shell script which builds and installs the EuroCons Programms
# programm
#
# $Id: Install 194 2002-06-11 13:12:28Z kpoitschke $
#
# $Revision: 194 $
#-------------------------------------------------------------------------

#-------------------------------------------------------------------------
# returns a lower case string that identifies the operating system
compute_system () {
    set `uname -a`
        case $1 in
            Linux)
                echo linux
            ;;
            SunOS)
                case $3 in
                    5.*)
                        echo solaris
                    ;;
                    *)
                        echo sunos
                    ;;
                esac
            ;;
            HP-UX)
                echo hpux
            ;;
            OSF1)
                echo dec 
            ;;
            SCO_SV)
                echo sco
            ;;  
            UNIX_SV)
                if [ -f /etc/issue ]; then
                    grep "UnixWare" /etc/issue >/dev/null 2>/dev/null
                        if [ $? != 1 ]; then
                                echo uw2 
                        else
                                echo svr4 
                        fi
                else
                        echo svr4
                fi
            ;;
            AIX)
                case $4 in
                    3)
                        echo aix3
                    ;;
                    4)
                        echo aix4
                    ;;
                esac
            ;;
            *)
                if [ -c /dev/tty1A ]; then
                    echo sco
                elif [ -c /dev/ttyd0 -a "$1" != "Linux" ]; then
                    echo ia
                elif [ -c /dev/ttypa -a "$1" != "Linux" ]; then
                    echo ia
                else 
                    echo "Unknown System"
                fi
                ;;
    esac
}
#-------------------------------------------------------------------------
# returns a string that identifies the hardware model
compute_hardware () {
  echo `uname -m | sed 's./.-.'`
}

#-------------------------------------------------------------------------
# returns the used compiler. HP-UX we use c89, else we use gcc
compute_compiler () {
    set `uname -a`
        case $1 in
            HP-UX)
                echo c89
            ;;
            *)
        echo gcc
                ;;
    esac
}

#-------------------------------------------------------------------------
# Check if we can use bsd style echo
compute_echo () {
    if [ `echo "\c" | wc -l` = 1 ]; then
        echo true
    else
        echo ""
    fi
}

#-------------------------------------------------------------------------
# Prompts something and waits for the user to hit the key.
read_one () {
    prompt=$1
    default=$2

    echo_n "$prompt"
    stty -icanon min 1
    answer=`/bin/dd count=1 2>/dev/null`
    stty icanon
    echo
}

#-------------------------------------------------------------------------
# Reads a line with a given default value
read_def () {
    prompt=$1
    default=$2

    echo_n "$prompt"
    read answer
    if [ -z "$answer" ]; then
        answer=$default
    fi
}

#-------------------------------------------------------------------------
# prompts a string without a newline
echo_n () {
    if [ $bsdecho ]; then
          echo -n "$*"
    else
          echo "$*\c"
    fi
}

#-------------------------------------------------------------------------
# create_build_dir dir
create_build_dir () {
    test -d $1 && cleanup $1
    mkdir $1
}

#-------------------------------------------------------------------------
# cleanup build_dir
cleanup () {
  test -z $1 && return
  echo "Cleanup $1"
  /bin/rm -rf $1
}

#-------------------------------------------------------------------------
# main [install_prefix] [more-configure-args]
# 
main () {
  # Set some global variables
  conf_opts=""
  build_dir=""
  enable_debug=false
  install_prefix="/usr/local"
  test -z "$AXN_HOME" || install_prefix="$AXN_HOME"
  install_after_build=true
  precomp_checks=sema

  system=`compute_system`
  compiler=`compute_compiler`
  hardware=`compute_hardware` 
  bsdecho=`compute_echo`
  build_dir=`pwd`"/build-$hardware-$system"
  # Check if we got some parameters
  test -z "$1" || install_prefix=$1
  test -z "$2" || conf_opts=$2


  # Ask the user for the build options
  read_def "Install in [$install_prefix] " $install_prefix
  install_prefix=$answer
  conf_opts="$conf_opts --prefix=$install_prefix"

  read_def "Enter your OS type [$system] " "$system"
  system=$answer

  read_def "Enter your compiler [$compiler] " "$compiler"
  compiler=$answer

 
  read_def "Compile with debugging enabled (y/n) [n] " "n"
  case $answer in
      y | Y )
        conf_opts="$conf_opts --enable-debug"
        ;;
      *)
        ;;
  esac

  read_def "Install after build (y/n) [y] " "y"
  case $answer in
      n | N )
        install_after_build=false
        ;;
      *)
        ;;
  esac

  echo
  echo "The programm will be configured using the following parameters:"
  echo
  echo "Install in      : $install_prefix"
  echo "System          : $system"
  echo "Compiler        : $compiler"
  echo "Install         : $install_after_build"
  echo "Configure Opts  : $conf_opts"
  echo "Build directory : $build_dir"

  echo
  read_def "Start build now (y/n) [y] " "y"
  case $answer in
      y|Y )
          ;;
      *)
        return
        ;;
  esac

  CC=$compiler
  export CC

  case $system in
    *hpux* )
      CFLAGS="-D_HPUX_SOURCE +e +O2"
      export CFLAGS
      ;;
    *)
      ;;
  esac

  create_build_dir $build_dir && \
  (cd $build_dir ; ../configure $conf_opts && make)
  stat=$?
  if [ $stat -eq 0 ] ; then
    echo 
    echo "Build process finished."
    echo
  
    if test $install_after_build = true ; then
        read_def "Install now (y/n) [y] " "y" 
        case $answer in
            y|Y ) 
                (cd $build_dir; make install)
                ;;
             *)
                ;;
        esac
    fi
    read_def "Should I cleanup the build directory (y/n) [y] " "y"
    case $answer in
        y | Y )
            cleanup $build_dir
            ;;
        *)
            ;;
    esac
  else
    echo
    echo "Build process failed"
    echo
  fi
}
#-------------------------------------------------------------------------

main $@

exit $?
# finish
