#!/bin/bash

# xrandr wrapper for FVWM-Crystal
# Dominique Michel <dominique_libre@users.sourceforge.net> 2013
# Released under the GNU/GPL license version 3 or any later version

LC_ALL=C

CURRENT_MODE=$( xrandr | grep '[0-9]\*' | sed -e 's:\(^ *\)\([0-9]*x[0-9]*\).*:\2:' )

ALL_MODES=( $( xrandr | grep -E "^ *[0-9]" | sed -e 's:\(^ *\)\([0-9]*x[0-9]*\).*:\2:' | sort -grs ) )

index=${#ALL_MODES[@]}
i=0

while [ "$i" -lt "$index" ]
do
    if [ "${CURRENT_MODE}" == "${ALL_MODES[i]}" ]
    then
	m=$((i-1))
	if [ "$m" -eq "-1" ]
	then
		m=$((${index}-1))
	fi
	NEXT_MODE="${ALL_MODES[m]}"
    fi
    let "i+=1"
done
xrandr -s "${NEXT_MODE}"
