#!/bin/sh

# apt-dater - terminal-based remote package update manager
#
# $Id: cmd 582 2011-09-27 13:45:43Z ellguth $
#
# Authors:
#   Andre Ellguth <ellguth@ibh.de>
#   Thomas Liske <liske@ibh.de>
#
# Copyright Holder:
#   2009 (C) IBH IT-Service GmbH [http://www.ibh.de/apt-dater/]
#
# License:
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this package; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

run_hist_cmd () {
    echo -n > "$AD_HIST_PATH/command"
    for i in "$@"; do
	echo -n "\"$i\" " >> "$AD_HIST_PATH/command"
    done

    chmod +x "$AD_HIST_PATH/command"
    script -c "$AD_HIST_PATH/command" -t "$AD_HIST_PATH/typescript" 2> "$AD_HIST_PATH/timingfile"

    echo -n "Duration=" >> "$AD_HIST_PATH/meta"
    cat "$AD_HIST_PATH/timingfile" | cut '-d ' -f1 | awk '{ s += $1 } END { print s }' >> "$AD_HIST_PATH/meta"

    if (egrep -aiqse "$AD_HIST_ERRPATTERN" "$AD_HIST_PATH/typescript"); then
	echo "ERRPATTERN=$AD_HIST_ERRPATTERN" >> "$AD_HIST_PATH/meta"
	touch "$AD_HIST_PATH/failed"
    fi
}

run_cmd () {
    $@
}

run_hook () {
    if [ -n "$1" -a -d "$1" ]; then
	run-parts "$1"
    fi
}

if [ "$AD_HIST_RECORD" = "true" -a "$AD_ACTION" != 'refresh' ]; then
    WRAP="run_hist_cmd"
else
    WRAP="run_cmd"
fi

handle_generic_ssh () {
    if [ -n "$AD_SSH_USER" ]; then
	my_ssh_user="-l $AD_SSH_USER";
    else
	my_ssh_user=""
    fi

    if [ -n "$AD_SSH_PORT" ]; then
	my_ssh_port="-p $AD_SSH_PORT";
    else
	my_ssh_port=""
    fi

    case "$AD_ACTION" in
	connect)
	    run_hook "$AD_HOOK_PRE_CONNECT"
	    $WRAP $AD_SSH_CMD $AD_SSH_ID $AD_SSH_OPTFLAGS $my_ssh_user $my_ssh_port "$AD_HOSTNAME"
	    run_hook "$AD_HOOK_POST_CONNECT"
	    ;;
	transfer)
	    if [ -n "$AD_SSH_USER" ]; then
		my_sftp_user="$AD_SSH_USER@";
	    else
		my_sftp_user=""
	    fi

	    if [ -n "$AD_SSH_PORT" ]; then
		my_sftp_port="-oPort=$AD_SSH_PORT";
	    else
		my_sftp_port=""
	    fi

	    run_hook "$AD_HOOK_PRE_CONNECT"
	    $WRAP $AD_SFTP_CMD $AD_SSH_ID $my_ssh_port $my_sftp_user"$AD_HOSTNAME"
	    run_hook "$AD_HOOK_POST_CONNECT"
	    ;;
	install)
	    run_hook "$AD_HOOK_PRE_INSTALL"
	    $WRAP $AD_SSH_CMD $AD_SSH_ID $AD_SSH_OPTFLAGS $my_ssh_user $my_ssh_port "$AD_HOSTNAME" `printf "$AD_CMD_INSTALL" "$AD_PARAM"`
	    run_hook "$AD_HOOK_POST_INSTALL"
	    ;;
	upgrade)
	    run_hook "$AD_HOOK_PRE_UPGRADE"
	    $WRAP $AD_SSH_CMD $AD_SSH_ID $AD_SSH_OPTFLAGS $my_ssh_user $my_ssh_port "$AD_HOSTNAME" $AD_CMD_UPGRADE
	    run_hook "$AD_HOOK_POST_UPGRADE"
	    ;;
	refresh)
	    run_hook "$AD_HOOK_PRE_REFRESH"
	    $AD_SSH_CMD $AD_SSH_ID -n -o BatchMode=yes -o ConnectTimeout=5 -q $my_ssh_user $my_ssh_port "$AD_HOSTNAME" $AD_CMD_REFRESH
	    run_hook "$AD_HOOK_POST_REFRESH"
	    ;;
	*)
	    echo "Unhandled action '$AD_ACTION'!" 1>&2
	    echo 1>&2
	    set | grep ^AD_ 1>&2
	    echo 1>&2
	    exit 1;
	;;
    esac
}

if [ -z "$AD_TYPE" -o "$AD_TYPE" = 'generic-ssh' ]; then
    handle_generic_ssh
else
    if [ -x "$AD_PLUGINDIR/$AD_TYPE/cmd" ]; then
	$WRAP "$AD_PLUGINDIR/$AD_TYPE/cmd"
    else
	echo "No plugin to handle type '$AD_TYPE'!" 1>&2
	echo 1>&2
	set | grep ^AD_ 1>&2
	echo 1>&2
	exit 2;
    fi
fi
