#!/bin/sh
# install_pkg for linux

# Copyright (c) 2001-2004 Digi International Inc., All Rights Reserved
# 
# This software contains proprietary and confidential information of Digi
# International Inc.  By accepting transfer of this copy, Recipient agrees
# to retain this software in confidence, to prevent disclosure to others,
# and to make no use of this software other than that for which it was
# delivered.  This is an unpublished copyrighted work of Digi International
# Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
# prohibited.
# 
# Restricted Rights Legend
#
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
# Technical Data and Computer Software clause at DFARS 252.227-7031 or
# subparagraphs (c)(1) and (2) of the Commercial Computer Software -
# Restricted Rights at 48 CFR 52.227-19, as applicable.
#
# Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
#
# ***************************************************************************

if [ "$UID" -ne "0" ]
then
    echo "You must be root to install."
    echo "Press <ENTER> to continue..."
    read dummy              
    exit 1    
fi
clear

PKG=$1
PROD=$2
SOFT=$3
GUI=$4
B_TAR="false"          # Boolean -- Is compressed tar also available.
DIST=      
if [ -f /etc/redhat-release ]; then
    DIST=redhat
elif [ -f /etc/SuSE-release ]; then
    DIST=suse
elif [ -f /etc/mandrake-release ]; then
    DIST=mandrake
else
    DIST=unsupported
fi

RPMBUILD="rpm"
RPMINSTALL="rpm -ihvv"
which rpmbuild &> /dev/null
if [ $? -eq 0 ] 
then
    RPMBUILD="rpmbuild"
fi

               
LOGFILEB=/tmp/digicd_build.log        # Rewritten every time
LOGFILEI=/tmp/digicd_install.log      # Each install appends to this file
LOGFILETEMP=/tmp/temp
cat /dev/null > $LOGFILEB             # Rewritten every time
cat /dev/null > $LOGFILEI             # Rewritten every time
cat /dev/null > $LOGFILETEMP          # Rewritten every time


logheader ()
{
    echo >> $LOGFILEI
    echo >> $LOGFILEI
    echo "************************************************" >> $LOGFILEI
    echo "**  Date: $(date +%2m/%2d/%Y)" >> $LOGFILEI
    echo "**  Building/Installing $PKG" >> $LOGFILEI
    echo "************************************************" >> $LOGFILEI
}

prompt ()
{
echo " 1 - Red Hat 7.2"
echo " 2 - Red Hat 7.3"
echo " 3 - Red Hat 8"
echo " 4 - Red Hat 9"
echo " 5 - Fedora Core 1 & 2" 
echo " 6 - Red Hat AS 2.1"
echo " 7 - Red Hat Enterprise Linux AS 3"
echo " 8 - Red Hat Enterprise Linux ES 3"
echo " 9 - Red Hat Enterprise Linux WS 3"
echo "10 - SuSE 9.1 Professional"
echo "11 - Other"
echo -n "Select your distribution from the list above (q to quit):"
}

parse ()
{
case $DISTNUM in 
    1) 
        DISTDEF=REDHAT_72
        ;;
    2) 
        DISTDEF=REDHAT_73
        ;;
    3)  
        DISTDEF=REDHAT_8
        ;;
    4) 
        DISTDEF=REDHAT_9
        ;;
    5) 
        DISTDEF=FEDORA
        ;;
    6) 
        DISTDEF=REDHAT_AS_21
        ;;
    7) 
        DISTDEF=REDHAT_AS_3
        ;;
    8) 
        DISTDEF=REDHAT_ES_3
        ;;
    9)  
        DISTDEF=REDHAT_WS_3
        ;;
    10)  
        DISTDEF=SUSE_91
        ;;
    11)     
        DISTDEF=OTHER
        ;;
    q | Q)
        exit
        ;;    
    *) 
        prompt
        read DISTNUM
        parse
        ;;
esac
}

need_replace ()
{
    RET=1
    case $DIST in
    redhat | mandrake | unsupported)
        RET=0
        ;;
    esac           
    return $RET
}

echo_tar ()
{
    if [ $DIST == "unsupported" ]; then
        echo
        echo "The source RPM installation may not be supported on"
        echo "this distribution of Linux.  Check the documentation"
        echo "included with you distribution for information about"
        echo "installing source RPMs."
    fi
    if [ $B_TAR == "true" ]; then
        echo
        echo "A compressed tar file of the source is available as"
        echo "an alternative installation method. View the release"
        echo "notes for more information.  Within the extracted"
        echo "source, there may be INSTALL or README files with"
        echo "additional instructions."
    fi
    echo
    echo "Location of installation files:"
    echo $(dirname ${PKG})
    echo
}

dgdmstart ()
{
    echo "Loading the driver and building the devices... "
    /etc/rc.d/init.d/dgdm start

    echo "Adding dgdm to the /etc/rc.d initialization hierarchy... "
    which chkconfig &> /dev/null
    if [ $? -eq 0 ] 
    then
            chkconfig --add dgdm
    else
            echo "The \"chkconfig\" tool was not available on your system."
            echo "Please add the services initiated by /etc/rc.d/init.d/dgdm"
            echo "by the preferred method for your distribution."
    fi
}    
                    

