#!/bin/bash

# set -x


function using() {
	echo "ver
	-a	show all version
	show shor version
	"
}

while getopts "a" opt
do
	case $opt in
		a)
			ISALL=1
			;;
		?)
			using
			exit 0
			;;
	esac
done


if [ -z "${PREFIX_DIR}" ]; then
	PREFIX_DIR=/usr/lib/`gcc -print-multiarch`/mwv207
	if [ ! -d ${PREFIX_DIR} ]; then
		echo "not install mwv207 driver!"
		exit 0
	fi
fi

function main() {
	SOFILES=$(ls -l $(find ${PREFIX_DIR} -name '*.so*') | sed -n '/^l/!p' | awk '{ print $NF }')
	for SOF in ${SOFILES} /usr/lib/xorg/modules/drivers/mwv207_drv.so /usr/lib/`gcc -print-multiarch`/dri/jmgpu_dri.so /usr/lib/`gcc -print-multiarch`/libdrm_jmgpu.so.1.0.0
	do
		if [ -z "${SOF}" ]; then
			continue
		fi
		if [ ! -e "${SOF}" ]; then
			continue
		fi
		# .so file name
		SHORT_FLN=$(echo ${SOF} | awk -F/ '{print $NF}')

		# $VERSION$1.1.6:19160$
		LONG_VERSION=$(strings ${SOF} | grep '\$VERSION\$')
		if [ -z "${LONG_VERSION}" ]; then
			continue;
		fi

		XYZ_VERSION=$(echo ${LONG_VERSION} | awk -F: '{ print $1 }' | awk -F\$ '{print $NF}')
		SHORT_TIME=$(echo ${LONG_VERSION} | awk -F: '{ print $NF }' | awk -F\$ '{print $1}')
		SHORT_LEN=0
		if [ ! -z "${SHORT_TIME}" ]; then
			SHORT_LEN=$(expr length ${SHORT_TIME})
		fi

		if [ "${SHORT_LEN}" == "5" ]; then
			# 日期精确到天
			LONG_TIME=$(LANG=en date --date=@$(expr ${SHORT_TIME} \* 3600 \* 24) "+%Y%m%d.%H%M")
		elif [ "${SHORT_LEN}" == "6" -o "${SHORT_LEN}" == "7" ]; then
			# 日期精确到小时
			LONG_TIME=$(LANG=en date --date=@$(expr ${SHORT_TIME} \* 3600) "+%Y%m%d.%H%M")
		fi
		if [ "${ISALL}" == "1" ]; then
			if [ "${SHORT_LEN}" == "5" -o "${SHORT_LEN}" == "6" -o "${SHORT_LEN}" == "7" ]; then
				echo "${SHORT_FLN}: ${XYZ_VERSION}-${LONG_TIME}"
			else
				echo "${SHORT_FLN}: ${XYZ_VERSION}"
			fi
		else
			echo "${SHORT_FLN}: ${XYZ_VERSION}"
		fi
	done
}

main

