#!/bin/sh
##
## install.sh -- install script for other UNIX platforms
##
## $Id: install.sh,v 1.14 2002/03/12 22:36:18 wagnerch Exp $
##

##
## These are the defaults used in the install
##
INSTALL="/usr/bin/install"
GZIP="/bin/gzip"
ETCDIR="/etc"
BINDIR="/usr/sbin"
MANDIR="/usr/man/man1"
LIBDIR="/usr/lib/mnwclient"
OWNER="root"
GROUP="root"
PERL="/usr/bin/perl"

##
## Figure out what OS this is
##
OS=`uname -s | sed -e 's/\//-/g' -e 's/ //g'`

case $OS in
  FreeBSD|OpenBSD)
    ##
    ## *BSD variants
    ##
    INSTALL="/usr/bin/install -C"
    GZIP="gzip"
    MANDIR="/usr/local/man/man1"
    GROUP="wheel"
    RCDIR="/usr/local/etc/rc.d"

    ##
    ## Do the installation
    ##
    [ -d $RCDIR ] &&
        $INSTALL -m 0755 -o $OWNER -g $GROUP initscripts/freebsd/mnwclient $RCDIR/mnwclient.sh
    $GZIP mnwclient.1
    [ -e $ETCDIR/mnwclient.rc ] ||
        $INSTALL -m 0600 -o $OWNER -g $GROUP mnwclient.rc $ETCDIR/mnwclient.rc
    $INSTALL -m 0755 -o $OWNER -g $GROUP mnwclient $BINDIR/mnwclient
    $INSTALL -m 0644 -o $OWNER -g $GROUP mnwclient.1.gz $MANDIR/mnwclient.1.gz
    mkdir -p $LIBDIR
    $INSTALL -m 0644 -o $OWNER -g $GROUP Event.pm $LIBDIR/Event.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Log.pm $LIBDIR/Log.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Filter.pm $LIBDIR/Filter.pm

    ##
    ## Check if there is an SysV init directory
    ##
    if [ ! -d $RCDIR ]; then
       echo "Your system doesn't appear to have a SysV init directory.  You must start"
       echo "the daemon manually, or install the startup script manually:"
       echo ""
       echo "initscripts/freebsd/mnwclient"
       echo ""
    fi
    ;;

  NetBSD)
    ##
    ## NetBSD
    ##
    GZIP="gzip"
    MANDIR="/usr/share/man/man1"
    GROUP="wheel"
    RCDIR="/etc/rc.d"
    PERL="/usr/pkg/bin/perl"

    ##
    ## Do the installation
    ##
    [ -d $RCDIR ] &&
        $INSTALL -m 0755 -o $OWNER -g $GROUP initscripts/netbsd/mnwclient $RCDIR/mnwclient
    $GZIP mnwclient.1
    [ -e $ETCDIR/mnwclient.rc ] ||
        $INSTALL -m 0600 -o $OWNER -g $GROUP mnwclient.rc $ETCDIR/mnwclient.rc
    cat mnwclient |sed "1s~^#\!.*/perl\(.*\)$~#\!$PERL\1~" >mnwclient.tmp
    mv mnwclient.tmp mnwclient
    $INSTALL -m 0755 -o $OWNER -g $GROUP mnwclient $BINDIR/mnwclient
    $INSTALL -m 0644 -o $OWNER -g $GROUP mnwclient.1.gz $MANDIR/mnwclient.1.gz
    mkdir -p $LIBDIR
    $INSTALL -m 0644 -o $OWNER -g $GROUP Event.pm $LIBDIR/Event.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Log.pm $LIBDIR/Log.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Filter.pm $LIBDIR/Filter.pm

    ##
    ## Check if there is an SysV init directory
    ##
    if [ ! -d $RCDIR ]; then
       echo "Your system doesn't appear to have a SysV init directory.  You must start"
       echo "the daemon manually, or install the startup script manually:"
       echo ""
       echo "initscripts/netbsd/mnwclient"
       echo ""
    fi
    ;;

  Linux)
    ##
    ## Linux
    ##
    RCDIR="/etc/rc.d/init.d"

    ##
    ## Do the installation
    ##
    [ -d $RCDIR ] &&
        $INSTALL -m 0755 -o $OWNER -g $GROUP mnwclient.init $RCDIR/mnwclient
    $GZIP mnwclient.1
    [ -e $ETCDIR/mnwclient.rc ] ||
        $INSTALL -m 0600 -o $OWNER -g $GROUP mnwclient.rc $ETCDIR/mnwclient.rc
    $INSTALL -m 0755 -o $OWNER -g $GROUP mnwclient $BINDIR/mnwclient
    $INSTALL -m 0644 -o $OWNER -g $GROUP mnwclient.1.gz $MANDIR/mnwclient.1.gz
    mkdir -p $LIBDIR
    $INSTALL -m 0644 -o $OWNER -g $GROUP Event.pm $LIBDIR/Event.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Log.pm $LIBDIR/Log.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Filter.pm $LIBDIR/Filter.pm

    ##
    ## Check if there is an SysV init directory
    ##
    if [ ! -d $RCDIR ]; then
       echo "Your system doesn't appear to have a SysV init directory.  You must start"
       echo "the daemon manually, or install the startup script manually:"
       echo ""
       echo "mnwclient.init"
       echo ""
    fi
    ;;

  SunOS)
    ##
    ## SunOS, Solaris
    ##
    INSTALL="/usr/sbin/install"
    MANDIR="/usr/local/man/man1"
    BINDIR="/usr/local/sbin"
    GROUP="bin"
    RCDIR="/etc/init.d"

    ##
    ## Do the installation
    ##
    if test -d $RCDIR; then
       $INSTALL -m 0755 -u $OWNER -g $GROUP -c $RCDIR initscripts/solaris/mnwclient
       ln $RCDIR/mnwclient /etc/rc3.d/S88mnwclient
    fi
    if test ! -e $ETCDIR/mnwclient.rc; then
      $INSTALL -m 0600 -u $OWNER -g $GROUP -c $ETCDIR mnwclient.rc
    fi
    $INSTALL -m 0755 -u $OWNER -g $GROUP -c $BINDIR mnwclient
    $INSTALL -m 0644 -u $OWNER -g $GROUP -c $MANDIR mnwclient.1
    mkdir -p $LIBDIR
    $INSTALL -m 0644 -u $OWNER -g $GROUP -c $LIBDIR Event.pm
    $INSTALL -m 0644 -u $OWNER -g $GROUP -c $LIBDIR Log.pm
    $INSTALL -m 0644 -u $OWNER -g $GROUP -c $LIBDIR Filter.pm

    ##
    ## Check if there is an SysV init directory
    ##
    if test ! -d $RCDIR; then
       echo "Your system doesn't appear to have a SysV init directory.  You must start"
       echo "the daemon manually, or install the startup script manually:"
       echo ""
       echo "initscripts/solaris/mnwclient"
       echo ""
    fi
    ;;

  *)
    ##
    ## Other UNIX variants, we will try ask some questions and hope
    ## for the best
    ##
    echo "I am not familiar with $OS, so I will need you to answer a few questions."
    echo ""
    ask_str "Where is install located and any parameters required?" INSTALL
    ask_str "Where is gzip located?" GZIP
    ask_str "Where to store system-wide resource files?" ETCDIR
    ask_str "Where to store system binaries?" BINDIR
    ask_str "Where to store manual pages?" MANDIR
    ask_str "Where to store library files?" LIBDIR
    ask_str "Owner of installation files?" OWNER
    ask_str "Group of installation files?" GROUP
    ask_str "Where is the perl interpreter located?" PERL
    echo ""

    ##
    ## Do the installation
    ##
    $GZIP mnwclient.1
    [ -e $ETCDIR/mnwclient.rc ] ||
        $INSTALL -m 0600 -o $OWNER -g $GROUP mnwclient.rc $ETCDIR/mnwclient.rc
    fix_perl mnwclient
    $INSTALL -m 0755 -o $OWNER -g $GROUP mnwclient $BINDIR/mnwclient
    $INSTALL -m 0644 -o $OWNER -g $GROUP mnwclient.1.gz $MANDIR/mnwclient.1.gz
    mkdir -p $LIBDIR
    $INSTALL -m 0644 -o $OWNER -g $GROUP Event.pm $LIBDIR/Event.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Log.pm $LIBDIR/Log.pm
    $INSTALL -m 0644 -o $OWNER -g $GROUP Filter.pm $LIBDIR/Filter.pm

    ##
    ## Let them know they have to start it manually
    ##
    echo "You will have to manually start the daemon, or install a cron job to"
    echo "ensure the process is running.  You can review the contrib directory"
    echo "to see init scripts run from the rc.local script at system startup"
    echo ""
    ;;
esac

##
## Prompt functions
##
prompt () {
    eval $3=\"$2\"
    /bin/echo -e "$1 [$2]: \c"
    read tmp
    if [ ! -t 1 ] ; then echo $tmp ; fi
    if [ -n "$tmp" ] ; then eval $3=\"$tmp\" ; fi
}

ask_bool () {
    default=`eval echo '$'$2`
    if [ ! "$default" ] ; then default=n ; fi
    answer=""
    while [ "$answer" != "n" -a "$answer" != "y" ] ; do
        prompt "$1 (y/n)" "$default" answer
    done
    eval "$2=$answer"
}

ask_str () {
    default=`eval echo '$'$2`
    prompt "$1" "`echo $default`" answer
    eval $2=\"$answer\"
}

##
## Fix perl bangpath at the start of mnwclient
##
fix_perl () {
   cat $1 |sed "1s~^#\!.*/perl\(.*\)$~#\!$PERL\1~" >$1.tmp
   mv $1.tmp $1
}
