#!/bin/bash

PROGNAME=$(basename $0)

: ${USERNAME:=dlee}
: ${SECRET:=password}

function make_line() {
    line_length=$1
    prefix=$2
    suffix=$3
    suffix_len=$(( ${line_length} - ${#prefix} - 1 )) # take off 1 for the newline
    printf "%s%*s" ${prefix} ${suffix_len} ${suffix}
}

line_length=$1

if test -z ${line_length}; then
    echo "Usage: ${PROGNAME} line-length" >&2
    exit 1
fi

echo == ${line_length} ==
nc localhost 5038 <<EOF
action: login
username: ${USERNAME}
secret: ${SECRET}

action: getvar
variable: foo
$(make_line ${line_length} variable: foo-${line_length})
actionid: 12345

EOF

if test $? -ne 0; then
    echo ${PROGNAME}: netcat failed to connect to AMI >&2
    exit 1
fi
