#!/bin/sh
mode=1024x768
while getopts m:l i; do
  case $i in
    m)	mode=$OPTARG ;;
    l)	lvdsmode=1 ;;
  esac
done
shift $((OPTIND - 1))

vdevs=`xrandr | grep -wi 'connected' | awk '{print $1}'`
vdevs=`echo $vdevs`
count=`echo $vdevs | wc -w`
lvds=${vdevs%% *}
vga=${vdevs##* }
if [ -n "$lvdsmode" -o $count -lt 2 ]; then
  mode=`xrandr|grep +|grep -v conn|head -1|awk '{print $1}'`
  if [ x"$lvds" = x"$vga" ]; then
    vga=`xrandr|grep 'disconnected .*[0-9]x[1-9]'|head -1|awk '{print $1}'`
  fi
  xrandr --output $vga --off
  xrandr --output $lvds --mode $mode
  exit 0
fi

xrandr --output $vga --mode $mode
xrandr --output $lvds --mode $mode
xrandr --output $vga --same-as $lvds
