#!/bin/bash

filename="$1"
neType="$2"
duration=180
expectBin=/usr/bin/expect
omcBin=/usr/local/omc/bin
logFile=/tmp/actpkg`date +%Y%m%d`.log
extension=$(basename "${1}" | awk -F . '{print $NF}')

if [ ! -f ${expectBin} ]; then
    echo "Please install software expect first"
    exit 99
fi

case "${extension}" in
    deb)
        if [ ${neType} == "OMC" ]; then
            systemctl stop restagent.service
        fi    
        expect <<EOF > ${logFile}
            spawn dpkg -i --force-all "$filename"
            set timeout ${duration}
            expect {
                "y/n" { send "y\n"; exp_continue }
            }
EOF
        if [ ${neType} == "OMC" ]; then
            ${omcBin}/setomc.sh -m upgrade >> ${logFile}
            ${omcBin}/omcsvc.sh restart >> ${logFile}
        fi
        ;;
    rpm)
        if [ ${neType} == "OMC" ]; then
            systemctl stop restagent.service
        fi    
        expect <<EOF > ${logFile}
            spawn rpm -Uvh "$filename"
            set timeout ${duration}
            expect {
                "y/n" { send "y\n"; exp_continue }
            }
EOF
        if [ ${neType} == "OMC" ]; then
            ${omcBin}/setomc.sh -m upgrade >> ${logFile}
            ${omcBin}/omcsvc.sh restart >> ${logFile}
        fi
        ;;
   *)

        echo "The file does not have a .deb or .rpm extension"
        exit 100                 
        ;;
esac

