#! /bin/bash
FILTER=/usr/share/rotation/winidfilter
if [ ! -f $FILTER ]; then
mkdir -p /usr/share/rotation
cat >> $FILTER << EOF
has no name
window
child
\/
applet
ROX
rox
Panel
panel
ScreenCap
Active tasks
Battery Monitor
SD Monitor
Screen Blanker
Volume Control
Wireless Monitor
Clock
X Virtual Keyboard
App Launcher
SCIM
scim
xmms
XMMS
glipper
GLIPPER
Phone Manager
Bluetooth File Sharing
gnome-phone-manager
gnome-obex-server
32x32
EOF
fi

# See how we were called.
  
closed() {
    if [ "`pidof xmms`" != "" ]; then
      xraise XMMS
    fi
    touch /tmp/norot
    /usr/X11R6/bin/xset s blank
    /usr/X11R6/bin/xset s activate
    return 0
}

landscape() {
    if [ "`xrandr -q|grep rotation|cut -d" " -f4`" = "normal" ]; then
      exit 0
    fi
    /usr/X11R6/bin/xrandr -o normal

    #if [ "`pidof xscreensaver`" != "" ]; then
    #  sudo -u zaurus xscreensaver-command -deactivate
    #fi

    if [ "`pidof xvkbd`" != "" ]; then
      killall xvkbd
    fi

    if [ ! -f /tmp/norot ] && [ ! -f /tmp/nores ]; then
      if [ "`pidof ROX-Filer`" != "" ] && [ `which rox-bset` != "" ] && [ "`grep ROX /home/root/Choices/pdaxii13.conf|grep true`" != "" ]; then
        /usr/bin/rox-bset --landscape
      fi

      for APP in `xwininfo -root -tree|grep -v -f $FILTER|cut -d\" -f2|grep [a-z]|sed -e "s/ /#/g"`
      do
        APP=`echo $APP|sed -e "s/#/ /g"`
        if [ "`echo $APP|grep 0x`" = "" ]; then
          #sleep 1
          #echo "processing $APP..." >> /home/root/rotate.landscape
          xresizewindow "$APP" "638x420+0+0"
        fi
      done
    
      if [ "`pidof torsmo`" != "" ]; then
        killall torsmo
        /usr/local/bin/torsmo -d &
      fi
      if [ "`pidof icewm`" != "" ]; then 
        /usr/bin/icetbfix
      fi
    else
      rm /tmp/norot 2>/dev/null
      echo landscape >> /tmp/nores
    fi

    MODEL=`cat /proc/deviceinfo/product`
    case "$MODEL" in
	SL-C860|SL-C760) 
	    echo "100" >/proc/driver/w100/fastsysclk
	    ;;
	SL-C700)
	    echo "75" >/proc/driver/w100/fastsysclk
	    ;;
	*)
	    ;;
    esac
    
    return 0
}	

portrait() {
    /usr/X11R6/bin/xrandr -o left

    if [ ! -f /tmp/norot ]; then
      if [ "`pidof ROX-Filer`" != "" ] && [ `which rox-bset` != "" ]; then
        /usr/bin/rox-bset --portrait
      fi

      for APP in `xwininfo -root -tree|grep -v -f $FILTER|cut -d\" -f2|grep [a-z]|sed -e "s/ /#/g"`
      do
        APP=`echo $APP|sed -e "s/#/ /g"`
        if [ "`echo $APP|grep 0x`" = "" ]; then
          #sleep 1
          #echo "processing $APP..." >> /home/root/rotate.portrait
          xresizewindow "$APP" "478x586+0+0"
        fi 
      done

      if [ "`pidof torsmo`" != "" ]; then
        killall torsmo
        /usr/local/bin/torsmo -d &
      fi

      if [ -z `ps -aux|grep xvkbd|grep -v grep` ]; then
        xvkbd -geometry 480x175-4+424 -no-keypad -modal &
      fi

      rm /tmp/nores 2>/dev/null
    fi

    MODEL=`cat /proc/deviceinfo/product`
    case "$MODEL" in
	SL-C860) 
	    echo "75" >/proc/driver/w100/fastsysclk
	    ;;
	SL-C700)
	    echo "75" >/proc/driver/w100/fastsysclk
	    ;;
	*)
	    ;;
    esac

    return 0
}  


case "$1" in
  closed)
  	closed
	;;
  landscape)
  	landscape
	;;
  portrait)
  	portrait
  	;;	
  *)
	echo "Usage: $0 {closed|landscape|portrait}"
	exit 1
esac

exit $?
