#!/bin/bash

# possible machine types:
#   - LT11.arm64.v1.rev1
#   - KVADRAT.arm64.v1.rev1
#   - ZHISD.arm64.v1.rev1
#   - OKOTAB5PRO.arm64.v1.rev1
#   - MIGT8X.amd64.v2.rev50
#   - MIGT8X.amd64.v2.rev51
#   - MIGT10X.amd64.v2.rev1
#   - MIGT10X-81AL37SY1.amd64.v1.rev51
#   - IRUONYX.amd64.v1.rev1
#   - LENOVOURU.amd64.v1.rev1
#   - LENOVOERU.amd64.v1.rev1
#   - CHUWIHI10X.amd64.v1.rev1
#   - CHUWIUBOOKXPRO.amd64.v1.rev1
#   - ZHISD.amd64.v1.rev1
#   - N100-1.amd64.v1.rev1
#   - PINEPRO.arm64.v1.rev1
#   - virtual-machine
#   - generic

MACHINE_TYPE=""
if [ -e /etc/astra/machine_type ]; then
    MACHINE_TYPE=`cat /etc/astra/machine_type | head -1 | tr -s ' '`
    echo "astra-mobile: detected machine type $MACHINE_TYPE"
fi

if [[ $MACHINE_TYPE == *"arm64"* ]]; then
    # enable qml users cache for arm (BT-37690)
mkdir -p /etc/xdg/QtProject
cat > /etc/xdg/QtProject/security.ini <<EOF
DisableUserQmlCaches=false
EOF

    # remove extra fonts and fly fontconfigs (BT-37690)
    env $ENV_NONINTERACTIVE apt-get $APT_OPTIONS -y remove fonts-noto-ui-extra
    env $ENV_NONINTERACTIVE apt-get $APT_OPTIONS -y remove fonts-noto-extra
    env $ENV_NONINTERACTIVE apt-get $APT_OPTIONS -y remove fly-fontconfig-settings
fi

if [[ $MACHINE_TYPE == *"amd64"* || $MACHINE_TYPE == *"generic"* || $MACHINE_TYPE == "virtual-machine" ]]; then
    # mic control fix (BT-50216)
    # daemons restart fix (BT-50751)
    # gps satellite location fix (BT-50697)
    sed -i "s/LOAD_KEXEC=.*/LOAD_KEXEC=false/" /etc/default/kexec
fi

if [[ $MACHINE_TYPE == *"generic"* || $MACHINE_TYPE == "virtual-machine"  ]]; then
    # BT-72722
    kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 0
fi

# BT-54899
sed -i "s/module-card-restore/module-card-restore restore_bluetooth_profile=true/" /etc/pulse/default.pa

# enable ifdnfc
sed -i "s/#FRIENDLYNAME/FRIENDLYNAME/" /etc/reader.conf.d/ifdnfc
sed -i "s/#LIBPATH/LIBPATH/" /etc/reader.conf.d/ifdnfc
sed -i "s/#CHANNELID/CHANNELID/" /etc/reader.conf.d/ifdnfc

cat > /etc/systemd/system/enable-ifdnfc.service <<EOF
[Unit]
Description=Enable ifdnfc
Requires=pcscd.service
After=pcscd.service

[Service]
Type=simple
ExecStart=/usr/bin/ifdnfc-activate no-exit

[Install]
WantedBy=multi-user.target
EOF

systemctl disable pcscd

# BT-62747
if [ -f /usr/bin/maliit-server-keyboard-show ]; then
    mkdir -p /var/lib/fly-dm/.local/share/maliit/keyboard2/devices/
    cat > /var/lib/fly-dm/.local/share/maliit/keyboard2/devices/default.json <<EOF
{
    "keyboardHeightLandscape": 0.3,
    "keyboardHeightPortrait": 0.25
}
EOF
    chown -R fly-dm:fly-dm /var/lib/fly-dm/.local/
    sed -i 's:#VkbdCmd=/usr/bin/fly-vkbd --strut-bottom -s black_flat:VkbdCmd=/usr/bin/maliit-server-keyboard-show:' /etc/X11/fly-dm/fly-dmrc
    if [ -f /usr/lib/dconf/dconf-service ]; then
        cat > /usr/share/fly-dm/preload/greeter/dconf-service.desktop <<EOF
