#!/bin/bash
set -e

STREAM=1

if test -n "$1"; then
    STREAM=$1
fi

if [ ! -f /tmp/ts.${STREAM}.pid ]; then
    if pkill -f "dumprtp 224.0.0.${STREAM}"; then
	echo "ts: was running without pidfile"
    else
	echo "ts: was not running"
    fi
    exit
fi

read TS_PID < /tmp/ts.${STREAM}.pid
if pkill -U ${TS_PID}; then
    echo "ts: stopped, pid was ${TS_PID}"
    rm -f /tmp/ts.${STREAM}.pid
else
    echo "ts: pid ${TS_PID} was not found"
    rm -f /tmp/ts.${STREAM}.pid
    if pkill -f "dumprtp 224.0.0.${STREAM}"; then
	echo "ts: was running with different pid"
    fi
fi
