#!/bin/bash

##-r, --register=idekey
#            Register client with DBGp proxy

proxyPort=9001
proxyHost=10.0.0.101
localPort=9003
ideKey=PHPSTORM


for arg in "$@"
do
    case $arg in
        -h|--help)
            # Wenn der Parameter -h oder --help ist, zeige eine Hilfe und beende das Skript
            echo "Der Skript baut eine Verbindung zum DBGp Proxy-Server auf. Dabei wird IDE-Key automatisch registriert."
			echo "Dabei wird bestehende verbindung getrennt."
            echo "Usage: $0 [options]"
            echo "Options:"
            echo "  -h, --help     Show this help message"
            echo "  -r, --register     Register client with DBGp proxy"
            echo "  -y, --proxy=host:port     Register with a DBGp proxy [$proxyHost:$proxyPort]"
            echo ""
			echo "Beispiel:"
			echo "dbgp-proxy-tunnel -r $ideKey $proxyHost"
            exit 0
            ;;
        -r|--register)
            if [ -n "$2" ]; then
                ideKey=$2
                #shift # Überspringe das nächste Argument
            else
                echo "Error: Missing argument for option $1"
                exit 1
            fi
            ;;
        -y|--proxy)
            if [ -n "$2" ]; then
                
				if echo "$2" | grep -q ":"; then
					proxyPort=$(echo $2 | cut -d ":" -f 2)
					proxyHost=$(echo $2 | cut -d ":" -f 1)
				else
					proxyHost=$2
				fi


                #shift # Überspringe das nächste Argument
            else
                echo "Error: Missing argument for option $1"
                exit 1
            fi
            ;;
        *)
            # Behandeln Sie hier weitere spezifische Parameter
            #echo "Unknown option: $1"
            #exit 1
            ;;
    esac
    shift # Überspringe das aktuelle Argument
done

echo "IDE Key: $ideKey"
echo "Proxy Port: $proxyPort"
echo "Proxy Host: $proxyHost"
echo "Local Port: $localPort"

killall -9 dbgpClient 2> /dev/null
dbgpClient -r $ideKey -y $proxyHost:$proxyPort -p $localPort