#!/bin/bash
trap ctrl_c INT

function ctrl_c() {
    echo "** Trapped CTRL-C"
    # state 0xa [HDMI CEA (16) RGB lim 16:9], 1920x1080 @ 60.00Hz, progressive
    #/boot/config.txt:
    # #hdmi_safe=1
    # # uncomment if hdmi display is not detected and composite is being output
    # hdmi_force_hotplug=1
    # hdmi_group=1
    # hdmi_mode=16
    # #hdmi_drive=2
    # #config_hdmi_boost=4
    sudo tvservice -p
    sudo chvt 2                      # change to different virtual terminal
    sudo chvt 1                      # change back to the previous virtual terminal
    # need to do this automatically from saved tvservice output:
    fbset -g 1920 1080 1920 1080 32  # adjust the size of the framebuffer
    echo -n ondemand | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null
}



SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"


echo -n performance | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null
sudo tvservice --off
( cd /boot ; sudo ${DIR}/tempest )
sudo tvservice -p
sudo chvt 2                      # change to different virtual terminal
sudo chvt 1                      # change back to the previous virtual terminal
# need to do this automatically from saved tvservice output:
fbset -g 1920 1080 1920 1080 32  # adjust the size of the framebuffer
echo -n ondemand | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null
