#!/bin/sh
# Provide ssh password
## Option 1: uncomment the following line to display entered password
#sudo read -sp "Enter ssh password : " PASSWORD_SSH
## Option 2: hide the entered password
echo "Welcome to AVDSP installation process"
echo "Check soundcard settings with alsamixer if no audio after this installation"
clear
read -p "Please enter for new DSP system then press Enter" DSP_NAME
clear
echo "Please enter password for SSH connections then press Enter: "
stty -echo
read PASSWORD_SSH
stty echo
# Install basic system
sudo usermod -aG audio mgt
sudo apt install -y libnss-mdns avahi-daemon lirc alsa-utils ir-keytable sshpass
sudo apt install -y python3 python3-pip python3-websocket python3-aiohttp python3-jsonschema unzip git rtirq-init # Cần thử bỏ python3-numpy python3-matplotlib 
sudo apt remove -y smartmontools sysstat haveged plymouth rsync rsyslog openvpn logrotate
sudo systemctl enable irexec
sudo echo 'tmpfs  /var/log  tmpfs  defaults,noatime,nosuid,nodev,noexec  0  0' >> /etc/fstab
sudo echo 'tmpfs  /var/lib/sudo  tmpfs  defaults,noatime,nosuid,nodev,noexec,size=1m,mode=0700  0  0' >> /etc/fstab
sudo sed -i 's/#Storage=auto/Storage=none/g' /etc/systemd/journald.conf && sed -i 's/SystemMaxUse=20M/SystemMaxUse=10M/g' /etc/systemd/journald.conf
# Reserve CPU for camilladsp
sudo echo 'extraargs=isolcpus=1,2' >> /boot/armbianEnv.txt
# Minimize boot time
sudo systemctl disable armbian-hardware-optimize.service
sudo systemctl disable armbian-hardware-monitor
sudo systemctl disable armbian-zram-config
sudo systemctl disable armbian-ramlog
sudo systemctl disable console-setup
sudo systemctl disable keyboard-setup
sudo systemctl disable resolvconf-pull-resolved
sudo systemctl disable resolvconf
sudo systemctl disable sysfsutils
sudo systemctl disable e2scrub_reap
sudo systemctl mask e2scrub_all.timer
sudo systemctl mask apt-daily-upgrade.timer
sudo systemctl mask logrotate.timer
sudo systemctl mask fstrim.timer 
sudo systemctl mask plymouth
sudo systemctl mask lircd-setup
sudo systemctl mask systemd-random-seed
# sudo echo "sudo systemctl mask systemd-remount-fs.service (dan den read-only, chu y co the su dung de khoa thay doi he thong)"
sudo systemctl mask kmod-static-nodes.service
sudo systemctl mask modprobe@configfs.service
sudo systemctl mask modprobe@fuse.service
sudo systemctl mask modprobe@drm.service
sudo systemctl mask systemd-update-utmp.service
sudo systemctl mask systemd-rfkill
sudo chmod -x /etc/cron.d/armbian-check-battery
sudo chmod -x /etc/cron.d/armbian-truncate-logs
sudo chmod -x /etc/cron.d/armbian-updates
sudo chmod -x /etc/cron.d/e2scrub_all
sudo chmod -x /etc/cron.d/sysstat
sudo chmod -x /etc/cron.daily/sysstat
sudo chmod -x /etc/cron.daily/logrotate
sudo chmod -x /etc/cron.daily/dpkg
sudo chmod -x /etc/cron.daily/cracklib-runtime
sudo chmod -x /etc/cron.daily/armbian-ram-logging
sudo chmod -x /etc/cron.daily/apt-compat
sudo sed -i '/After=/c\#After=systemd-sysusers.service systemd-hwdb-update.service\nStartLimitIntervalSec=0.01\nStartLimitBurst=771' /lib/systemd/system/systemd-udevd.service
sudo sed -i '/After=/c\#After=systemd-udevd-kernel.socket systemd-udevd-control.socket\nStartLimitIntervalSec=0.01\nStartLimitBurst=771' /lib/systemd/system/systemd-udev-trigger.service
# Install alsa loopback
sudo echo -e '# alsa loopback\nsnd\nsnd-timer\nsnd-pcm\nsnd-aloop' >> /etc/modules-load.d/aloop.conf
# Install camilladsp, need to copy camilladsp manually
mkdir /home/mgt/camilladsp
mkdir /home/mgt/camilladsp/coeffs
mkdir /home/mgt/camilladsp/configs
echo 'devices:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  samplerate: 48000' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  chunksize: 1024' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  queuelimit: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  adjust_period: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  capture:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    channels: 2' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    format: S16LE' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    device: hw:Loopback' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    type: Alsa' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  capture_samplerate: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  enable_rate_adjust: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  enable_resampling: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  playback:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    channels: 6' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    device: hw:Loopback' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    format: S16LE' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    type: Alsa' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  rate_measure_interval: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  resampler_type: BalancedAsync' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  silence_threshold: -60' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  silence_timeout: 3' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  stop_on_rate_change: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  target_level: 1024' >> /home/mgt/camilladsp/configs/camilladsp.yml  
echo 'mixers:' >> /home/mgt/camilladsp/configs/camilladsp.yml  
echo '  2to6channel:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    channels:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      in: 2' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      out: 6' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    mapping:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 2' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 3' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 4' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '    - dest: 5' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      sources:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '      - channel: 1' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        gain: 0' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        inverted: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '        mute: false' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo 'pipeline:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '- name: 2to6channel' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  type: Mixer' >> /home/mgt/camilladsp/configs/camilladsp.yml
# Setup camilladsp service
sudo echo '[Unit]' >> /lib/systemd/system/camilladsp.service
sudo echo 'Description=AVDSP (Powered by CamillaDSP)' >> /lib/systemd/system/camilladsp.service
sudo echo 'After=sound.target' >> /lib/systemd/system/camilladsp.service
sudo echo 'StartLimitIntervalSec=0.1' >> /lib/systemd/system/camilladsp.service
sudo echo 'StartLimitBurst=771' >> /lib/systemd/system/camilladsp.service
sudo echo '[Service]' >> /lib/systemd/system/camilladsp.service
sudo echo 'Type=simple' >> /lib/systemd/system/camilladsp.service
sudo echo 'ExecStart=taskset -c 1 /home/mgt/camilladsp/camilladsp -p 1234 /home/mgt/camilladsp/active_config.yml' >> /lib/systemd/system/camilladsp.service
sudo echo 'Restart=always' >> /lib/systemd/system/camilladsp.service
sudo echo 'RestartSec=1' >> /lib/systemd/system/camilladsp.service
sudo echo 'User=mgt' >> /lib/systemd/system/camilladsp.service
sudo echo 'Group=mgt' >> /lib/systemd/system/camilladsp.service
sudo echo 'CPUSchedulingPolicy=fifo' >> /lib/systemd/system/camilladsp.service
sudo echo 'CPUSchedulingPriority=99' >> /lib/systemd/system/camilladsp.service
sudo echo '[Install]' >> /lib/systemd/system/camilladsp.service
sudo echo 'WantedBy=graphical.target' >> /lib/systemd/system/camilladsp.service
sudo systemctl enable camilladsp
# Install pycamilladsp
sudo git clone https://github.com/HEnquist/pycamilladsp /home/mgt/camilladsp/pycamilladsp
cd /home/mgt/camilladsp/pycamilladsp
pip3 install .
cd /home/mgt/camilladsp
# Install pycamilladsp-plot
sudo git clone https://github.com/HEnquist/pycamilladsp-plot /home/mgt/camilladsp/pycamilladsp-plot
cd /home/mgt/camilladsp/pycamilladsp-plot
pip3 install .
cd /home/mgt/camilladsp
# Install camillagui
wget https://github.com/HEnquist/camillagui-backend/releases/download/v1.0.1/camillagui.zip
unzip camillagui.zip -d camillagui
sed -i 's%~/camilladsp/default_config.yml%~/camilladsp/active_config.yml%g' /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i 's%update_config_symlink: false%update_config_symlink: true%g' /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i 's%~/camilladsp/camilladsp.log%~/camilladsp/logs/camilladsp.log%g' /home/mgt/camilladsp/camillagui/config/camillagui.yml
mkdir /home/mgt/camilladsp/logs
touch /home/mgt/camilladsp/logs/camilladsp.log
sudo ln -s /home/mgt/camilladsp/configs/camilladsp.yml /home/mgt/camilladsp/active_config.yml
sudo rm camillagui.zip
# Setup camillagui service
sudo echo '[Unit]' >> /lib/systemd/system/camillagui.service
sudo echo 'Description=AVDSP Backend and GUI (Powered by CamillaDSP Backend & GUI)' >> /lib/systemd/system/camillagui.service
sudo echo 'After=multi-user.target' >> /lib/systemd/system/camillagui.service
sudo echo '[Service]' >> /lib/systemd/system/camillagui.service
sudo echo 'User=mgt' >> /lib/systemd/system/camillagui.service
sudo echo 'Type=idle' >> /lib/systemd/system/camillagui.service
sudo echo 'ExecStart=python3 /home/mgt/camilladsp/camillagui/main.py' >> /lib/systemd/system/camillagui.service
sudo echo '[Install]' >> /lib/systemd/system/camillagui.service
sudo echo 'WantedBy=multi-user.target' >> /lib/systemd/system/camillagui.service
sudo systemctl enable camillagui
# Assign CPU for irexec
sudo sed -i '/ExecStart=/c\ExecStart=taskset -c 2 /usr/bin/irexec /etc/lirc/irexec.lircrc\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=99' /lib/systemd/system/irexec.service
# Install USB Audio Gadget
sudo echo 'dtoverlay=dwc2' >> /boot/armbianEnv.txt
sudo echo 'options g_audio c_chmask=3 p_chmask=3 c_srate=48000 p_srate=48000 c_ssize=2 p_ssize=2 iProduct="AVDSP USB Gadget"' >> /etc/modprobe.d/g_audio.conf
sudo echo 'g_audio' >> /etc/modules-load.d/g_audio.conf
sudo echo 'dwc2' >> /etc/modules-load.d/dwc2.conf
# Change ownership of camilladsp folder to mgt
sudo chown -R mgt:mgt /home/mgt/camilladsp
# Copy prebuilt camilladsp
sudo sshpass -p $PASSWORD_SSH scp -p22 mgt@192.168.5.17:/home/mgt/camilladsp/camilladsp /home/mgt/camilladsp/
sudo chown -R mgt:mgt /home/mgt/camilladsp
# Remove installation script
sudo rm /home/mgt/Cam_Ins.sh
# Clean up system
sudo apt autoremove -y
# Change hostname
sudo truncate -s 0 /etc/hostname
sudo echo '$DSP_NAME' >> /etc/hostname
sudo sed -i 's%orangepilite%$DSP_NAME%g' /etc/hosts
# Reboot system
echo "AVDSP installation completed, reboot in 30 seconds" # Đọc cách làm đếm ngược đơn giản
sleep 30
sudo reboot
