#!/bin/bash

D_DSI="DSI-1"
D_I2C="10-0014"
T_HMI=0

init_i2c(){
    T_CSI_DSI=$1
    t_dsi=$(dtc -I fs /proc/device-tree 2>/dev/null | sed -nE "s/.*${T_CSI_DSI} = (.*i2c.*);$/\1/p"  | sed 's/\//\\\//g')
    t_i2c=$(dtc -I fs /proc/device-tree 2>/dev/null | sed -nE "s/.*\<i2c([0-9]*) = ${t_dsi};$/\1/p" | head -n 1)
    D_I2C="${t_i2c}-0014"
}

init(){
    t_i2c_csi_dsi=i2c_csi_dsi

    dpkg -l | grep -q "ed-.*-101c-firmware"
    if [ $? -eq 0 ] ; then
        T_HMI=10
        dpkg -l | grep -q "ed-hmi3.*-101c-firmware"
        if [ $? -eq 0 ] ; then
            D_DSI="DSI-2"
            t_i2c_csi_dsi=i2c_csi_dsi0
        fi
    else
        dpkg -l | grep -q "ed-.*-070c-firmware"
        if [ $? -eq 0 ] ; then
            T_HMI=7
            dpkg -l | grep -q "ed-hmi3.*-070c-firmware"
            if [ $? -eq 0 ] ; then
                D_DSI="DSI-2"
                t_i2c_csi_dsi=i2c_csi_dsi0
            fi
        fi
    fi

    init_i2c ${t_i2c_csi_dsi}
}


update_wayfire_101C(){
    local t_user=$1
    cat > /home/${t_user}/.config/wayfire.ini << EOF
[command]
repeatable_binding_volume_up = KEY_VOLUMEUP
command_volume_up = wfpanelctl volumepulse volu
repeatable_binding_volume_down = KEY_VOLUMEDOWN
command_volume_down = wfpanelctl volumepulse vold
binding_mute = KEY_MUTE
command_mute = wfpanelctl volumepulse mute
binding_menu = <super>
command_menu = wfpanelctl smenu menu
binding_terminal = <ctrl> <alt> KEY_T
command_terminal = lxterminal
binding_bluetooth = <ctrl> <alt> KEY_B
command_bluetooth = wfpanelctl bluetooth menu
binding_netman = <ctrl> <alt> KEY_W
command_netman = wfpanelctl netman menu
binding_grim = KEY_SYSRQ
command_grim = grim
binding_orca = <ctrl> <alt> KEY_SPACE
command_orca = gui-pkinst orca reboot
binding_quit = <ctrl> <alt> KEY_DELETE
command_quit = lxde-pi-shutdown-helper
binding_power = KEY_POWER
command_power = pwrkey

[input-device:generic ft5x06 (79)]
output = DSI-1

[input-device:generic ft5x06 (80)]
output = DSI-1

[input-device:FT5406 memory based driver]
output = DSI-1

[input]
xkb_model = pc105
xkb_layout = gb
xkb_variant =

[output:${D_DSI}]
mode = 800x1280@40000
position = 0,0
transform = 90
primary = true

[input-device:Goodix Capacitive TouchScreen]
output = ${D_DSI}

[input-device:${D_I2C} Goodix Capacitive TouchScreen]
output = ${D_DSI}

[window-rules]
rule-1 = on created if title is "LXTerminal" then maximize
EOF
}

update_labwc_101c(){
    local t_user=$1
    cat > /home/${t_user}/.config/kanshi/config << EOF
profile {
        output ${D_DSI} mode 800x1280 position 0,0 transform 90
}
profile {
        output ${D_DSI} mode 800x1280 position 0,0 transform 90
        output HDMI-A-1 enable position 1280,0 transform normal
}
EOF
}

update_labwc_070c(){
    local t_user=$1
    cat > /home/${t_user}/.config/kanshi/config << EOF
profile {
        output ${D_DSI} position 0,0 transform normal
        output HDMI-A-1 enable position 1024,0 transform normal
}
EOF
}

update_kanshi(){
    local t_user=$1
    cat > /home/${t_user}/.config/labwc/rc.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>
<labwc_config>
  <touch deviceName="${D_I2C} Goodix Capacitive TouchScreen" mapToOutput="${D_DSI}" />
</labwc_config>
EOF
}

update_squeekboard(){
    if [ -f /etc/xdg/labwc/autostart ]; then
        cat /etc/xdg/labwc/autostart | grep -q "^export SQUEEKBOARD_PREFERRED_OUTPUT"
        if [ $? -ne 0 ] ; then
            sed -i "1i\export SQUEEKBOARD_PREFERRED_OUTPUT=${D_DSI}" /etc/xdg/labwc/autostart
        fi
    fi
}

