#!/bin/sh
#
#        Script: netbook-fixes
#         Title: Screen and other fixups for netbook devices
#
# A bundle of functions that will fix some of the netbook screen
# issues.  Some are only relevant for specific platforms.
#
# Last Modified: 2026-04-10 14:31:00
#

# NOTES
#echo 1 | sudo tee /sys/class/graphics/fbcon/rotate_all
#    0 - Normal rotation
#    1 - Rotate clockwise (right)
#    2 - Rotate upside down
#    3 - Rotate counter-clockwise (left)
#
#https://github.com/plbossart/UCM/iss...ment-383365511
# RUN: monitor-sensor to see directions

# This gets goofy
# No Idea, . . .  Rotate right?
# SENSOR_MAT="0, 1, 0; 1, 0, 0; 0, 0, 1"
SENSOR_MAT="0, 1, 0; -1, 0, 1; 0, 0, 1"

# I want ethX names, and I'm not worried about meltdown/spectre on my laptop
GRUB_PARMS="net.ifnames=0 mitigations=off i915.enable_psr=2 i915.mitigations=off"

# Chuwi Minibook X, KooTigers 8"
MONITOR_OUTPUT_ID=DSI-1
# Chuwi, Koo use 1, OneMix is 3
ROTATE=1

#----------------------------------------------------------------------
# DO_INSTALL
#----------------------------------------------------------------------
do_install() {
    sudo apt -y install firmware-intel-sound alsa-firmware-loaders
    sudo cp ./TS-disable ./FIX /usr/local/bin
    sudo chmod 0775 /usr/local/bin/TS-disable /usr/local/bin/FIX
    sudo ln /usr/local/bin/TS-disable /usr/local/bin/TS-enable
    sudo apt-get -y install xbindkeys xdotool
}

#----------------------------------------------------------------------
# FIX_GRUB
#----------------------------------------------------------------------
fix_grub() {
    cat /etc/default/grub | awk \
    -v grub_parms="$GRUB_PARMS"
    -v rotate=$ROTATE \
'
/^GRUB_CMDLINE_LINUX_DEFAULT/ && ! /rotate/ {
    printf "GRUB_CMDLINE_LINUX_DEFAULT=\"quiet fbcon=rotate:%d\"\n",rotate
    next
}
/^GRUB_CMDLINE_LINUX=/ {
    printf "GRUB_CMDLINE_LINUX=\"%s\"\n",grub_parms
    next
}
    {
    print $0
}
' > /etc/default/grub.new
    mv /etc/default/grub /etc/default/grub.old
    mv /etc/default/grub.new /etc/default/grub
}

#----------------------------------------------------------------------
# FIX_CONSOLE_FONT
# I like a somewhat larger font
#----------------------------------------------------------------------
fix_console_font() {
    cat /etc/default/console-setup | awk '
/^FONTFACE/ {
    next
}
/^FONTSIZE/ {
    next
}
END {
    printf "FONTFACE=\"Terminus\"\n"
    printf "FONTSIZE=\"12x24\"\n"
}
' > /etc/default/console_setup.new  # '
    mv /etc/default/console_setup /etc/default/console_setup.old
    mv /etc/default/console_setup.new /etc/default/console_setup
}

#----------------------------------------------------------------------
# FIX_ROTATE_SENSOR
# Abandoned This was the OneMix
#----------------------------------------------------------------------
fix_rotate_sensor() {
    cat - <<FIX_ROTATE_END > /lib/udev/hwdb.d/61-sensor-local.hwdb
sensor:modalias:acpi:BOSC0200*:dmi*
 ACCEL_MOUNT_MATRIX=0, 1, 0; 1, 0, 0; 0, 0, 1
FIX_ROTATE_END
    systemd-hwdb update
}

#----------------------------------------------------------------------
# FIX_SOUND
# This is for the Teclast F5, and older onemix/distro releases
#----------------------------------------------------------------------
fix_sound() {
    echo "blacklist snd_hdmi_lpe_audio" > /etc.modprobe.d/blacklist_hdmi.conf
}

#----------------------------------------------------------------------
# FIX_SDDM
# Using Xorg Below
#----------------------------------------------------------------------
fix_sddm() {
    setup_script=/usr/share/sddm/scripts/Xsetup
    grep xrandr $setup_script && return
    echo "xrandr --output eDP-1 --rotate right" >> $setup_script
}

#----------------------------------------------------------------------
# FIX_USERXWIN
#----------------------------------------------------------------------
fix_userxwin() {
    cat - > ~/.xbindkeysrc <<EOF_XBINDKEYS
"xdotool click 2"
  Control + b:3 + Release
EOF_XBINDKEYS
    cat - > ~/.xsessionrc <<EOF_XSESSIONRC
/usr/local/bin/FIX
xbindkeys
EOF_XSESSIONRC
}

#----------------------------------------------------------------------
# USAGE
#----------------------------------------------------------------------
usage() {
    echo "$pgm {cmd}"
    echo "  cmd is one of ..."
    echo "    --help       This help text"
    echo "    all          All fixes"
    echo "    sensor       Rotate sensor fixus"
    echo "    grub         Add stuff to grub.cfg"
    echo "    font         Console Fonts"
    echo "    sddm         Fixups for sddm/rotation"
    echo "    install      Install needed software packages"

}

#----------------------------------------------------------------------
# ADD_XORG_CONF
#----------------------------------------------------------------------
add_xorg_conf() {
    cat - <<EOF_ADD_XORG_CONF10 > /etc/X11/xorg.conf.d/10-monitor.conf
Section "Monitor"
  Identifier "$MONITOR_OUTPUT_ID"
  option "Rotate" "right"
EndSection

Section "Screen"
    Identifier "Screen0"
    Device "Device0"
    Monitor "$MONITOR_OUTPUT_ID"
EndSection
EOF_ADD_XORG_CONF10

    cat - <<EOF_ADD_XORG_CONF30 > /etc/X11/xorg.conf.d/30-touchpad.conf
Section "InputClass"
    Identifier "touchpad"
    Driver "libinput"
    MatchIsTouchpad "on"
    Option "SendEventsMode" "disabled-on-external-mouse"
EndSection
EOF_ADD_XORG_CONF30
}

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------
pgm=$0
while [ $# -gt 0 ]
do
    case "x$1" in
      xall)
        do_install
        fix_grub
        fix_console_font
        add_xorg_conf
        fix_user_xwin
        ;;
      xfont)
        fix_console_font
        ;;
      xgrub)
        fix_grub
        ;;
      xinstall)
        do_install
        ;;
      xxorg)
        add_xorg_conf
        ;;
#      xsddm)
#        fix_sddm
#        ;;
#      xsensor)
#        fix_rotate_sensor
#        ;;
      xxwin)
        fix_userxwin
        ;;
      x--help|x-u)
        usage
        ;;
    esac
    shift
done
