#!/bin/bash
# compatibilityChecker - Sutton project OEM platform compatibility checker
# Copyright (C) 2013 Penk Chen <penk.chen@canonical.com> 
# Copyright (C) 2014 Bin Li <bin.li@canonical.com> 
# 
# Usage: compatibilityChecker [ -r ] [ -p ] [ -s ]
#       -r      print release status: Release Version | Development Version | Unsupported Hardware
#       -p      print platform name
#       -s      print system ID

export LANG=C

BASEDIR=$(dirname "$0")
if [ "$BASEDIR" = "/usr/bin" ]; then
	BASEDIR=/usr/share/sutton
else
	BASEDIR=${BASEDIR}/../lib
fi
. $BASEDIR/device.db
. $BASEDIR/config.sh

bios_v3=$(get_bios_version 2>/dev/null)
id=$(get_devices | compose_cid 2>/dev/null)
igpu=$(get_devices | filter_igpu_devices | compose_cid 2>/dev/null)
dgpu=$(get_devices | filter_dgpu_devices | compose_cid 2>/dev/null)
netdevices=$(get_devices | filter_net_devices 2>/dev/null)
platform="$(get_platform 2>/dev/null)"
image="$(get_image_name 2>/dev/null)"
buildstamp="$(get_image_stamp 2>/dev/null)"

output="Uncertified Hardware" 

usage()
{
	cat >&2 <<EOF
Usage: $0 [ -r ] [ -p ]
	-r 	print release status: Release Version | Development Version | Uncertified Hardware
	-p 	print platform name
	-s 	print system ID 
EOF
	exit 1
}

Q()
{
	[ -f $BASEDIR/config.db ] || exit

	sqlite3 $BASEDIR/config.db "$@"
}

while getopts ":rsp" o; do
	case "${o}" in
		r) ;; # ignored
		p) echo $(get_platform); exit ;;
		s) echo $id; exit ;;
		*) usage ;;
	esac
done

if [ -f /etc/buildstamp ]; then
	lines=$(Q "SELECT id,bios_version,gmdate,igpu,dgpu,gmimage,product_version FROM configs
		WHERE gmimage='$image'
		AND bios_version='$bios_v3';")
	num=$(echo "$lines" | wc -l)
	if [ $num -gt 1 ]; then
		#echo "There are two configs with same BIOS Version"
		# We need filter the correct one with platform name
		line_1=$(echo "$lines" | grep -i "$platform")
		if [ -n "$line_1" ]; then
			lines=$line_1
		else
			# When can't get correct platform name, just use the first one.
			lines=$(echo "$lines" | head -n1)
		fi
	fi
	if [ -n "$lines" ]; then

		gmdate=$(echo "$lines" | cut -d '|' -f3)

		if [ -n "$gmdate" ] && [ -n "$buildstamp" ]; then
			epoch="$(date --date="$gmdate -7 day" +%s)"
			buildstamp_epoch="$(date --date="$buildstamp" +%s)"
	
			if [ "$buildstamp_epoch" -gt "$epoch" ]; then
				output="Release Version"
			else
				output="Development Version" # not reaching GM yet
			fi
		fi
		igpu_d=$(echo "$lines" | cut -d '|' -f4)
		# When Walter3 swith from Hybrid to discrete, there are only 1 iGPU.
		if [[ ${igpu_d/${igpu}//} == $igpu_d ]] ; then
			output="$output \n(with uncertified igpu: ${igpu})"
		fi
		dgpu_d=$(echo "$lines" | cut -d '|' -f5)
		if [ "$dgpu" != "$dgpu_d" ] ; then
			output="$output \n(with uncertified dgpu: ${dgpu})"
		fi
	else
		output="$output (with uncertified BIOS: $bios_v3)"
	fi
fi

unknown_net_devices="$(get_devices | filter_net_devices \
	| filter_unknown_devices | paste -sd ' ')"
if [ -n "$unknown_net_devices" ]; then
	output="$output \n(with uncertified net devices: ${unknown_net_devices})"
fi

echo "$output"
