#!/bin/bash

if [ $# -le 0 ] ; then
  echo "Usage: $(basename "$0") [on|off|only|above|right|left]"
  exit 1
fi

case "$1" in
  on)
    action="on"
    ;;
  only)
    action="only"
    ;;
  above)
    action="above"
    ;;
  right)
    action="right-of"
    ;;
  left)
    action="left-of"
    ;;
  clone)
    action="clone"
    ;;
  off)
    action="off"
    ;;
  *)
    echo "Bad action specified"
    exit 1
    ;;
esac

ext_mon=""

#local_mon=LVDS-1
local_mon="$(xrandr | sed -n -e 's/^\(LVDS-*[0-3]\|eDP-*[0-3]\) connected.*$/\1/p' | head -n 1 )"

ext_mon="$(xrandr | sed -n -e 's/^\(HDMI-*[0-3]\|DP-*[0-3]\) connected.*$/\1/p' | head -n 1 )"

if [ -z "$ext_mon" ] ; then
  ext_mon="$(xrandr | sed -n -e 's/^\(DP[0-9]-*[0-3]\) connected.*$/\1/p' | head -n 1 )"
fi

echo "ext_mon=$ext_mon local_mon=$local_mon"

if [ $action = "only" ] ; then
  if [ -n "$ext_mon" ] ; then
    #echo "Switching to external monitor \"$ext_mon\""
    #xrandr --output LVDS1 --off
    xrandr --output "$ext_mon" --auto --scale 1x1 --primary
    xrandr --output "$local_mon" --off --scale 1x1
  fi
  sleep 1
fi

if [ $action = "on" ] ; then
  if [ -n "$ext_mon" ] ; then
    #echo "Switching to external monitor \"$ext_mon\""
    #xrandr --output LVDS1 --off
    xrandr --output "$local_mon" --auto --scale 1x1
    xrandr --output "$ext_mon" --auto --scale 1x1 --primary
    #LVDS1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
    local_x=$( xrandr --current | sed -n "s/^$local_mon connected \([0-9]*\)x.*\$/\1/p" )
    local_y=$( xrandr --current | sed -n "s/^$local_mon connected [0-9]*x\([0-9]*\)[^0-9].*\$/\1/p" )
    ext_x=$( xrandr --current | sed -n "s/^$ext_mon connected \(primary \|\)\([0-9]*\)x.*\$/\2/p" )
    ext_y=$( xrandr --current | sed -n "s/^$ext_mon connected \(primary \|\)[0-9]*x\([0-9]*\)[^0-9].*\$/\2/p" )
    scale_x=$[$ext_x*100/$local_x]
    scale_y=$[$ext_y*100/$local_y]
    if false ; then
      if [ $scale_x -le $scale_y ] ; then
        scale_x=$scale_y
      else
        scale_y=$scale_x
      fi
    fi
    #echo "xrandr --output \"$local_mon\" --same-as \"$ext_mon\"  --scale \"$[$scale_x/100].$[$scale_x%100]x$[$scale_y/100].$[$scale_y%100]\""
    xrandr --output "$local_mon" --same-as "$ext_mon"  --scale "$[$scale_x/100].$[$scale_x%100]x$[$scale_y/100].$[$scale_y%100]"
    #xrandr --output LVDS1 --off
    sleep 4
  fi
fi

if [ $action = "above" -o $action = "right-of" -o $action = "left-of" ] ; then
  if [ -n "$ext_mon" ] ; then
    #echo "Switching to external monitor \"$ext_mon\""
    #xrandr --output LVDS1 --off
    xrandr --output "$local_mon" --auto --scale 1x1 --primary
    xrandr --output "$ext_mon" --auto --"$action" "$local_mon"
    sleep 4
  fi
fi

if [ $action = "clone" ] ; then
  if [ -n "$ext_mon" ] ; then
    xrandr --output "$local_mon" --auto --scale 1x1 --primary
    xrandr --output "$ext_mon" --auto --scale 1x1
    local_x=$( xrandr --current | sed -n "s/^$local_mon connected \(primary \|\)\([0-9]*\)x.*\$/\2/p" )
    local_y=$( xrandr --current | sed -n "s/^$local_mon connected \(primary \|\)[0-9]*x\([0-9]*\)[^0-9].*\$/\2/p" )
    ext_x=$( xrandr --current | sed -n "s/^$ext_mon connected \(primary \|\)\([0-9]*\)x.*\$/\2/p" )
    ext_y=$( xrandr --current | sed -n "s/^$ext_mon connected \(primary \|\)[0-9]*x\([0-9]*\)[^0-9].*\$/\2/p" )
    scale_x=$[$local_x*100/$ext_x]
    scale_y=$[$local_y*100/$ext_y]
    if false ; then
      if [ $scale_x -le $scale_y ] ; then
        scale_x=$scale_y
      else
        scale_y=$scale_x
      fi
    fi
    xrandr --output "$ext_mon" --same-as "$local_mon" --scale "$[$scale_x/100].$[$scale_x%100]x$[$scale_y/100].$[$scale_y%100]"
    sleep 4
  fi
fi

if [ $action = "off" ] ; then
  sleep 1
  xrandr --output "$local_mon" --auto --primary --scale 1x1
  if [ -n "$ext_mon" ] ; then
    #echo "Switching back to \"$local_mon\""
    xrandr --output "$ext_mon" --off
  fi
  xrandr --output "$local_mon" --auto --primary --scale 1x1
  for out_dis in $(xrandr --current | sed -n 's/^\([^ ]*\) disconnected.*$/\1/p') ; do
    xrandr --output "$out_dis" --off
  done
  sleep 1
else
  if [ -e /usr/bin/xsetwacom ] ; then
    xsetwacom --list | sed -n -e 's/^[^:]*id: \([0-9]\+\)[ \t].*$/\1/p' | \
    while read wac_dev ; do 
      xsetwacom set "$wac_dev" MapToOutput "$ext_mon"
    done
  fi
fi