[Desktop Entry]
Type=Application
Name=dconf-service
Exec=/usr/lib/dconf/dconf-service
EOF
    else
        cat > /usr/share/fly-dm/preload/greeter/dconf-service.desktop <<EOF
[Desktop Entry]
Type=Application
Name=dconf-service
Exec=/usr/libexec/dconf-service
EOF
    fi
fi

# BT-71659
INT_CHECK_CONFIG_PATH='/root/.config/rusbitech/fly-admin-int-check.conf'
ASTRA_MOBILE_IGNORE_LIST='/etc/xdg/rusbitech/fly-admin-int-check.conf'

if [ -f "$INT_CHECK_CONFIG_PATH" ] && [ -f "$ASTRA_MOBILE_IGNORE_LIST" ]; then
    OLD_IFS=$IFS
    IFS=', '

    ignoreFilters=()
    read -a ignoreFilters <<< $(cat $ASTRA_MOBILE_IGNORE_LIST | grep 'ignoreFilters=' | sed 's/ignoreFilters=//g')

    extraIgnoreFilters=()
    read -a extraIgnoreFilters <<< $(cat $ASTRA_MOBILE_IGNORE_LIST | grep 'extraIgnoreFilters=' | sed 's/extraIgnoreFilters=//g')

    intCheckIgnoreFilters=()
    read -a intCheckIgnoreFilters <<< $(cat $INT_CHECK_CONFIG_PATH | grep 'ignoreFilters=' | sed 's/ignoreFilters=//g')

    resultIgnoreFilters=( "${ignoreFilters=[@]}" "${extraIgnoreFilters[@]}" "${intCheckIgnoreFilters[@]}" )
    read -a extraIgnoreFilters <<< $(sort -u <<< ${resultIgnoreFilters[*]})

    IFS=$OLD_IFS

    result=''
    printf -v result "%s, " "${resultIgnoreFilters[@]}"
    result=${result:0:-2}

    if [ ! -z "$result" ]; then
        if grep -Fxq "[MainWindow]" "$INT_CHECK_CONFIG_PATH"; then
            # section MainWindow exists! make replase
            sed -i "/^\[MainWindow\]$/,/^\[/ s#^ignoreFilters=.*#ignoreFilters=$result#" "$INT_CHECK_CONFIG_PATH"
        fi
    fi
fi

