#!/bin/bash

set -e
set -x

stream_server="rtmp://49.235.45.154:1935"
framerate=30
main_bitrate=720
sub_bitrate=480
process_name="streamer_whip"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

if [ ! -f "$SCRIPT_DIR/$process_name" ]; then
    echo "可执行文件${SCRIPT_DIR}/${process_name}不存在"
    exit 1
fi

if [ ! -x "$SCRIPT_DIR/$process_name" ]; then
    echo "可执行文件${process_name}没有执行权限"
    exit 1
fi

echo $SCRIPT_DIR/$process_name

stop() {
    ps -ef|grep "${process_name}"|grep -v grep|awk '{print $2}'|xargs -I{} kill -9 {}
    
    # trigger_count=$(ps -ef | grep "tztek-jetson-tool-internal-trigger-camera" | grep -v grep| wc -l)
    # if [ $trigger_count -gt 0 ]; then
    #     ps -ef|grep "tztek-jetson-tool-internal-trigger-camera" |grep -v grep| awk '{print $2}' | xargs -I{} sudo kill -9 {}
    # fi
}


start() {
    gst_count=$(ps -ef | grep "${process_name}" | grep -v grep| wc -l)
    if [ $gst_count -ge 1 ]; then
        exit 0
    fi
    
    sleep 1
    
    nohup $SCRIPT_DIR/$process_name -c $SCRIPT_DIR/config_sdgaosu.yaml -d $1 >$SCRIPT_DIR/running.log 2>&1 &
}

if [ $# -eq 0 ]; then
    stop
    echo "Usage: $0 [veh01 | veh02 | stop]"
    exit 1
else
    case $1 in
        veh01)
            start veh01
        ;;
        veh02)
            start veh02
        ;;
        veh03)
            start veh03
        ;;
        veh04)
            start veh04
        ;;
        stop)
            stop
        ;;
        *)
            echo "Usage: $0 [veh01 | veh02 | stop]"
            exit 1
        ;;
    esac
fi
