#!/bin/bash

initialize=0

for i in $(seq 1 2 ${#@}); do
    case ${@:$i:1} in
        initialize) initialize=1 ;;
    esac
done

custom_randr() { default_randr "$@"; }

# ** load config
CPATH="${XDG_CONFIG_HOME:-$HOME/.config}/ferrydust"
if [ -f "$CPATH/ferryrandr.rc" ]; then
  source "$CPATH/ferryrandr.rc"
fi

# ** fix options missing in config
# 
[ -z $POSITION] && POSITION="left-of"
# maximum number of outputs the viceo card can handle
[ -z $MAX_ENABLED_OUTPUTS] && MAX_ENABLED_OUTPUTS=2
# all available outputs
[ -z $OUTPUTS] && OUTPUTS=$(xrandr |awk '$2 ~ /connected/ {print $1}')
# your notebook LVDS monitor
[ -z $DEFAULT_OUTPUT] && DEFAULT_OUTPUT=$(sed -ne 's/.*\(LVDS[^ ]*\).*/\1/p' <<< $OUTPUTS)
# remove default from list
OUTPUTS=${OUTPUTS/$DEFAULT_OUTPUT/}

# get info from xrandr
XRANDR=$(xrandr)
# number of actually connected outputs
NUM_CONNECTED=$(echo "$XRANDR" | grep " connected" | wc -l)

echo "$XRANDR" > /home/mark/fr.log
echo "------------" >> /home/mark/fr.log
echo "default output: $DEFAULT_OUTPUT" >> /home/mark/fr.log
echo "other outputs: $OUTPUTS" >> /home/mark/fr.log
echo "connected: $NUM_CONNECTED" >> /home/mark/fr.log
echo "max: $MAX_ENABLED_OUTPUTS" >> /home/mark/fr.log

default_randr() {
  # ** disable default when to many outputs are connected
  if [ $NUM_CONNECTED -gt $MAX_ENABLED_OUTPUTS ]; then
    echo "More than $MAX_ENABLED_OUTPUTS displays connected, disabling LVDS."
    xrandr --output $DEFAULT_OUTPUT --off
  else
    if [[ $XRANDR == *$DEFAULT_OUTPUT\ connected\ \(* ]]; then
      echo "Setting LVDS to auto"
      xrandr --output $DEFAULT_OUTPUT --auto
    fi
    LAST=$DEFAULT_OUTPUT
  fi

  # ** set everything but internal
  i=0
  for CURRENT in $OUTPUTS; do
    if [[ $XRANDR == *$CURRENT\ connected*  ]]; then
      if [ $i -lt $MAX_ENABLED_OUTPUTS ]; then
        if [[ $XRANDR == *$CURRENT\ connected\ \(* ]]; then
          echo "Setting $CURRENT to auto ($POSITION $LAST)"
          xrandr --output $CURRENT --auto --$POSITION $LAST
        fi
        LAST=$CURRENT
      fi
      let i++
    else
      echo "Disabling unconnected display $CURRENT"
      xrandr --output $CURRENT --off
    fi
  done
}


# Filter events that do not really alter anything
if [ "$2" == "Connected" ]; then
	if [ "$3" != "None" ]; then
		echo "Ignoring resolution change..."
		exit 0
	#elif [[ $XRANDR =~ .*$1\ connected.*\n.*\*\+  ]]; then
	elif [[ $XRANDR =~ .*$1\ connected.*\n(.*[0-9]+x[0-9]+\ +[0-9.]+.*\n)*.*[0-9]+x[0-9]+\ +[0-9.]+.*\*.*  ]]; then
		echo "Ignoring connection of already used Display..."
		exit 0
	fi
elif [ "$2" == "Disconnected" -a "$3" == "None" ]; then
	echo "Ignoring disconnectetion of unused Display..."
	exit 0
fi

# ** Filter repeating events (eg dock/undock with multiple displays)
lastrandrf="${CPATH}/lastrandr"
if [ -f "$lastrandrf" ]; then
    lastrandr=$(stat -c %Y $lastrandrf)
else
    lastrandrf=0
fi
touch $lastrandrf
now=$(date +%s)
let lastrandr+=2
if [ "$lastrandr" -gt "$now" ]; then
    echo "ignoring too fast repeated event."
    exit
fi

custom_randr