case "$MACHINE_TYPE" in
    LENOVOERU.amd64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/lenovo/calibrate-screen-ideapad-duet3
        /root/hardware/lenovo/ignore-scndpwrbtn-ideapad-duet3
        sed -i "s/normal/right_side_up/g" /etc/default/grub
        /usr/sbin/update-grub
        cp -f /root/hardware/lenovo/lenovo-screen-status-helperrc /etc/xdg/screen-status-helperrc
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270
    ;;

    LENOVOURU.amd64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/lenovo/calibrate-screen-ideapad-duet3
        /root/hardware/lenovo/ignore-scndpwrbtn-ideapad-duet3
        cp -f /root/hardware/lenovo/99-lenovo-pci-modem.rules /etc/udev/rules.d/
        sed -i "s/normal/right_side_up/g" /etc/default/grub
        /usr/sbin/update-grub
        cp -f /root/hardware/lenovo/lenovo-screen-status-helperrc /etc/xdg/screen-status-helperrc
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270
    ;;

    MIGT8X.amd64.v2.rev50 )
        /root/hardware/mig/calibrate-screen-t8s
        cp -f /root/hardware/mig/99-mig-fix-wakeup.rules /etc/udev/rules.d/
        cp -f /root/hardware/mig/autobrightness-helperrc /etc/xdg/
        cp -f /root/hardware/mig/t8s-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        cp -f /root/hardware/mig/gpsd-t8s /etc/default/gpsd
        cp -f /root/hardware/mig/snd-hda-intel.conf /etc/modprobe.d/snd-hda-intel.conf
        cp -f /root/hardware/mig/screen-status-helperrc /etc/xdg/screen-status-helperrc
        sed -i "s/normal/left_side_up/g" /etc/default/grub
        sed -i "s/ acpi_mask_gpe=0x6F//" /etc/default/grub
        sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 acpi_mask_gpe=0x6F\"/" /etc/default/grub
        echo "GRUB_PRELOAD_MODULES=\"cachebits10\"" >> /etc/default/grub
        /usr/sbin/update-grub
        /root/hardware/mig/enable-snd-power-save.sh
        cp -f /root/hardware/mig/kcm_keys_single.desktop /usr/share/applications/
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
        /root/hardware/mig/t8s-v50-fix-gps
    ;;

    MIGT8X.amd64.v2.rev51 )
        /root/hardware/mig/calibrate-screen-t8s
        cp -f /root/hardware/mig/99-mig-fix-wakeup.rules /etc/udev/rules.d/
        cp -f /root/hardware/mig/autobrightness-helperrc /etc/xdg/
        cp -f /root/hardware/mig/gpsd-t8s-v51 /etc/default/gpsd
        cp -f /root/hardware/mig/ru.astralinux.t8s.policy /usr/share/polkit-1/actions/
        cp -f /root/hardware/mig/t8s_scan_trig /usr/lib/astra-mobile/
        cp -f /root/hardware/mig/snd-hda-intel.conf /etc/modprobe.d/snd-hda-intel.conf
        cp -f /root/hardware/mig/screen-status-helperrc /etc/xdg/screen-status-helperrc
        sed -i "s/normal/left_side_up/g" /etc/default/grub
        sed -i "s/ acpi_mask_gpe=0x6F//" /etc/default/grub
        sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 acpi_mask_gpe=0x6F\"/" /etc/default/grub
        echo "GRUB_PRELOAD_MODULES=\"cachebits10\"" >> /etc/default/grub
        /usr/sbin/update-grub
        /root/hardware/mig/t8s-v51-fix
        /root/hardware/mig/enable-snd-power-save.sh
        cp -f /root/hardware/mig/mig-t8-f-key.service /etc/systemd/system/
        cp -f /root/hardware/mig/mig-t8-f-key /usr/sbin/
        cp -f /root/hardware/mig/mig-t8-scanner-key.service /etc/systemd/system/
        cp -f /root/hardware/mig/mig-t8-scanner-key /usr/sbin/
        cp -f /root/hardware/mig/kcm_keys_single.desktop /usr/share/applications/
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    MIGT10X.amd64.v2.rev1 )
        cp -f /root/hardware/mig/99-mig-fix-wakeup.rules /etc/udev/rules.d/
        cp -f /root/hardware/mig/snd-hda-intel.conf /etc/modprobe.d/snd-hda-intel.conf
        cp -f /root/hardware/mig/screen-status-helperrc /etc/xdg/screen-status-helperrc
        cp -f /root/hardware/mig/t10-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        /root/hardware/mig/calibrate-screen-and-fix-touch-t10
        /root/hardware/mig/t10-new-fix
        /root/hardware/mig/t10-sdhci-driver-hack
        sed -i "s/normal/left_side_up/g" /etc/default/grub
        sed -i "s/ acpi_mask_gpe=0x6F//" /etc/default/grub
        sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 acpi_mask_gpe=0x6F\"/" /etc/default/grub
        echo "GRUB_PRELOAD_MODULES=\"cachebits10\"" >> /etc/default/grub
        /usr/sbin/update-grub
        /root/hardware/mig/enable-snd-power-save.sh
        cp -f /root/hardware/mig/kcm_keys_single.desktop /usr/share/applications/
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
        if ! grep -q "/dev/mmcblk" /etc/fstab.pdac; then
            if grep -q "1.8" /etc/os-release; then
                echo -e "\n/dev/mmcblk* /run/user/*/media/* auto pdac,owner,group,nodev,noexec,noauto,defaults 0 0" >> /etc/fstab.pdac
            elif grep -q "1.7" /etc/os-release; then
                echo -e "\n/dev/mmcblk* /run/user/*/media/* auto owner,group,nodev,noexec,noauto,defaults 0 0" >> /etc/fstab.pdac
            fi
        fi
    ;;

    MIGT10X-81AL37SY1.amd64.v1.rev51 )
        cp -f /root/hardware/mig/99-mig-fix-wakeup.rules /etc/udev/rules.d/
        cp -f /root/hardware/mig/snd-hda-intel.conf /etc/modprobe.d/snd-hda-intel.conf
        cp -f /root/hardware/mig/t10-81a-screen-status-helperrc /etc/xdg/screen-status-helperrc
        cp -f /root/hardware/mig/t10-81a-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        /root/hardware/mig/calibrate-screen-and-fix-touch-t10-81AL37SY1
        /root/hardware/mig/t10-new-fix
        /root/hardware/mig/t10-sdhci-driver-hack
        sed -i "s/normal/right_side_up/g" /etc/default/grub
        sed -i "s/ acpi_mask_gpe=0x6F//" /etc/default/grub
        sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 acpi_mask_gpe=0x6F\"/" /etc/default/grub
        echo "GRUB_PRELOAD_MODULES=\"cachebits10\"" >> /etc/default/grub
        /usr/sbin/update-grub
        /root/hardware/mig/enable-snd-power-save.sh
        cp -f /root/hardware/mig/kcm_keys_single.desktop /usr/share/applications/
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270
        if ! grep -q "/dev/mmcblk" /etc/fstab.pdac; then
            if grep -q "1.8" /etc/os-release; then
                echo -e "\n/dev/mmcblk* /run/user/*/media/* auto pdac,owner,group,nodev,noexec,noauto,defaults 0 0" >> /etc/fstab.pdac
            elif grep -q "1.7" /etc/os-release; then
                echo -e "\n/dev/mmcblk* /run/user/*/media/* auto owner,group,nodev,noexec,noauto,defaults 0 0" >> /etc/fstab.pdac
            fi
        fi
    ;;

    IRUONYX.amd64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/iru/fix-volkeys-onyx-i7
        sed -i "s/ acpi_enforce_resources=lax//" /etc/default/grub
        sed -i "s/\(GRUB_CMDLINE_LINUX_DEFAULT=\".*\)\"/\1 acpi_enforce_resources=lax\"/" /etc/default/grub

        # default scale (BT-40373)
        sed -i "s/scale\": 2/scale\": 1.7/" /usr/share/kscreen/generic-config
        /usr/sbin/update-grub
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    CHUWIHI10X.amd64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/chuwi/calibrate-screen-hi10x
        /root/hardware/chuwi/fix-sound-hi10x
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    CHUWIUBOOKXPRO.amd64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/chuwi/calibrate-screen-xpro
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    KVADRAT.arm64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        echo pn7160_i2c > /etc/modules-load.d/pn7160_i2c.conf
        /root/hardware/yadro/calibrate-screen-ts11
        /root/hardware/yadro/wifi-filter-fix
        /root/hardware/yadro/rk-device-permission-fix
        /root/hardware/yadro/chromium-libv4l2-fix
        /root/hardware/yadro/kvadrat-fix-volume
        /root/hardware/yadro/setup-video-devices-fix
        /root/hardware/yadro/fix-mount-sdcard
        cp -f /root/hardware/yadro/gpsd-ts11 /etc/default/gpsd
        cp -f /root/hardware/yadro/generic-config-ts11 /usr/share/kscreen/generic-config
        rm -f /etc/udev/rules.d/99-ts11-disable-modem.rules
        cp -f /root/hardware/yadro/99-himax-touchscreen-disable-mouse.rules /etc/udev/rules.d/
        cp -f /root/hardware/yadro/ts11-enable-modem.service /etc/systemd/system/
        cp -f /root/hardware/yadro/lte-modem.nmconnection /etc/NetworkManager/system-connections/
        chmod 600 /etc/NetworkManager/system-connections/*
        cp -f /root/hardware/yadro/autobrightness-helperrc /etc/xdg/
        cp -f /root/hardware/yadro/kvadrat-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        mkdir -p /etc/mpv/
        cp -f /root/hardware/yadro/kvadrat-mpv.conf /etc/mpv/mpv.conf
        cp -f /root/hardware/yadro/kill-fly-camera /usr/lib/systemd/system-sleep/kill-fly-camera
        cp -f /root/hardware/yadro/kvadrat-check-flashlight /usr/lib/systemd/system-sleep/kvadrat-check-flashlight
        cp -f /root/hardware/yadro/fix-audio-level /usr/lib/systemd/system-sleep/fix-audio-level
        cp -f /root/hardware/yadro/kvadrat.pref /etc/apt/preferences.d/
        cp -f /root/hardware/yadro/kvadrat-screen-status-helperrc /etc/xdg/screen-status-helperrc
        systemctl enable rkaiq
        systemctl enable ts11-enable-modem
        mkdir -p /vendor/etc
        ln -fs /usr/lib/firmware /vendor/etc/firmware
        mkdir -p /etc/pipewire/media-session.d
        cp -f /root/hardware/yadro/media-session.conf /etc/pipewire/media-session.d/media-session.conf
        sed -i "s/timeout=.*/timeout=2500/" /etc/xdg/outputswatcher
        sed -i "s/set-source-volume 7 400000//" /etc/pulse/default.pa
        sed -i "s/Modules=kcm_password/Modules=kcm_password,kcm_nightcolor,kcm_mobile_hotspot/" /etc/xdg/plasmasettingsrc
        sed -i "s/; default-sample-rate = 44100/default-sample-rate = 48000/" /etc/pulse/daemon.conf
        kwriteconfig5 --file /etc/NetworkManager/NetworkManager.conf --group keyfile --key unmanaged-devices "interface-name:usb*;interface-name:eth0"
        kwriteconfig5 --file /etc/xdg/outputswatcher --group ExtMonitor --key legacyMode true
        cat >> /etc/xdg/plasmamobilerc <<EOF
