#!/usr/bin/env bash
# compatibilityChecker - Stella project OEM platform compatibility checker
# Copyright (C) 2013 Penk Chen <penk.chen@canonical.com> 
#                    Tim Chen <tim.chen119@canonical.com>
# 
# Usage: compatibilityChecker [ -r ] [ -p ] [ -s ] [ -l ] [ -g PLATFORMID ] [ -a ]
#       -r      print release status: Release Version | Development Version | Unsupported Hardware
#       -p      print platform name
#       -s      print system ID
#       -l      print all platform name
#       -g      print ppa group based on PLATFORMID
#       -a      print all ppa groups

. /usr/share/volatile/platform-compatibility-list

usage() { echo "Usage: $0 [ -r ] [ -p ] [ -l ] [ -g PLATFORMID ] [ -a ]
	-r 	print release status: Release Version | Development Version | Unsupported Hardware
	-p 	print platform name
	-s 	print system ID 
    -l  print all platform name
    -g  print ppa group based on PLATFORMID
    -a  print all ppa groups
" 1>&2; exit 1; }

set -x

ID=$(get-platform-info -S)
OPTION="release"
while getopts ":rsplga" o; do
	case "${o}" in
	r) OPTION="release";
	;;
	p) echo ${platformLookupTable[$ID]} | cut -d':' -f1;
	exit;
	;;
	s) echo $ID;
	exit;
	;;
    l) echo ${platformLookupTable[@]} | tr ' ' '\n' | cut -d':' -f1 | sort -ub | sed '/^$/d'
    exit;
    ;;
    g) echo ${platformLookupTable[$2]} | cut -d':' -f3;
    exit;
    ;;
    a) echo ${platformLookupTable[@]} | tr ' ' '\n' | cut -d':' -f3 | sort -ub | sed '/^$/d'
    exit;
    ;;
	*) usage
	;;
	esac
done
shift $((OPTIND-1))

OUTPUT=`echo ${platformLookupTable[$ID]} | cut -d':' -f1`

if [ $OPTION == "release" ]; then
    if [ ! -z $OUTPUT ]; then
        # Got the system ID, checking for GM date 
        GMDate=`echo ${platformLookupTable[$ID]} | cut -d':' -f2`

        if [ ! -z $GMDate ]; then
            GMDateEpoch=`date --date=$GMDate +%s`
            OneWeekBeforeGMDateEpoch=`date --date="$GMDate -10 day" +%s`
            BuildDate=`tail -n1 /etc/buildstamp | rev | cut -d'-' -f2 | rev`
            BuildDateEpoch=`date --date=$BuildDate +%s`

            if [ $BuildDateEpoch -gt $OneWeekBeforeGMDateEpoch ]; then
                OUTPUT="Release Version"
            else
                OUTPUT="Development Version" # not reaching GM yet
            fi
        else
            OUTPUT="Development Version" # GM date not available
        fi 
    else 
        OUTPUT="Unsupported Hardware" 
    fi  
fi  
echo $OUTPUT
