#!/bin/sh

# display mode
mode=$1

. /usr/lib/smartclient/sc_hardware.sh
sc_hardware_init

# regex to find the primary (internal) display
MONITOR_PRIMARY_REGEX="LVDS.*"

if ! isLaptopPC; then
    pcType=`get_pcType`
    printf "this system ($pcType) is not defined as a laptop. exiting\n"
    exit 0
fi

# get connected monitors
MONITORS_CONNECTED=`LANG=C xrandr | sed -n -r 's/(.*) connected .*/\1/p'`
printf "detected and connected monitors:\n%s\n\n" "$MONITORS_CONNECTED"

MONITORS_DISCONNECTED=`LANG=C xrandr | sed -n -r 's/(.*) disconnected .*/\1/p'`
printf "disconnected monitors:\n%s\n\n" "$MONITORS_DISCONNECTED"

# reset all disconnected monitors to auto
for i in $MONITORS_DISCONNECTED; do
    MONITORS_DISCONNECTED_MODE="$MONITORS_DISCONNECTED_MODE --output $i --auto"
done
if [ "$MONITORS_DISCONNECTED_MODE" ]; then
    xrandr $MONITORS_DISCONNECTED_MODE
fi


# detect primary (internal) monitor
for i in $MONITORS_CONNECTED; do
    if expr match "$i" "$MONITOR_PRIMARY_REGEX" >/dev/null; then
        MONITOR_PRIMARY=$i
        printf "primary monitor name: $MONITOR_PRIMARY\n\n"
    fi
done


# if [ "$MONITOR_PRIMARY" ]; then
#     printf "primary monitor name: $MONITOR_PRIMARY\n"
#     MONITOR_PRIMARY_MODE=`LANG=C xrandr | sed -n -r "s/$MONITOR_PRIMARY connected ([0-9x\+]+) .*/\1/p"`
#     printf "primary Monitor Mode: $MONITOR_PRIMARY_MODE\n"
# fi

#
# three modes:
#   auto:
#       all monitors are set to auto. 
#       The result is, that the larger resolution is choosen, 
#       and smaller displays are limited clones of largerest one
#   mode:
#       if a mode is given as cmdline option,
#       it mode is set for all external (not primary) displays.
#       The primary display is set to auto (full-) resolution.
#       Attention: the full primary resolution might not be supported by the other displays
#       Use mode 0x0 to turn off the other displays.
#   fallback:
#       set all displays to a fallback resolution. Not used.
#

for i in $MONITORS_CONNECTED; do
    MONITORS_AUTO="$MONITORS_AUTO --output $i --auto"
    MONITORS_FALLBACK="$MONITORS_FALLBACK --output $i --mode 1024x768"
    if [ "$i" = "$MONITOR_PRIMARY" ]; then
        MONITORS_AUTO="$MONITORS_AUTO --primary"
        MONITORS_MODE="$MONITORS_MODE --output $i --auto --primary"
    else
        MONITORS_MODE="$MONITORS_MODE --output $i --mode ${mode:-1024x768}"
    fi
done
echo "auto:     $MONITORS_AUTO"
echo "fallback: $MONITORS_FALLBACK"
echo "mode:     $MONITORS_MODE"

if [ -n "$mode" ]; then
    printf "mode $mode defined, using mode\n"
    SETTINGS=$MONITORS_MODE
else
    # both monitor to auto 
    #   => larger resolution is choosen, smaller display is clone of larger one
    printf "no modes defined, using auto\n"
    SETTINGS=$MONITORS_AUTO
fi

if ! xrandr $SETTINGS; then
    printf "failed to modify display settings\n"
    exit 1
fi


sleep 3
xsetroot -cursor /usr/share/pixmaps/empty.xbm /usr/share/pixmaps/empty.xbm
xrefresh -white
xsetroot -cursor_name arrow

exit 0