[QuickSettings]
disabledQuickSettings=org.kde.plasma.quicksetting.forcerotation.left,org.kde.plasma.quicksetting.forcerotation.right,org.kde.plasma.quicksetting.nightcolor
EOF
        cp -f /root/hardware/yadro/99-ts11-init-modem.rules /etc/udev/rules.d/

        if ! [ -d /etc/systemd/system/ModemManager.service.d ]; then
            mkdir /etc/systemd/system/ModemManager.service.d
        fi
        cp -f /root/hardware/yadro/override.conf /etc/systemd/system/ModemManager.service.d/

        if ! [ -d /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile ]; then
            mkdir /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile
        fi
        cp -f /usr/lib/aarch64-linux-gnu/ModemManager/libmm-plugin-generic.so /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile/

        /root/hardware/yadro/setup-keyboard
        /root/hardware/yadro/fix-inputs
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270

        cp -f /root/hardware/yadro/01-astra-mobile-kcms.conf /etc/tlp.d

        DEF_SOURCE="set-default-source alsa_input.platform-sound.HiFi__hw_rockchipes8326c_1__source"
        DEF_SINK="set-default-sink alsa_output.platform-sound.HiFi__hw_rockchipes8326c__sink"

        if ! grep -q "${DEF_SOURCE}" "/etc/pulse/default.pa"; then
            echo "$DEF_SOURCE" >> "/etc/pulse/default.pa"
        fi

        if ! grep -q "${DEF_SINK}" "/etc/pulse/default.pa"; then
            echo "$DEF_SINK" >> "/etc/pulse/default.pa"
        fi

    ;;

    LT11.arm64.v1.rev1 )
        /root/hardware/mig/lt11/touchscreen-and-screen-fix
        /root/hardware/mig/lt11/enable-wifi-filter
        /root/hardware/mig/lt11/lt11-init
        cp -f /root/hardware/mig/lt11/lt11-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        cp -f /root/hardware/mig/lt11/lt11.pref /etc/apt/preferences.d/
        kwriteconfig5 --file /etc/xdg/plasmamobilerc --group Brightness --key MinLevel 26
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    ZHISD.amd64.v1.rev1 )
        /root/hardware/zhisd/calibrate-screen-zhisd
        cp -f /root/hardware/zhisd/fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        cp -f /root/hardware/zhisd/gpsd /etc/default/
        cp -f /root/hardware/zhisd/es8336.conf /etc/modprobe.d/
        cp -f /root/hardware/zhisd/amd64/screen-status-helperrc /etc/xdg/screen-status-helperrc
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    ZHISD.arm64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/zhisd/arm64/calibrate-screen
        /root/hardware/zhisd/arm64/wifi-filter-fix
        /root/hardware/zhisd/arm64/rk-device-permission-fix
        /root/hardware/zhisd/arm64/chromium-libv4l2-fix
        /root/hardware/zhisd/arm64/setup-video-devices-fix
        /root/hardware/zhisd/arm64/fix-mount-sdcard
        cp -f /root/hardware/zhisd/arm64/gpsd /etc/default/gpsd
        cp -f /root/hardware/zhisd/arm64/generic-config /usr/share/kscreen/generic-config        
        cp -f /root/hardware/zhisd/arm64/zhisd-enable-modem.service /etc/systemd/system/      
        chmod 600 /etc/NetworkManager/system-connections/*
        cp -f /root/hardware/zhisd/arm64/autobrightness-helperrc /etc/xdg/
        cp -f /root/hardware/zhisd/arm64/fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        mkdir -p /etc/mpv/
        cp -f /root/hardware/zhisd/arm64/zhisd-mpv.conf /etc/mpv/mpv.conf
        cp -f /root/hardware/zhisd/arm64/kill-fly-camera /usr/lib/systemd/system-sleep/kill-fly-camera
        cp -f /root/hardware/zhisd/arm64/zhisd-check-flashlight /usr/lib/systemd/system-sleep/zhisd-check-flashlight
        cp -f /root/hardware/zhisd/arm64/zhisd.pref /etc/apt/preferences.d/
        cp -f /root/hardware/zhisd/arm64/screen-status-helperrc /etc/xdg/screen-status-helperrc
        systemctl enable rkaiq
        systemctl enable zhisd-enable-modem
        echo goodix_gt9xx > /etc/modules-load.d/goodix_gt9xx.conf
        mkdir -p /vendor/etc
        ln -fs /usr/lib/firmware /vendor/etc/firmware
        mkdir -p /etc/pipewire/media-session.d
        cp -f /root/hardware/zhisd/arm64/media-session.conf /etc/pipewire/media-session.d/media-session.conf
        sed -i "s/timeout=.*/timeout=2500/" /etc/xdg/outputswatcher
        sed -i "s/Modules=kcm_password/Modules=kcm_password,kcm_nightcolor,kcm_mobile_hotspot/" /etc/xdg/plasmasettingsrc
        sed -i "s/; default-sample-rate = 44100/default-sample-rate = 48000/" /etc/pulse/daemon.conf
        kwriteconfig5 --file /etc/NetworkManager/NetworkManager.conf --group keyfile --key unmanaged-devices "interface-name:usb*;interface-name:eth0"
        kwriteconfig5 --file /etc/xdg/outputswatcher --group ExtMonitor --key legacyMode true
        cat >> /etc/xdg/plasmamobilerc <<EOF
