#!/bin/sh
#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2020-2020 Bareos GmbH & Co. KG
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   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
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.


set -u
set -e

self="$1"
#out="@tmpdir@/$self"
out="@tmpdir@/command-$self.out"

> $out

#printf "dir=%s\n" "$BAREOS_DIRECTOR_BINARY" >> $out
#printf "fd =%s\n" "$BAREOS_FILEDAEMON_BINARY" >> $out

if [ "$self" = "client" ]; then
    parentbin="$BAREOS_FILEDAEMON_BINARY"
elif [ "$self" = "server" ]; then
    parentbin="$BAREOS_DIRECTOR_BINARY"
else
    echo "Invalid parameter. Must be 'client' or 'server'".
    exit 1
fi

printf "PID=%s\n" "$$" >> $out
printf "looking for parent process: %s\n" "$parentbin" >> $out

PID=$$
while [ $PID -gt 1 ]; do
  #CMD=$(ps -fp $PID -o comm=)
  FULLCMD=$(ps -fp $PID -o args=)
  printf "PID=%s: %s\n" "$PID" "$FULLCMD" >> $out
  if printf "%s" "$FULLCMD" | grep -q $parentbin ; then
    printf "found calling bareos daemon (%s)\n" "$self" >> $out
    # backup the log file.
    printf "%s\n" "$out"
    printf "%s\n" "@tmpdir@/common"
    printf "%s\n" "@tmpdir@/command-$self"
    exit 0
  fi
  PID=$(ps -o ppid= -p $PID)
done

echo "EXPECTED PARENT NOT FOUND ($parentbin)" | tee -a $out
#exit 1
