#!/bin/bash
if [ $# != 0 ] ; then
  # parameters not properly implemented yet - it fails and puts up a menu on the Vectrex
  if [ ! -f /opt/pitrex/roms/vectrex/$1.BIN ] ; then
    echo "vecx: file /opt/pitrex/roms/vectrex/$1.BIN does not exist. (current directory: `pwd`)"
    exit 1
  fi
fi
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
}

echo -n performance | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null
sudo tvservice --off
if [ $# != 0 ] ; then
    echo "WARNING: at the moment, vecx.direct does not accept a rom file as a parameter"
    sudo ./vecx /opt/pitrex/roms/vectrex/$1.BIN
else
    # choose the game from the menu...
    sudo ./vecx
fi
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