[QuickSettings]
disabledQuickSettings=org.kde.plasma.quicksetting.forcerotation.left,org.kde.plasma.quicksetting.forcerotation.right,org.kde.plasma.quicksetting.nightcolor
EOF
        cp -f /root/hardware/zhisd/arm64/99-zhisd-init-modem.rules /etc/udev/rules.d/
        if ! [ -d /etc/systemd/system/ModemManager.service.d ]; then
            mkdir /etc/systemd/system/ModemManager.service.d
        fi
        cp -f /root/hardware/zhisd/arm64/override.conf /etc/systemd/system/ModemManager.service.d/

        if ! [ -d /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile ]; then
            mkdir /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile
        fi
        cp -f /usr/lib/aarch64-linux-gnu/ModemManager/libmm-plugin-generic.so /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile/

        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270

        sed -i "s/\(append root=LABEL=rootfs.*\)/\1 video=panel_orientation=left_side_up/" /boot/extlinux/extlinux.conf
    ;;

    N100-1.amd64.v1.rev1 )
        /root/hardware/n100-1/calibrate-screen-n100-1
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 90
    ;;

    OKOTAB5PRO.arm64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/byterg/calibrate-screen-okotab5pro
        /root/hardware/byterg/rk-device-permission-fix
        /root/hardware/byterg/chromium-libv4l2-fix
        /root/hardware/byterg/setup-video-devices-fix
        cp -f /root/hardware/byterg/gpsd-okotab5pro /etc/default/gpsd
        cp -f /root/hardware/byterg/generic-config-okotab5pro /usr/share/kscreen/generic-config
        chmod 600 /etc/NetworkManager/system-connections/*
        cp -f /root/hardware/byterg/okotab5pro-fly-camera.conf /etc/xdg/rusbitech/fly-camera.conf
        mkdir -p /etc/mpv/
        cp -f /root/hardware/byterg/okotab5pro-mpv.conf /etc/mpv/mpv.conf
        cp -f /root/hardware/byterg/kill-fly-camera /usr/lib/systemd/system-sleep/kill-fly-camera
        cp -f /root/hardware/byterg/okotab5pro-check-flashlight /usr/lib/systemd/system-sleep/okotab5pro-check-flashlight
        cp -f /root/hardware/byterg/okotab5pro.pref /etc/apt/preferences.d/
        cp -f /root/hardware/byterg/screen-status-helperrc /etc/xdg/screen-status-helperrc
        systemctl enable rkaiq        
        mkdir -p /vendor/etc
        ln -fs /usr/lib/firmware /vendor/etc/firmware
        mkdir -p /etc/pipewire/media-session.d      
        cp -f /root/hardware/byterg/media-session.conf /etc/pipewire/media-session.d/media-session.conf          
        sed -i "s/timeout=.*/timeout=2500/" /etc/xdg/outputswatcher        
        sed -i "s/Modules=kcm_password/Modules=kcm_password,kcm_nightcolor,kcm_mobile_hotspot/" /etc/xdg/plasmasettingsrc        
        kwriteconfig5 --file /etc/NetworkManager/NetworkManager.conf --group keyfile --key unmanaged-devices "interface-name:usb*;interface-name:eth0"        
        cat >> /etc/xdg/plasmamobilerc <<EOF
