#!/bin/bash
#
#   YADT - an Augmented Deployment Tool
#   Copyright (C) 2010-2014  Immobilien Scout GmbH
#
#   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 3 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 program.  If not, see <http://www.gnu.org/licenses/>.

set -u -e -E -C -o pipefail

TARGETS=${*:-*}
LOCK_FILE=/var/lock/yadt/$(basename $0 .sh)
SYNC_CALL=$(dirname $(readlink -e $0))/sync_logs_of_target.py

LOG_DIR=$(sed -n "/^LOG_DIR_PREFIX/{s/^.*=\s*//;s/'//g;s_/\$__;p}" /etc/yadtshell/loggingconf.py)
LOG_DIR=$LOG_DIR/$(date +%Y-%m-%d)
NOW=$(date +%Y-%m-%d--%H-%M-%S)
HOST=$(hostname -s)
USERNAME=$(whoami)

mkdir -p $LOG_DIR
LOG_FILE=$LOG_DIR/yadtshell.$HOST.$NOW.$USERNAME.000.$HOST.log

exec 3>&1 4>&2 1>$LOG_FILE 2>&1

echo "starting on $NOW"
echo "pid: $$"
echo "command line: $*"

if ! echo $$ > $LOCK_FILE; then
    echo "cannot acquire lock: pid of lock holder is $(cat $LOCK_FILE), aborting here" >&2
    exit 1
fi

cleanup(){
    echo releasing lock $LOCK_FILE
    rm -f $LOCK_FILE
    echo finished after $SECONDS seconds.
    echo
}
trap cleanup EXIT

for thisFILE in $TARGETS; do
    [[ -d $thisFILE ]] || continue
    [[ -e $thisFILE/target ]] || continue
    (
        echo "processing $thisFILE"
        cd $thisFILE
        if ! $SYNC_CALL; then
            echo "problems while syncing $thisFILE, continuing"
        fi
    )
done

