#!/bin/sh


show_name=0
show_ver=0
show_arch=0
show_default=1
fname=""

usage="usage: $0 [option]... <cls_filename>\n"
usage=$usage"options:\n"
usage=$usage"\t-n, --name\n"
usage=$usage"\t-v, --ver\n"
usage=$usage"\t-a, --arch\n"

usage_exit() {
	printf "$usage"
	exit 1
}

err_exit() {
	echo "Error: $*"
	exit 1
}

while [ $# -ge 1 ]; do
case $1 in
    -n | --name )
	    show_name=1
	    show_default=0
	    ;;
    -v | --ver )
	    show_ver=1
	    show_default=0
	    ;;
    -a | --arch )
	    show_arch=1
	    show_default=0
	    ;;
    * )
	    [ -n "$fname" ] && usage_exit
	    fname=$1
	    ;;

esac
shift
done

[ -z "$fname" ] && usage_exit

if [ $show_default -eq 1 ]; then
	show_name=1
	show_ver=1
	show_arch=1
fi

[ -e "$fname" ] || err_exit "File not found: ${fname}"

if [ $show_name -eq 1 ]; then
	raw_name=`nm $fname | grep __cls_name__`
	name=`echo $raw_name | sed 's/.*cls_name__//g'`
	[ -z "$name" ] && err_exit "Could not detect class name"
	s=$name
	c=" "
fi

if [ $show_ver -eq 1 ]; then
	raw_ver=`nm $fname | grep __cls_ver__`
	ver=`echo $raw_ver | sed 's/.*cls_ver__//g; s/_/./g'`
	[ -z "$ver" ] && err_exit "Could not detect class version"
	s=$s$c$ver
	c=" "
fi

if [ $show_arch -eq 1 ]; then
	raw_arch=`readelf -h $fname | grep Machine`
	arch=""
	[ `echo $raw_arch | grep -c 386` -gt 0 ] && arch="i386"
	[ `echo $raw_arch | grep -c 86-64` -gt 0 ] && arch="x86-64"
	[ -z "$arch" ] && err_exit "unknown file architecture"
	s=$s$c$arch
	c=" "
fi

echo $s
