#!/bin/bash

if [ "$(basename $0)" == "qlprecorder" ]; then
    qlprec=1
else
    qlprec=0
fi

options=$(getopt -o "vr:du:htf" --long "version,record:,help-all,debug,fs:,fa:,strict-stream,quiet,url:" -- "$@")
[ $? -eq 0 ] || { 
    echo "Error: Incorrect options provided" >&2
    exit 1
}
eval set -- "$options"

while true; do
    case "$1" in
        -v|--version)
            opts+=("-V")
            shift
            ;;
        -r|--record)
            if [ $qlprec -eq 1 ]; then
                echo Error: There is no need to specify --record when qlprecorder is used. >&2
                exit 2
            fi
            opts+=("-r")
            echo Warning: Recording with filename provided is not supported by dmlive. >&2
            shift 2
            ;;
        --help-all|--help|-h)
            opts+=("-h")
            shift
            ;;
        -d|--debug)
            opts+=("--log-level" "1")
            shift
            ;;
        --fs|--fa|-f)
            echo Warning: ${1-} is not supported by dmlive. >&2
            shift 2
            ;;
        --strict-stream)
            echo Warning: ${1-} is not supported by dmlive. >&2
            shift
            ;;
        --quiet)
            opts+=("-q")
            shift
            ;;
        -u|--url)
            opts+=("-u" "${2-}")
            shift 2
            ;;
        -t)
            opts+=("-w" "${2-}")
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            echo Error: ${1-} is unknown!
            exit 1
            ;;
    esac
done

if [ $qlprec -eq 1 ]; then
    opts+=("-r")
fi

exec dmlive ${opts[@]}
