#!/bin/bash

# mintfb-randr created by Shane Joe Lazar <shane@archlinux.us>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA

set -e

####################
#VARIABLES
#-------------------
RAWCURRENT=""
#-------------------
#/VARIABLES
####################

####################
#FUNCTIONS
#-------------------

parameterCheck () {
if [ -z $@ ]
then
	usage
	exit 1
fi
}

preConfig () {
mkdir -p $HOME/.linuxmint
touch $HOME/.linuxmint/mintfb-xrandr
}

getCurrent () {
RAWCURRENT=`xrandr`

echo "$RAWCURRENT" | grep -n "Screen" | while read line
do
	screennum=`echo $line | cut -d " " -f2 | cut -d ":" -f1`
	res1=`echo $line | cut -d " " -f8`
	res2=`echo $line | cut -d " " -f10 | cut -d "," -f1`
	res=`echo "$res1"x"$res2"`
	echo "xrandr --screen $screennum -s $res ; "
done
echo "fbsetbg -l"
}

save () {
	rm $HOME/.linuxmint/mintfb-xrandr
	echo "#!/bin/bash" > $HOME/.linuxmint/mintfb-xrandr
	getCurrent >> $HOME/.linuxmint/mintfb-xrandr
	chmod +x $HOME/.linuxmint/mintfb-xrandr
	fbsetbg -l
}

restore () {
	sh $HOME/.linuxmint/mintfb-xrandr &
}

config () {
	lxrandr
	save
}

usage () {
	echo "mintfb-randr"
	echo ""
	echo "------------------------------------------------"
	echo ""
	echo "Author: Shane Joe Lazar <shane@archlinux.us>"
	echo "Version: 0.2"
	echo "License: GPL v3 or newer"
	echo ""
	echo "------------------------------------------------"
	echo ""
	echo "This is a script written for Linux Mint Fluxbox CE 8. It enables configuration, saving and restoring of screen resolutions using the lxrandr and xrandr tools. It also uses the fbsetbg command to (re)load the last desktop background."
	echo ""
	echo "------------------------------------------------"
	echo ""
	echo "Usage:"
	echo "mintfb-randr [option]"
	echo ""
	echo "Available options are:"
	echo ""
	echo "save : This saves the current xrandr configuration to the file $HOME/.linuxmint/mintfb-xrandr"
	echo ""
	echo "config : This runs the lxrandr tool and saves the chosen setting to $HOME/.linuxmint/mintfb-xrandr"
	echo ""
	echo "restore : This restores the xrandr settings from the file $HOME/.linuxmint/mintfb-xrandr"
	echo ""
	echo "------------------------------------------------"
}

#-------------------
#/FUNCTIONS
####################

####################
#RUN
#-------------------

parameterCheck $@

set -u

preConfig

case $1 in
"save")
	save
;;
"config")
	config
;;
"restore")
	restore
;;
*)
	usage
esac

#-------------------
#/RUN
####################