update_labwc_autostart(){
    local t_user=$1
    cat > /home/${t_user}/.config/labwc/autostart << EOF
if [ -f /home/${t_user}/.config/wf-panel-pi.ini ];then
    cat /home/${t_user}/.config/wf-panel-pi.ini | grep -q "^monitor="
    if [ \$? -ne 0 ];then
        mkdir -p /home/${t_user}/.config/labwc
        sed -i "/^\[panel\]/a\monitor=${D_DSI}" /home/${t_user}/.config/wf-panel-pi.ini
    fi
else
    cat > /home/${t_user}/.config/wf-panel-pi.ini << EOL
[panel]
monitor=${D_DSI}
launcher_000001=lxde-x-www-browser.desktop
launcher_000002=pcmanfm.desktop
launcher_000003=lxterminal.desktop
EOL
fi
EOF
}

write_ini(){
    local t_user=$1
    mkdir -p /home/${t_user}/.config/
    if [ ${T_HMI} -eq 10 ]; then
        update_wayfire_101C ${t_user}
    fi
    update_labwc_autostart ${t_user}
    chown -R ${t_user}:${t_user} /home/${t_user}/.config
}

update_labwc(){
    local t_user=$1
    mkdir -p /home/${t_user}/.config/
    mkdir -p /home/${t_user}/.config/labwc
    mkdir -p /home/${t_user}/.config/kanshi

    if [ ${T_HMI} -eq 10 ]; then
        update_labwc_101c ${t_user}
    elif [ ${T_HMI} -eq 7 ]; then
        update_labwc_070c ${t_user}
    fi

    update_kanshi ${t_user}
    chown -R ${t_user}:${t_user} /home/${t_user}/.config
}

update_x11_101c(){
    cat > /usr/share/dispsetup.sh << EOF
#!/bin/sh

D_DSI=\$(xrandr | grep "^DSI.* connected" | awk '{print \$1}')
D_HDMI=\$(xrandr | grep "^HDMI.* connected" | awk '{print \$1}')
if xrandr --output \${D_HDMI} --auto --pos 1280x0  --output \${D_DSI} --primary --mode 800x1280 --pos 0x0 --rotate left --dryrun; then
    xrandr --output \${D_HDMI} --auto --pos 1280x0 --output \${D_DSI} --primary --mode 800x1280 --pos 0x0 --rotate left

elif xrandr --output \${D_DSI} --primary --mode 800x1280 --pos 0x0 --rotate left --dryrun; then
            xrandr --output \${D_DSI} --primary --mode 800x1280 --pos 0x0 --rotate left
fi
if xinput | grep -q "${D_I2C} Goodix Capacitive TouchScreen" ; then
    xinput --map-to-output pointer:"${D_I2C} Goodix Capacitive TouchScreen" \${D_DSI}
fi

if [ -e /usr/share/ovscsetup.sh ] ; then
        /usr/share/ovscsetup.sh
fi
exit 0
EOF
}

update_x11_070c(){
    cat > /usr/share/dispsetup.sh << EOF
#!/bin/sh

D_DSI=\$(xrandr | grep "^DSI.* connected" | awk '{print \$1}')
D_HDMI=\$(xrandr | grep "^HDMI.* connected" | awk '{print \$1}')

if xrandr --output \${D_HDMI} --auto --pos 1024x0  --output \${D_DSI} --primary --pos 0x0 --dryrun; then
    xrandr --output \${D_HDMI} --auto --pos 1024x0 --output \${D_DSI} --primary --pos 0x0
elif xinput | grep -q "${D_I2C} Goodix Capacitive TouchScreen" ; then
    xinput --map-to-output pointer:"${D_I2C} Goodix Capacitive TouchScreen" ${D_DSI}
fi

if [ -e /usr/share/ovscsetup.sh ] ; then
        /usr/share/ovscsetup.sh
fi
exit 0
EOF
}


update_wayland(){
    for t_user in `ls /home/`
    do
        if [ -d "/home/${t_user}" ];then
            update_labwc "${t_user}"
            write_ini "${t_user}"
        fi
    done
    if [ ${T_HMI} -eq 10 ] || [ ${T_HMI} -eq 7 ]; then
        update_squeekboard
    fi
}

update_x11(){
    if [ ${T_HMI} -eq 10 ]; then
        update_x11_101c
        chmod 755 /usr/share/dispsetup.sh
    elif [ ${T_HMI} -eq 7 ]; then
        update_x11_070c
        chmod 755 /usr/share/dispsetup.sh
    fi
}

run(){
    init
    echo "DISPLAY SETUP for HMI Type ${T_HMI} on ${D_DSI} via ${D_I2C}"
    update_wayland
    update_x11
}


run
