#! /bin/bash

#*********************************************************************
#
# fai-save-log -- save log files from fai to a local or remote location
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2002-2010 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# 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.
# 
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
save_log_local() {


    # can be an extern script
    # save log files on local disk

    local logbase=$FAI_ROOT/var/log/fai
    local thislog=$logbase/$HOSTNAME/$FAI_ACTION-$fai_rundate

    find $LOGDIR -xdev -size 0 -type f -exec rm {} \;
    # create the symbolic links forcefully; in case of dirinstalls or
    # softupdates they already exist as the fai script creates them together
    # with the directory $LOGDIR
    ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last-$FAI_ACTION
    ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last

    [ -d "$thislog" ] && return # nothing to do, if directory already exists

    mkdir -p $thislog
    cp -a $LOGDIR/* $thislog
    chown root:adm $thislog
    chmod 0750 $thislog
    ln -snf $HOSTNAME $logbase/localhost
    ln -snf $FAI_ACTION-$fai_rundate $logbase/$HOSTNAME/last-$FAI_ACTION
    ln -snf $FAI_ACTION-$fai_rundate $logbase/$HOSTNAME/last
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
save_log_remote_shell() {

    local thislogdir
    if [ -n "$LOGREMOTEDIR" ]; then
        thislogdir=$LOGREMOTEDIR/$HOSTNAME/
    else
        thislogdir=$HOSTNAME/
    fi

    local thislog
    thislog=$thislogdir/$FAI_ACTION-$fai_rundate

    echo "Save log files via $remotesh to $LOGUSER@$LOGSERVER:$thislog"
    find $LOGDIR -xdev -size 0 -type f -exec rm {} \;
    $remotesh -l $LOGUSER $LOGSERVER " \
       mkdir -p $thislog ;\
       cd $thislogdir ;\
       ln -snf $FAI_ACTION-$fai_rundate last-$FAI_ACTION ;\
       ln -snf $FAI_ACTION-$fai_rundate last"

    $remotecp -pr $LOGDIR/* $LOGUSER@$LOGSERVER:$thislog
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
save_log_remote() {

    # save log files to $LOGUSER/$HOSTNAME/.. on $LOGSERVER
    # also create a link last-$FAI_ACTION the the directory of the
    # last action. The name of the log directory contains date and
    # time of the action performed

    if [ -z "$LOGUSER" ]; then
        echo "\$LOGUSER is undefined. Not saving log files to remote."
        return
    fi
    # LOGSERVER is overridable until now
    : ${LOGSERVER:=$SERVER}
    if [ -z "$LOGSERVER" ]; then
        echo "\$LOGSERVER is undefined. Not saving log files to remote."
        return
    fi

    case "$FAI_LOGPROTO" in
        ftp)
            fai-savelog-ftp ;;
        none)
            echo "Don't save logs with remote method, only local storage." ;;
        ssh)
            export remotesh=ssh
            export remotecp=scp
            save_log_remote_shell ;;
        rsh)
            export remotesh=rsh
            export remotecp=rcp
            save_log_remote_shell ;;
    esac
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {

    cat <<EOF
fai-savelog, save log files from fai to a local or remote location

   Copyright (C) 2002-2010 by Thomas Lange

Usage: fai-savelog [OPTION]

   -r       Save log files to \$LOGSERVER using rcp or scp
   -l       Save log files to local directory \$FAI_ROOT/var/log/fai

EOF
exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

[ "$1" ] || usage

: ${FAI_ACTION:=noaction}
while getopts lr opt ; do
        case "$opt" in
        l) save_log_local ;;
        r) save_log_remote ;;
        *) usage ;;
        esac
done
