#! /bin/bash
#
# author: Mike Becher <mibe@hrz.tu-chemnitz.de>
#
# cmd:
#  pbsnodes-short
#
# description:
#  prints pbsnodes -a output at one line
#
# pbsnodes -a output is:
#clic4l43
#     state = offline
#     np = 1
#     properties = BDmodule6
#     ntype = cluster
#     jobs = 0/1346.clic0a1.hrz.tu-chemnitz.de

getprog() {
  local arg=$1

  if [ "_$arg" = "_" ]; then
    return 1
  fi

  type $arg 2>/dev/null | \
	awk '{print $NF}' 2>/dev/null | \
	sed 's/.*(//g;s/).*//g;' 2>/dev/null

  return 0
}

usage() {
  cat <<EOF

usage: $prog_name [options]

options:
  -h|-help|--help
           print this message and exit
  -s <server>
           query <server> instead of default server

description:
  print output from pbsnodes command at one line

EOF
  return 0;
}

# some default settings
option_server=""
prog_name=$( basename $0; )

# process options
while [ "_$1" != "_" ]
do
  case "$1" in
   -h|-help|--help)
    usage
    exit 0
    ;;
   -s)
    if [ "_$2" != "_" ]
    then
      option_server="-s $2"
      shift 1
    else
      usage
      cat >&2 <<EOF
error: option -s needs an argument <server>
EOF
      exit 1
    fi
    ;;
   *)
    usage
    cat >&2 <<EOF
error: unknown option "$1"
EOF
    exit 1
    ;;
  esac 
  shift 1
done

# try first users path
pbsnodes_bin=$( getprog pbsnodes; )
awk_bin=$( getprog gawk; )
sort_bin=$( getprog sort; )

# local, global and other stuff path
OLD_PATH="$PATH"
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin"
PATH="$PATH:/usr/local/bin:/usr/local/sbin"
PATH="$PATH:/uni/global/bin:/uni/dept/bin"
PATH="$PATH:/usr/afsws/bin:/usr/kerberos/bin"
[ "_$OLD_PATH" != "_" ] && PATH="$PATH:$OLD_PATH"
export PATH

# looking for pbsnodes if not found already
[ "_$pbsnodes_bin" = "_" ] && \
  pbsnodes_bin=$( getprog pbsnodes; )

# looking for gawk if not found already
[ "_$awk_bin" = "_" ] && \
	awk_bin=$( getprog gawk; )

# looking for standard awk if gawk was not found already
[ "_$awk_bin" = "_" ] && \
	awk_bin=$( getprog awk; )

# looking for sort if not found already
[ "_$sort_bin" = "_" ] && \
	awk_bin=$( getprog sort; )

# did we got a valid pbsnodes command?
if [ "_$pbsnodes_bin" = "_" ]
then
  echo "error: pbsnodes command not found ... abort" >&2
  exit 1
fi

# did we got a valid (g)awk command?
if [ "_$awk_bin" = "_" ]
then
  echo "error: (g)awk command not found ... abort" >&2
  exit 1
fi

if [ "_$sort_bin" != "_" ]
then
 $pbsnodes_bin -a $option_server | \
 $awk_bin '
BEGIN{
	started=0;
}

/^[a-zA-Z]/{ 
	if (started == 0) {
		started=1;
	}

	if (started == 1) {
		currnode=$1;
		nodestate[currnode]="unknown";
		nodenp[currnode]="0";
		nodeproperties[currnode]="-";
		nodentype[currnode]="unknown";
		nodejobs[currnode]="unused";
	}
}
/state \=/{
	if (started == 1) {
	  nodestate[currnode]=$NF;
	}
}
/np \=/{
	if (started == 1) {
	  nodenp[currnode]=$NF;
	}
}
/properties \=/{
	if (started == 1) {
	  nodeproperties[currnode]=$NF;
	}
}
/ntype \=/{
	if (started == 1) {
	  nodentype[currnode]=$NF;
	}
}
/jobs \=/{
	if (started == 1) {
	  nodejobs[currnode]=$NF;
	}
}

END {
	if (started == 1) {
	  for (node in nodentype) {
	    printf("%s (%s) (%s) (%s) (%s) (%s)\n",
		node, 
		nodentype[node], 
		nodenp[node],
		nodeproperties[node],
		nodestate[node],
		nodejobs[node]);
	  }
	} else {
	  print "no nodes found";
	}
}' | $sort_bin
else 
 $pbsnodes_bin -a $option_server | \
 $awk_bin '
BEGIN{
	started=0;
}

/^[a-zA-Z]/{ 
	if (started == 0) {
		started=1;
	}

	if (started == 1) {
		currnode=$1;
		nodestate[currnode]="unknown";
		nodenp[currnode]="0";
		nodeproperties[currnode]="-";
		nodentype[currnode]="unknown";
		nodejobs[currnode]="unused";
	}
}
/state \=/{
	if (started == 1) {
	  nodestate[currnode]=$NF;
	}
}
/np \=/{
	if (started == 1) {
	  nodenp[currnode]=$NF;
	}
}
/properties \=/{
	if (started == 1) {
	  nodeproperties[currnode]=$NF;
	}
}
/ntype \=/{
	if (started == 1) {
	  nodentype[currnode]=$NF;
	}
}
/jobs \=/{
	if (started == 1) {
	  nodejobs[currnode]=$NF;
	}
}

END {
	if (started == 1) {
	  for (node in nodentype) {
	    printf("%s (%s) (%s) (%s) (%s) (%s)\n",
		node, 
		nodentype[node], 
		nodenp[node],
		nodeproperties[node],
		nodestate[node],
		nodejobs[node]);
	  }
	} else {
	  print "no nodes found";
	}
}'
fi

exit 0
