#!/bin/bash
#
# Copyright (C) 2025 Eugene 'Vindex' Stulin
# Distributed under the Boost Software License 1.0.

set -eu -o pipefail

# The function prints help information.
Print_Help() {
cat <<EndOfHelp
Find information about the specified busy port.

Usage:
    ${0##*/} <port>

Example:
    ${0##*/} 80

Help and version:
    ${0##*/} --help|-h
    ${0##*/} --version|-v
EndOfHelp
}


# The function prints information about wrong usage to stderr.
Wrong_Usage() {
    echo "Wrong usage. See: ${0##*/} --help" >&2
}


# The function prints the script version.
Print_Version() {
    local VERSION_SCRIPT="$(dirname -- "${BASH_SOURCE[0]}")/bbsi-version"
    "$VERSION_SCRIPT"
}


if [[ $# -ne 1 ]]; then
    Wrong_Usage
    exit 1
elif [[ "$1" == "-h" || "$1" == "--help" ]]; then
    Print_Help
    exit 0
elif [[ "$1" == "-v" || "$1" == "--version" ]]; then
    Print_Version
    exit 0
fi

if ! command -v netstat &>/dev/null ; then
    echo "netstat is not installed. It is required for the script to work" >&2
    echo "Most likely, the relevant package is called net-tools" >&2
    exit 1
fi

netstat -tunlp 2>/dev/null | grep ":$1 "