debug ()
{
    echo PKG:$PKG
    echo PROD:$PROD
    echo SOFT:$SOFT
    echo PKG_NAME:$PKG_NAME
    echo B_TAR:$B_TAR
    echo RPMINSTALL:$RPMINSTALL
    echo_tar
    read dummy
    #exit 1
}

case $SOFT in
pam)
      DIRNAME=${PKG}
      BASENAME=setup
      PWD=$(pwd)
      cd ${DIRNAME}
      
      ./${BASENAME}
      if [ $? -ne 0 ]
      then
         echo " "
         echo "There was an error installing PortAssist Manager."
         echo "Try running setup directly from the software/pam directory."
         echo "Press <ENTER> to continue..."
         read -s dummy
      else     
         echo "To complete PortAssist Manager installation, you may need to re-start your server."      
         echo "Press <ENTER> to continue..."
         read -s dummy
         exit 0
      fi
      cd ${PWD}
      ;;

driver | rp | dpar)
        #
        #  Generic "Source RPM" installation format.  Currently includes
        #  the ClassicBoard PCI driver and the RealPort driver.
        #
        case $PROD in
        *)
            if [ $SOFT == "dpar" ]; then
                PKG_NAME=digidpar
            else
                PKG_NAME=realport
            fi
            B_TAR="true"
            ;;
        esac
        logheader
        CUR_VER=`rpm -q $PKG_NAME`
        if [ "$?" -eq 0 ]; then
            echo ""
            echo "$CUR_VER is currently installed and must be removed"
            echo "before this installation begins."
            echo "Would you like to have this package removed and"
            echo -n "continue with installation? (y/n):"
            read ans
            if [ _$ans != "_y" -a _$ans != "_Y" ]; then
                echo "Intstallation cancelled, no changes were made."
                echo "Press <ENTER> to continue..."
                read -s dummy
                exit 
            fi
            echo "Removing previous version of package..."
            rpm -e $PKG_NAME 2>&1 | tee $LOGFILETEMP
        fi
        echo "Installing the RPM... "
        
        if [ "$PKG_NAME" = "realport" ]
        then
            prompt
            read DISTNUM
            parse
            $RPMBUILD --rebuild --define DISTRO=${DISTDEF} ${PKG} 2>&1 | tee $LOGFILEB
        else
            $RPMBUILD --rebuild ${PKG} 2>&1 | tee $LOGFILEB
        fi
        set $(grep Wrote: $LOGFILEB)
        if [ "$1" = "Wrote:" ]
        then
            RPMFILE=$2    # param from "set" command above
            cat $LOGFILETEMP >> $LOGFILEI
            cat $LOGFILEB >> $LOGFILEI
            $RPMINSTALL $RPMFILE 2>&1 | tee -a $LOGFILEI
        else
            echo
            echo "ERROR: RPM file not written. See $LOGFILEB"
            echo "for build error messages."
            echo_tar
            echo "Press <ENTER> to continue..."
            read -s dummy              
            exit 1
        fi
        
        if [ $? -ne 0 ]
        then
            echo
            echo "ERROR: RPM installation has failed. See $LOGFILEI"
            echo "for build and install error messages."
            echo_tar
            echo "Press <ENTER> to continue..."
            read -s dummy              
            exit 1
        else
            echo ""
            echo "Installation successful.  See $LOGFILEI for a"
            echo "copy of the messages above."
            case $PKG_NAME in 
            dgdm)
                dgdmstart
                if [ z$GUI != "zNOGUI" ]; then
                    echo "dm_gui is used to configure your device."
                    echo -n "Would you like to run dm_gui now? (y/n):"
                    read ans
                    if [ _$ans == "_y" -o _$ans == "_Y" ]; then
                        dm_gui
                    fi
                fi
                ;;
            epca)
                echo "digiConf is used to configure your device."
                echo -n "Would you like to run digiConf now? (y/n):"
                read ans
                if [ _$ans == "_y" -o _$ans == "_Y" ]; then
                    digiConf
                fi
                ;;
            realport)
                if [ z$GUI != "zNOGUI" ]; then
                    echo "dgrp_gui is used to configure RealPort"
                    echo -n "Would you like to run dgrp_gui now? (y/n):"
                    read ans
                    if [ _$ans == "_y" -o _$ans == "_Y" ]; then
                        /usr/bin/dgrp/config/dgrp_gui
                    fi
                else
                    echo "Run dgrp_cfg_node to configure RealPort."
                    echo "Type man dgrp_cfg_node for instructions."
                fi
                ;;
            default)
                ;;
            esac
            
            echo "Press <ENTER> to continue..."
            read -s dummy
        fi      
        ;;

esac   

