#!/bin/ksh

# $Id: roller.ksh 603 2010-12-29 15:37:28Z olbohlen@EWENET.EWE.DE $
# hopefully an universal and automagic file and patch deployment tool
# written by Olaf 'I am sooo tired of writing a new rollout script for every package' Bohlen
# olaf.bohlen@btc-it-services.com 2010-12-27

usage() {
    cat <<EOF
Usage: $0 [ install | remove | info | run ] [ pkg | patch | tar | script ] [ filename ] [ hostname ]

EOF
}

prep_workdir() {
    ${SSHCMD} ${CLIENT} "mkdir -p /var/tmp/roller-work"
    ${SCPCMD} ${FILENAME} ${CLIENT}:/var/tmp/roller-work
}

clr_workdir() {
    ${SSHCMD} ${CLIENT} "/usr/bin/rm -f /var/tmp/roller-work/${FILENAME}"
}

pr_status() {
    typeset rt

    rt=$1
    echo "I: tried to ${ACTION} ${MODE} ${FILENAME} on ${CLIENT} returned with ${rt}"
}

run_script() {
    prep_workdir

    ${SSHCMD} ${CLIENT} /var/tmp/roller-work/${FILENAME}
    rt=$?
    pr_status ${rt}
    clr_workdir
    exit ${rt}
}

rem_pkg() {
    typeset rt

    prep_workdir
    cat | ${SSHCMD} ${CLIENT} "cat - >/var/tmp/roller-work/admin" <<EOF
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=quit
space=quit
setuid=nocheck
conflict=nocheck
action=nocheck
networktimeout=60
networkretries=3
authentication=quit
keystore=/var/sadm/security
proxy=
basedir=default
EOF

    ${SSHCMD} ${CLIENT} "/usr/bin/yes y | /usr/sbin/pkgrm -a /var/tmp/roller-work/admin ${FILENAME}"
    rt=$?
    pr_status ${rt}
    clr_workdir
    exit ${rt}
    
}

inst_pkg() {
    typeset pkgflag
    typeset rt
    # copy file to server
    prep_workdir
    cat | ${SSHCMD} ${CLIENT} "cat - >/var/tmp/roller-work/admin" <<EOF
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=quit
space=quit
setuid=nocheck
conflict=nocheck
action=nocheck
networktimeout=60
networkretries=3
authentication=quit
keystore=/var/sadm/security
proxy=
basedir=default
EOF
    if [ x$(${SSHCMD} ${CLIENT} "uname -r") == x5.10 ]; then
	pkgflag=-G
    else
	pkgflag=""
    fi
    ${SSHCMD} ${CLIENT} "/usr/bin/yes y | /usr/sbin/pkgadd -a /var/tmp/roller-work/admin ${pkgflag} -d /var/tmp/roller-work/${FILENAME} all"
    rt=$?
    pr_status ${rt}
    clr_workdir
    exit ${rt}
}

inst_patch() {
    typeset rt

    # copy patch
    prep_workdir

    # patchadd patch
    ${SSHCMD} ${CLIENT} "/usr/sbin/patchadd -G /var/tmp/roller-work/${FILENAME}"
    rt=$?
    pr_status ${rt}
    clr_workdir
    exit ${rt}
}

# MAIN #
########

SSHCMD="ssh"
SCPCMD="scp"
TIMEOUT="-o ConnectTimeout=5"
#SSHCMD="sshroot"
#SCPCMD=scproot

ACTION=$1   # install, remove, info, run (run filename on remote host)
MODE=$2     # pkg, patch, tar, ...
FILENAME=$3 # file containing patch, pkg, tar, script, whatever
CLIENT=$4   # clients host name

if [ x${CLIENT} == x ]; then
    echo "E: missing target clients hostname..."
    exit 1
fi

# sanity check, is CLIENT reachable? if not, terminate
${SSHCMD} ${CLIENT} "exit 42"
if [ $? -ne 42 ]; then
    # something went wrong...
    echo "E: host ${CLIENT} not reachable, skipping..."
    exit 1
fi

case ${ACTION} in
    install) inst_${MODE}
	;;

    remove) rem_${MODE}
	;;

    info) info_${MODE}
	;;

    run) run_${MODE}
	;;

    *) usage
       exit 1;;
esac

