#!/bin/bash

COM_DIR=/usr/local/ctyun/clink/Mirror/Registry/
version=0

os=`cat /etc/os-release |grep -w "NAME" |cut -d"=" -f2 |sed 's/"//g'`
ver=`cat /etc/os-release |grep -w "VERSION_ID" |cut -d"=" -f2 |sed 's/"//g'`
dist_id=''
os_info=''

if [[ $os == "uos" ]];then
	os_info=$(cat /etc/os-version |grep "SystemName" |cut -d" " -f5|cut -d"=" -f2)
	os_info=`printf %s $os_info`
	dist_id=$(grep MinorVersion /etc/os-version |cut -d "=" -f2)
elif [[ $os == "Kylin" ]];then
	os_info=`cat /etc/os-release |grep -w VERSION|cut -d"=" -f2`
	dist_id=$(grep KYLIN_RELEASE_ID /etc/os-release |cut -d "=" -f2 |sed 's/"//g')
elif [[ $os == "Ubuntu" ]];then
	os_info=`cat /etc/os-release |grep -w VERSION|cut -d"=" -f2`
	dist_id=$(grep VERSION_ID /etc/os-release |cut -d "=" -f2|sed 's/"//g')
	#os_info=`echo $os_info |cut -d"=" -f2`
else
	#echo "no match os:$os"
	os_info=$os
fi

ARCH=$(arch || uname -m)
if [ ${ARCH}x == "x86_64"x ] ; then
    ARCH="amd64"
elif [ ${ARCH}x == "aarch64"x ]; then
    ARCH="arm64"
fi

echo "+------------------------------------------------------------+"
if [ "$os_info" != "$os" ];then
	printf "%15s%s\n" "" "操作系统 ：$os_info ($os)"
else
	printf "%15s%s\n" "" "操作系统 ：$os_info"
fi

if [ [$dist_id != ""] -a [$dist_id != $ver] ];then
	printf "%15s%s\n" "" "系统版本 ：$ver ($dist_id)"
else
	printf "%15s%s\n" "" "系统版本 ：$ver"
fi
printf "%15s%s\n" "" "系统架构 ：$ARCH"

echo "+------------------------------------------------------------+"
printf '|%24s | %-14s | %-28s|\n' "组件名称" "组件版本" "更新时间"
echo "+------------------------------------------------------------+"
for folder in `ls -t $COM_DIR`
do
	#echo $folder
	if [ $folder == "Default" ]
	then	
		continue
	fi
	if [ $folder == "NULL" ]
	then
		rm -rf $COM_DIR/NULL
	fi
	if [ -f $COM_DIR$folder ]
	then
		for line in `cat $COM_DIR$folder`
		do
			if [[ $line =~ "VersionCode" ]]
			then
				version=${line#*VersionCode=}				
				break
			else
				continue
			fi
		done
		mod_time=$(stat -c %y $COM_DIR$folder |cut -d"." -f1)
		#echo $folder=${line#*VersionCode=}
		printf '|%20s | %-10s | %-24s|\n' $folder $version "$mod_time"
	fi
done
echo "+------------------------------------------------------------+"
