#!/bin/sh
#(c) Copyright Barry Kauler 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v3.99 BK: fix for new version of xrandr, plus some enhancement.

#note, /etc/xrandrindex is read by /root/.xinitrc when X is started.

XRANDR="`xrandr -q`"

#v3.99
#RESCHOICES="`echo "$XRANDR" | tr '*' ' ' | tr -s " " | grep "^ [0-9]" | cut -f 1-5 -d " " | sed -e 's/ x /x/g'`"
RESCHOICES="`echo "$XRANDR" | tr -s " " | grep "^ [0-9]" | cut -f 2,3 -d " " | tr ' ' '@' | tr '\n' ' '`"

#SELECTIONS="`echo "$RESCHOICES" | tr "\n" "X" | sed -e 's/X/ off /g'`"
XYCURRENT='800x600' #precaution.
for ONECHOICE in $RESCHOICES
do
 XYRES="`echo -n "$ONECHOICE" | cut -f 1,2 -d 'x'`"
 if [ "`echo "$ONECHOICE" | grep '\*'`" != "" ];then
  SELECTIONS="${SELECTIONS} ${XYRES} ${ONECHOICE} on"
  XYCURRENT="$XYRES"
 else
  SELECTIONS="${SELECTIONS} ${XYRES} ${ONECHOICE} off"
 fi
done

#echo "$SELECTIONS"

TESTINDEX="`Xdialog --left --title "Xorg Resolution Changer" --stdout --no-tags --radiolist "Please choose a resolution to test.\n\nNote: After testing a resolution, you\nwill be given the opportunity to make\nit permanent if you wish.\n\nIF THE NEW RESOLUTION DOES NOT\nWORK, WAIT 60 SECONDS OR PRESS\nCTRL-ALT-BACKSPACE" 0 0 8 $SELECTIONS 2> /dev/null `"

if [ ! $? -eq 0 ];then
 #made no choice.
 exit
fi 

xrandr -s $TESTINDEX

XRANDR="`xrandr -q`"

TESTLINE="`echo "$XRANDR" | tr -s " " | grep "^ [0-9]" | grep '\*'`"
TESTX="`echo -n "$TESTLINE" | cut -f 2 -d " " | cut -f 1 -d 'x'`"
TESTY="`echo -n "$TESTLINE" | cut -f 2 -d " " | cut -f 2 -d 'x'`"
TESTVR="`echo -n "$TESTLINE" | cut -f 3 -d ' ' | cut -f 1 -d '*'`"

xmessage -bg "violet" -center -timeout 60 -title "Xorg Resolution Changer" -buttons Permanent:12,Session:11,RESTORE_DEFAULT:10  "X is now running at a resolution of ${TESTX}x${TESTY}
at a vertical refresh frequency of ${TESTVR} Hz.

Note1:
some window managers, such as JWM, do not work properly after a resolution
change, however they do work properly after the w.m. is restarted with the
new resolution (see Shutdown menu).

Note2:
It is NOT recommended to choose 'Permanent' here, as this does not really
change the default resolution, instead it switches resolutions after X has
started, which causes a delay and flicker at startup and may have other
undesirable side-effects. Instead, run the Video Wizard to choose a
permanent resolution.

Do you want to make this permanent? (not recommended)...
Or, just keep this resolution for current session?...
Or, go back to default resolution?...
IF YOU DO NOT CHOOSE IN 60 SECONDS, X WILL RESTORE TO DEFAULT RESOLUTION"

RETVAL=$?
if [ $RETVAL -lt 11 ];then
 rm -f /etc/xrandrindex
 xrandr -s $XYCURRENT #restore to what it was before.
 exit
fi
if [ $RETVAL -eq 11 ];then #keep current session only.
 rm -f /etc/xrandrindex
 exit
fi

echo -n "${TESTX}x${TESTY}" > /etc/xrandrindex

Xdialog --title "Xorg Resolution Changer" --msgbox "The next time you boot Puppy, X will start\nwith a screen resolution of ${TESTX}x${TESTY}\nNote, it is recommened to use the X Video Wizard to choose resolution, not this method." 0 0

###END###
