#!/bin/bash -e
# Setup "Clockwork Pi uConsole" from "Raspberry Pi OS Full" running instance.
# You should execute this script as a non root user.
# This script will overwrite /boot/config.txt, $HOME/.config/monitors.xml and some other files.
# Be sure to know what you're doing or else, prefer to use the official image from Clockwork Pi
# at https://github.com/clockworkpi/uConsole#cm4

echo "uConsole install script v0.1"
echo "Updated: 2023-09-23"
echo "Sources:"
echo "- https://github.com/clockworkpi/DevTerm/wiki/Create-DevTerm-CM4-OS-image-from-scratch"
echo "- https://github.com/clockworkpi/uConsole/wiki/How-uConsole-CM4-OS-image-made"

# Adding clockwork repo
echo "------------------------------------------"
echo "Adding clockwork repo"
curl https://raw.githubusercontent.com/clockworkpi/apt/main/debian/KEY.gpg | sudo tee /etc/apt/trusted.gpg.d/clockworkpi.asc
echo "deb https://raw.githubusercontent.com/clockworkpi/apt/main/debian/ stable main" | sudo tee /etc/apt/sources.list.d/clockworkpi.list
sudo apt update

# Install drivers & kernel
echo "------------------------------------------"
echo "Installing drivers & kernel"
sudo apt install -y devterm-fan-temp-daemon-cm4 devterm-audio-patch devterm-wiringpi-cm4-cpi uconsole-kernel-cm4-rpi uconsole-4g-util-cm4 pppoe

# upgrade swap file size
echo "------------------------------------------"
echo "Increasing swap file size"
sudo sed -i 's/#\?[ ]*CONF_SWAPSIZE=.*/CONF_SWAPSIZE=512/' /etc/dphys-swapfile

# Update monitors file to use embeded screen with rotation
echo "------------------------------------------"
echo "Installing monitors.xml"
cd
cat << EOF > .config/monitors.xml
<monitors version="2">
  <configuration>
    <logicalmonitor>
      <x>0</x>
      <y>0</y>
      <primary>yes</primary>
      <monitor>
        <monitorspec>
          <connector>DSI-1</connector>
          <vendor>unknown</vendor>
          <product>unknown</product>
          <serial>unknown</serial>
        </monitorspec>
        <mode>
          <width>1280</width>
          <height>720</height>
          <rate>60.000</rate>
        </mode>
      </monitor>
      <transform>
        <rotation>right</rotation>
      </transform>
    </logicalmonitor>
  </configuration>
</monitors>
EOF
sudo mkdir -p /etc/skel/.config
sudo cp .config/monitors.xml /etc/skel/.config/monitors.xml

# Remove quiet option
echo "------------------------------------------"
echo "Removing quiet option in /boot/cmdline.txt"
sudo sed -i 's/ quiet//' /boot/cmdline.txt

# Replace /boot/config.txt
echo "------------------------------------------"
echo "Overwriting /boot/config.txt"
sudo bash -c 'cat << EOF > /boot/config.txt
disable_overscan=1
dtparam=audio=on
[pi4]
max_framebuffers=2

[all]
ignore_lcd=1
dtoverlay=dwc2,dr_mode=host
dtoverlay=vc4-kms-v3d-pi4,cma-384
dtoverlay=devterm-pmu
dtoverlay=devterm-panel-uc
dtoverlay=devterm-misc
dtoverlay=audremap,pins_12_13

dtparam=spi=on
gpio=10=ip,np
EOF'

# 4G extension related
echo "------------------------------------------"
echo "Blacklist QMI"
sudo bash -c 'cat << EOF > /etc/modprobe.d/blacklist-qmi.conf
blacklist qmi_wwan
blacklist cdc_wdm
EOF'

echo "------------------------------------------"
read -p "Would you like to install games? [y/N]: " yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
    echo "Installing games"
    # Install default games
    sudo apt install -y devterm-tic80-cpi uconsole-love2d retroarch dosbox devterm-cavestory-cpi-cm4 uconsole-liko12 uconsole-lowresnx uconsole-dosbox-staging
    # add config file for dosbox,opentyrian, ...
    if [ ! -d /usr/local/bin/skel ]; then
        cd /usr/local/bin/
        sudo git clone -b uconsole-cm4 https://github.com/cuu/skel.git
        sudo cp -rf skel/etc/xdg/autostart/first-touch.desktop /etc/xdg/autostart/
        sudo cp -rf skel/usr/local/bin/chgpas.sh /usr/local/bin/
    fi
fi

echo "------------------------------------------"
read -p "Would you like to reboot now? [y/N]: " yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
    echo "Restarting"
    sudo shutdown -r now
fi
