#!/usr/bin/env bash
#
################################################################################
# BOARD SCRIPT, NOT FOR STANDALONE USE
################################################################################
#
# kyoungmeyer@emacinc.com
# December 9, 2013
#
# Copyright (C) 2013, EMAC, Inc.
# All rights reserved.      
#

# Constants
Version="1.0.1"
IMAGE_DIR=/root/.splash_image/
MOUNT_FS="/usr/bin/oemntrw"
version () { 
	echo "$(basename $0) (EMAC OE Automation Tools) Version $Version"
	echo "Copyright (C) 2013-2014 EMAC, Inc.  All rights reserved."; 
}
help () {
    echo
    echo "This is a board script that depends on another script to work"
    echo "properly, please do not use as a standalone script"
    echo
    echo "Options:"
    echo "  -v      Display version info and exit"
    echo "  -h      Display this help and exit"
    echo
    version
    echo
    echo "Copyright (C) 2013-2014, EMAC, Inc."
    echo "All rights reserved."
    exit 0
}
board_script () {
    if [[ -f $IMAGE_DIR/resolution.txt ]]; then
        rm -f $IMAGE_DIR/resolution.txt
    fi
    
    if [[ ! -e /dev/fb0 ]]; then
        hash xrandr 2>/dev/null || { echo >&2 "Error: xrandr not found";  exit 10; }

        xrandr --current  | grep '*' | uniq | egrep -o '[0-9]{3,4}+x[0-9]{3,4}+' > $IMAGE_DIR/resolution.txt
    else
        hash fbset 2>/dev/null || { echo >&2 "Error: fbset not found";  exit 11; }

        fbset | grep 'mode "' | egrep -o '[0-9]{3,4}+x[0-9]{3,4}+' > $IMAGE_DIR/resolution.txt
    fi
}
while getopts ":vh" opt; do
  case $opt in
    v   ) version; exit 0;;
    h   ) help; exit 0;;
    \?  ) echo "Invalid option: -$OPTARG" >&2; exit 1;;
  esac
done

$MOUNT_FS
board_script
exit 0