[QuickSettings]
disabledQuickSettings=org.kde.plasma.quicksetting.forcerotation.left,org.kde.plasma.quicksetting.forcerotation.right,org.kde.plasma.quicksetting.nightcolor
EOF
        cp -f /root/hardware/byterg/99-okotab5pro-init-modem.rules /etc/udev/rules.d/
        
        if ! [ -d /etc/systemd/system/ModemManager.service.d ]; then
            mkdir /etc/systemd/system/ModemManager.service.d
        fi   
        
        cp -f /root/hardware/byterg/override.conf /etc/systemd/system/ModemManager.service.d/

        if ! [ -d /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile ]; then
            mkdir /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile
        fi
        cp -f /usr/lib/aarch64-linux-gnu/ModemManager/libmm-plugin-generic.so /usr/lib/aarch64-linux-gnu/ModemManager/astra-mobile/
        
        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 270

        cp -f /root/hardware/byterg/01-astra-mobile-kcms.conf /etc/tlp.d
    ;;

    PINEPRO.arm64.v1.rev1 )
        rm -f /etc/modules-load.d/t8s_ctl.conf
        /root/hardware/pine64/chromium-libv4l2-fix
        cp -f /root/hardware/pine64/99-calibration.rules /etc/udev/rules.d/
        cp -f /root/hardware/pine64/pinephonepro.pref /etc/apt/preferences.d/
        systemctl enable rkaiq
        systemctl enable eg25-manager

        cp -f /root/hardware/pine64/org.freedesktop.ModemManager1.pkla \
              /var/lib/polkit-1/localauthority/10-vendor.d/

        kwriteconfig5 --file /etc/unl0kr.conf --group general --key angle 0

        mkdir -p /usr/share/alsa/ucm2/Rockchip/PinePhonePro/
        cp -f /root/hardware/pine64/HiFi.conf           /usr/share/alsa/ucm2/Rockchip/PinePhonePro/
        cp -f /root/hardware/pine64/PinePhonePro.conf   /usr/share/alsa/ucm2/Rockchip/PinePhonePro/
        cp -f /root/hardware/pine64/VoiceCall.conf      /usr/share/alsa/ucm2/Rockchip/PinePhonePro/
        mkdir -p /usr/share/alsa/ucm2/conf.d/simple-card/
        rm -f /usr/share/alsa/ucm2/conf.d/simple-card/PinePhonePro.conf
        ln -s '../../Rockchip/PinePhonePro/PinePhonePro.conf' /usr/share/alsa/ucm2/conf.d/simple-card/PinePhonePro.conf

        cp -f /root/hardware/pine64/pa-select-default-profile.desktop /etc/xdg/autostart/
        cp -f /root/hardware/pine64/pa-select-default-profile.sh /usr/bin/

        cp -f /root/hardware/pine64/org.kde.phone.homescreen.js \
              /usr/share/plasma/look-and-feel/org.kde.plasma.phone/contents/plasmoidsetupscripts/
    ;;

    virtual-machine )
        /root/hardware/virt-machine/calibrate-screen-virt-machine
    ;;
esac
