DEFAULT_SERVICE_NAME="ghost-node.service"

check_unit_file_validity() {
    SERVICE_NAME=$1
    if [[ "$SERVICE_NAME" != *.service ]]; then
        SERVICE_NAME="${SERVICE_NAME}.service"
    fi

    SERVICE_FULL_PATH="/etc/systemd/system/$SERVICE_NAME"
    if [[ -e "$SERVICE_FULL_PATH" ]]; then
        NETWORK_ONLINE_EXISTS=$(grep -Fx "After=network-online.target" "$SERVICE_FULL_PATH")
        NETWORK_EXISTS=$(grep -Fx "After=network.target"  "$SERVICE_FULL_PATH")

        if [[ -z "$NETWORK_ONLINE_EXISTS" && -n "$NETWORK_EXISTS" ]]
        then
            echo "[-] WARNING: missing network-online.target dependency in $SERVICE_FULL_PATH, trying to replace"
            sudo sed -i "s/After=network.target/After=network-online.target\nRequires=network-online.target/g" "$SERVICE_FULL_PATH"
        else
            echo "[+] INFO: network-online.target is set correctly for $SERVICE_FULL_PATH"
        fi
    else
        echo "[-] No serivce found at $SERVICE_FULL_PATH"
    fi

}

check_unit_file_validity $DEFAULT_SERVICE_NAME
read -p "[?] Enter names for the node service, separated by commas (default: ghost-node): " -a SERVICE_NAMES
for NAME in "${SERVICE_NAMES[@]}"; do
    check_unit_file_validity $NAME 
done
sudo systemctl daemon-reload
