#! /bin/bash
MYNAME=$(basename "$0")
MYDIR=$(dirname "$0")
MYDIR=$(readlink -m "$MYDIR")
VERSION="0.0.5"
VERBOSE=""
LOGFILE=""
SERVER="127.0.0.1"
SSH="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet"

if [ -f "$MYDIR/utilityfunctions.sh" ]; then
    source $MYDIR/utilityfunctions.sh
else
    echo "File '$MYDIR/utilityfunctions.sh' was not found." >&2
    exit 5
fi

#
# Prints the software version and exits.
#
function printVersionAndExit()
{
    echo "$MYNAME Version $VERSION on $(hostname)" >&2
}

function printHelpAndExit()
{
cat <<EOF
Usage:
  $MYNAME [OPTION]... [CONTAINER_NAME]...

  $MYNAME - .

  -h, --help           Print this help and exit.
  -v, --version        Print version information and exit.
  --verbose            Print more messages.
  --log-file=FILE      Store all the messages in the given file too.
  --server=SERVER      List the containers on remote server.

  --long               Print detailed list.

EOF
    exit 0
}

ARGS=$(\
    getopt \
        -o hvs:c:l \
        -l "help,verbose,version,log-file:,server:,long" \
        -- "$@")

if [ $? -ne 0 ]; then
    exit 6
fi

eval set -- "$ARGS"
while true; do
    case "$1" in
        -h|--help)
            shift
            printHelpAndExit
            ;;

        --verbose)
            shift
            VERBOSE="true"
            ;;

        -v|--version)
            shift
            printVersionAndExit
            ;;

        --log-file)
            shift
            LOGFILE=$(readlink -m "$1")
            shift
            ;;

        --server)
            shift
            SERVER="$1"
            shift
            ;;


        --long)
            shift
            OPTION_LONG="true"
            ;;

        --)
            shift
            break
            ;;

        *)
            ;;
    esac
done

EXTRA_OPTIONS=$*

function host_name()
{
    local ip="$1"
    local name

    if [ -z "$ip" -o "$ip" = "-" ]; then
        echo "-"
        return 1
    fi

    name=$(nslookup "$ip" | grep "name = " | awk '{print $4}')
    if [ -z "$name" ]; then
        name="-"
    fi

    echo "$name"
}

function print_header()
{
    cat <<EOF
CONTAINER_NAME           IP               DNS_NAME
-------------------------------------------------------------------------------
EOF

}

function list_containers_long()
{
    print_header

    local oldIfs="$IFS"
    local lines

    if [ "$VERBOSE" = "true" ]; then
        echo "$SSH $SERVER -- sudo lxc-ls --fancy | grep -v NAME"
    fi
    lines=$($SSH $SERVER -- sudo lxc-ls --fancy | grep -v NAME)
    IFS=$'\n'
    for line in $lines; do
        name=$(echo $line | awk '{print $1}')
        state=$(echo $line | awk '{print $2}')
        autostart=$(echo $line | awk '{print $3}')
        ip=$(echo $line | awk '{print $5}')
        dns_name=$(host_name "$ip")

        if [ "$state" = "RUNNING" ]; then
            printf "$CONTAINER_COLOR%-24s$TERM_NORMAL " $name
        else
            printf "$CONTAINER_COLOR_STOPPED%-24s$TERM_NORMAL " $name
        fi

        if [ "$ip" != "-" ]; then
            printf "$IP_COLOR%-16s$TERM_NORMAL " $ip
        else
            printf "$ERR_COLOR%-16s$TERM_NORMAL " $ip
        fi

        if [ "$dns_name" != "-" ]; then
            printf "$OK_COLOR%-20s$TERM_NORMAL " $dns_name
        else
            printf "$ERR_COLOR%-20s$TERM_NORMAL " $dns_name
        fi

        printf "\n"
    done
    IFS=$oldIfs
}

function list_containers_brief()
{
    local oldIfs="$IFS"
    local lines

    if [ -n "$SERVER" ]; then
        lines=$($SSH $SERVER -- sudo ls -1 /var/lib/lxc)
    else
        lines=$(sudo ls -1 /var/lib/lxc 2>/dev/null)
    fi

    IFS=$'\n'
    for line in $lines; do
        if sudo test -d "/var/lib/lxc/${line}"; then
            line=$(basename $line)
            echo "$line"
        fi
    done
    IFS=$oldIfs
}

#if [ -z "$SERVER" ]; then
#    printError "Server name is not provided."
#    printError "Use the --server command line option to set the server name."
#    exit 6
#fi

if [ -n "$OPTION_LONG" ]; then
    list_containers_long
else
    if [ -t 1 ]; then
        list_containers_brief | column
    else
        list_containers_brief
    fi
fi
