#!/bin/sh
#BK video-wizard (c) Copyright 2007 Barry Kauler
#2007 Lesser GPL licence v2 (see: /usr/share/doc/legal)
#written for Puppy v3.00
#bugfix v3.02
#v404 flag to rebuild drive icons (see /sbin/clean_desk_icons).
#w019 apr2009: updated for woof.
#101020 no longer have /etc/modprobe.conf.
#120202 rodin.s: internationalized.
#120213 /var/local/pup_event_icon_change_flag path changed from /tmp (see /sbin/clean_desk_icons)

export TEXTDOMAIN=video-wizard
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
export LANGORG=$LANG

. /etc/rc.d/PUPSTATE

CURRENTX="`readlink /usr/bin/X`"
if [ "$CURRENTX" != "Xvesa" ];then
 Xdialog --title "$(gettext 'Xvesa Video Wizard')" --msgbox "$(gettext 'The Xvesa X server is not currently running.
Select the Xorg Video Wizard from the menu 
-- it has the option of switching over to the
Xvesa server. Click OK button to exit...')" 0 0
 exit
fi

ALLMODES="`Xvesa -listmodes 2>&1 | grep '^0x' | tr ':' ' ' | tr -s ' ' | cut -f 1,2 -d ' ' | sort -g -k 2 | tr ' ' '|' | tr '\n' ' '`"

#v3.02 verify correct mode-number for 640x480x16... (0x0111)
MODEBASENUM="`echo "$ALLMODES" | tr ' ' '\n' | grep '640x480x16' | cut -f 1 -d '|'`"

if [ -e /tmp/videomode ];then
 CURRENTMODE="`cat /tmp/videomode`"
 #in case new mode no good, ctrl-alt-backspace will come up in safe mode...
 [ "$MODEBASENUM" ] && echo -n "$MODEBASENUM 640x480x16" > /tmp/videomode #v3.02
else
 CURRENTMODE="`cat /etc/videomode`"
fi
echo "$CURRENTMODE" > /tmp/xvesacurrentmode #v3.02
CURRENTNUMBER="`echo -n "$CURRENTMODE" | cut -f 1 -d ' '`"
CURRENTVALS="`echo -n "$CURRENTMODE" | cut -f 2 -d ' '`"

[ -f /etc/xextraoptions ] && CURRENTXOPTIONS="`cat /etc/xextraoptions`"

RADIOSECTION=""
for ONEMODE in $ALLMODES
do
 MODENUMBER="`echo -n "$ONEMODE" | cut -f 1 -d '|'`"
 MODEVALS="`echo -n "$ONEMODE" | cut -f 2 -d '|'`"
 XVAL=`echo -n "$MODEVALS" | cut -f 1 -d 'x'`
 YVAL=`echo -n "$MODEVALS" | cut -f 2 -d 'x'`
 COLDEPTH=`echo -n "$MODEVALS" | cut -f 3 -d 'x'`
 [ $XVAL -lt 640 ] && continue
 [ $YVAL -lt 480 ] && continue
 [ $COLDEPTH -lt 16 ] && continue
 RADIOSTATE="false"
 [ "$CURRENTNUMBER" = "$MODENUMBER" ] && RADIOSTATE="true"
 RADIOSECTION="${RADIOSECTION}
    <radiobutton active=\"${RADIOSTATE}\">
      <label>${MODEVALS}</label>
      <variable>RADIO_${ONEMODE}</variable>
    </radiobutton>
"
done

# icon-name=\"gtk-preferences\"
export MAIN_DIALOG="
<window title=\"$(gettext 'Xvesa Video Wizard')\">
<hbox>
 <vbox>
  <frame $(gettext 'Video modes')>
   ${RADIOSECTION}
  </frame> 
 </vbox>
 <vbox>
  <text>
    <label>\" \"</label>
  </text>
  <text>
    <label>$(gettext 'Welcome to the Xvesa Video Wizard!')</label>
  </text>
  <text>
    <label>`eval_gettext \"The current video mode is \\\${CURRENTVALS}. If you want something else, make a choice on the left then click the 'CHANGE' button. If you are happy with the current screen resolution, click the 'OKAY' button.\"`</label>
  </text>
  <text>
    <label>$(gettext 'NOTE: x16 is 16-bit color which is 65,536 colors (HighColor), x24 is 24-bit color which is 16 million colors (TrueColor).')</label>
  </text>
  <text>
    <label>$(gettext 'IMPORTANT: if a mode does not work, press CTRL-ALT-BACKSPACE (hold down CTRL and ALT keys and tap BACKSPACE key). Please remember this key combination. It is the standard Linux technique for emergency shutdown of the X graphics server.')</label>
  </text>
  <text>
    <label>\" \"</label>
  </text>
  
  <hbox>
   <vbox>
    <text>
     <label>$(gettext 'For the experts, X server startup options:')</label>
    </text>
    <entry>
     <default>\"${CURRENTXOPTIONS}\"</default>
     <variable>ENTRY</variable>
    </entry>
   </vbox>
   <text>
    <label>\"    \"</label>
   </text>
   
    
    <button>
      <label>$(gettext 'CHANGE')</label>
      <action type=\"exit\">TESTBUTTON</action>
    </button>
    <button>
      <label>$(gettext 'OKAY')</label>
      <action type=\"exit\">OKAYBUTTON</action>
    </button>
    
   
  </hbox>
 </vbox>
</hbox>
</window>
"

RETVALS="`gtkdialog3 --program=MAIN_DIALOG`"

echo "$RETVALS"

[ "`echo "$RETVALS" | grep '^EXIT='`" = "" ] && exit #invalid stuff.
[ "`echo "$RETVALS" | grep 'abort'`" != "" ] && exit #invalid stuff.

CHOSENMODE="`echo "$RETVALS" | grep 'RADIO' | grep '"true"' | cut -f 2 -d '_' | cut -f 1 -d '=' | tr '|' ' '`"
CHOSENBUTTON="`echo "$RETVALS" | grep '^EXIT' | cut -f 2 -d '"'`" #'geany
CHOSENXOPTIONS="`echo "$RETVALS" | grep '^ENTRY' | cut -f 2 -d '"'`" #'geany

echo "CHOSENMODE=$CHOSENMODE CHOSENBUTON=$CHOSENBUTTON"

echo -n "$CHOSENMODE"  > /tmp/videomode
echo -n "$CHOSENXOPTIONS" > /etc/xextraoptions

if [ "$CHOSENBUTTON" = "TESTBUTTON" ];then
 echo 'ICONWIPE' > /var/local/pup_event_icon_change_flag #v404 see /sbin/clean_desk_icons (called from .xinitrc). 120213 path changed from /tmp (see /sbin/clean_desk_icons)
 rm -f /tmp/Xflag #prevent endless restarts. see /usr/bin/xwin.
 exec killall X #restart X in new test mode.
fi

if [ "$CHOSENBUTTON" = "OKAYBUTTON" ];then
 echo -n "$CHOSENMODE" > /etc/videomode
 rm -f /tmp/videomode
 rm -f /tmp/Xflag #prevent endless restarts. see xwin*.
fi

if [ $PUPMODE -eq 5 ];then
 if [ ! "`grep 'alias snd\-card\-0' /etc/modprobe.d/alsa.conf`" = "" ];then #101020
  wavplay /usr/share/audio/2barks.wav &
  sleep 3
  killall wavplay 2> /dev/null
 fi
fi

###END###
