#!/bin/sh
# vim: set sw=4 expandtab:
#
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright 2017, Jérôme Pouiller <jerome.pouiller@gmail.com>
#

usage() {
    echo 'Usage: relink PID'
    echo 'Redirect outputs of a running program as if it was output of this command.'
    echo 'On exit, this script automatically detach from PID, original output of PID'
    echo 'is restored, and PID continue to run normaly'
    echo
    echo 'Exemples:'
    echo '  relink $(pidof dhclient)'
    echo '  relink $(pidof dhclient) | grep usefull_line'
}

# detach
atexit() {
  sh $RESTORE_SCRIPT
  rm $RESTORE_SCRIPT
  # Note: no need to kill 'cat'
  rm $FIFO_OUT
  rm $FIFO_ERR
  exit 0
}
trap atexit INT

if [ "$#" -ne 1 ]; then
    usage
    exit 1
fi

PID=$1
FIFO_OUT=$(mktemp -u)
FIFO_ERR=$(mktemp -u)
RESTORE_SCRIPT=$(mktemp -u)
mkfifo $FIFO_OUT
mkfifo $FIFO_ERR
cat $FIFO_ERR >&2 &
cat $FIFO_OUT &
CAT_PID=$!
reredirect -o $FIFO_OUT -e $FIFO_ERR $PID > $RESTORE_SCRIPT || atexit
wait $CAT_PID

# $PID has stopped
rm $RESTORE_SCRIPT
rm $FIFO_OUT
rm $FIFO_ERR
exit 0

