#!/bin/sh
# Installation script for BR_glibc_systemd builds

# 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
clear
echo "----------------------------------------------------------------------------------"
echo "-                       Welcome to AVDSP installation                            -"
echo "----------------------------------------------------------------------------------"
echo "Check sound card settings with alsamixer if no audio after this installation"
read -p "Please enter name for new DSP system then press Enter: " DSP_NAME
echo "Please enter password for packages server connection then press Enter: "
stty -echo
read PASSWORD_SSH
stty echo
echo "$DSP_NAME is installing, please wait"
CURRENT_HOST_NAME=$(hostname)

# Install basic system
cd /etc/
#wget http://mgt.dynu.net:8008/CAMILLADSP/chrony.conf
#curl -L -o /etc/chrony.conf http://mgt.dynu.net:8008/CAMILLADSP/chrony.conf
#systemctl restart chrony
#echo "Updating system clock"
#sleep 5s

mv /home/mgt/ath9k_htc /lib/firmware/

#sed -i "s/#Storage=auto/Storage=none/g" /etc/systemd/journald.conf
#sed -i "s/SystemMaxUse=/SystemMaxUse=10M/g" /etc/systemd/journald.conf

# Reserve CPU
#CURRENT_APPEND=$(awk '/append / {print $0}' /boot/extlinux/extlinux.conf)
#sed -i "s%$CURRENT_APPEND%$CURRENT_APPEND isolcpus=1,2%g" /boot/extlinux/extlinux.conf
sed  -i '4s/$/ isolcpus=1,2/' /boot/extlinux/extlinux.conf

# Minimize boot time
systemctl disable fail2ban.service
systemctl disable haveged.service
systemctl disable inadyn.service
systemctl disable triggerhappy.service
systemctl disable systemd-network-generator.service
systemctl disable systemd-networkd-wait-online.service
systemctl disable avahi-dnsconfd

systemctl disable lircd-setup

#systemctl disable chrony

mkdir -p /home/mgt/temp
cd /home/mgt/temp
curl -L -o systemd_timers.zip http://mgt.dynu.net:8008/CAMILLADSP/systemd_timers.zip
unzip systemd_timers.zip
mv /home/mgt/temp/*.timer /etc/systemd/system/
rm -rf /home/mgt/temp
systemctl enable sshd.timer
systemctl enable wpa_supplicant.timer
systemctl enable avahi-daemon.timer
systemctl enable systemd-networkd.timer
systemctl enable systemd-resolved.timer
systemctl enable systemd-timesyncd.timer
systemctl enable lircd.timer
systemctl enable lircmd.timer
systemctl enable irexec.timer
systemctl enable alsa-state.timer
systemctl disable lircd.service
systemctl disable lircmd.service
systemctl disable irexec.service
systemctl disable avahi-daemon.service
systemctl disable systemd-networkd.service
systemctl disable systemd-resolved.service
systemctl disable systemd-timesyncd.service
systemctl disable alsa-state.service
systemctl disable sshd.service
systemctl disable wpa_supplicant.service


#systemctl mask lircd-setup
#sed -i "/After=/c\#After=systemd-sysusers.service systemd-hwdb-update.service\nStartLimitIntervalSec=0.01\nStartLimitBurst=771" /lib/systemd/system/systemd-udevd.service
#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
echo -e '# alsa loopback\nsnd\nsnd-timer\nsnd-pcm\nsnd-aloop' >> /etc/modules-load.d/aloop.conf

# Add alsa-ucm for Alsa restore
mkdir -p /home/mgt/temp
cd /home/mgt/temp
curl -L -o alsa-ucm-conf.tar.gz http://mgt.dynu.net:8008/CAMILLADSP/alsa-ucm-conf.tar.gz
gunzip alsa-ucm-conf.tar.gz
tar xvf alsa-ucm-conf.tar -C /usr/share/alsa --strip-components=1
cd /home/mgt/
rm -rf /home/mgt/temp

# Modify alsa-restore.service and alsa-state.service
#sed -i "s%Description=Save/Restore Sound Card State%Description=Save/Restore Sound Card State\nAfter=multi-user.target%g" /lib/systemd/system/alsa-restore.service
sed -i "s%Description=Save/Restore Sound Card State%Description=Save/Restore Sound Card State\nDefaultDependencies=no\nRequires=local-fs.target sound.target systemd-udev-trigger.service\nAfter=systemd-user-sessions.service%g" /lib/systemd/system/alsa-restore.service
sed -i "s%ExecStart=-/usr/sbin/alsactl restore%ExecStart=-/usr/sbin/alsactl restore -f /etc/alsa/asound.state%g" /lib/systemd/system/alsa-restore.service
sed -i "s%ExecStart=-/usr/sbin/alsactl store%ExecStart=-/usr/sbin/alsactl store -f /etc/alsa/asound.state%g" /lib/systemd/system/alsa-restore.service
sed -i "s%Description=Manage Sound Card State (restore and store)%Description=Manage Sound Card State (restore and store)\nAfter=multi-user.target%g" /lib/systemd/system/alsa-state.service

# Fix buildroot python3 compilation error
mv /usr/lib/python3.11/site-packages/pip/__pip-runner__.pyc /usr/lib/python3.11/site-packages/pip/__pip-runner__.py
mv /usr/lib/python3.11/site-packages/pip/_vendor/pep517/in_process/_in_process.pyc /usr/lib/python3.11/site-packages/pip/_vendor/pep517/in_process/_in_process.py

# Install camilladsp
mkdir -p /home/mgt/camilladsp
mkdir -p /home/mgt/camilladsp
mkdir -p /home/mgt/camilladsp/coeffs
mkdir -p /home/mgt/camilladsp/configs
echo 'devices:' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  samplerate: 48000' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  chunksize: 2048' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  queuelimit: 4' >> /home/mgt/camilladsp/configs/camilladsp.yml
echo '  adjust_period: 3' >> /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: 2048' >> /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
cd /home/mgt/camilladsp
curl -L -o /home/mgt/camilladsp/camilladsp http://mgt.dynu.net:8008/CAMILLADSP/camilladsp
chmod +x camilladsp

# Setup camilladsp service
echo '[Unit]' >> /lib/systemd/system/camilladsp.service
echo 'Description=AVDSP (Powered by CamillaDSP)' >> /lib/systemd/system/camilladsp.service
echo 'DefaultDependencies=no' >> /lib/systemd/system/camilladsp.service
echo 'Requires=sound.target systemd-journald.socket' >> /lib/systemd/system/camilladsp.service
echo '#Requires=local-fs.target sound.target systemd-journald.socket' >> /lib/systemd/system/camilladsp.service
echo '#Requires=local-fs.target sound.target systemd-udevd.service' >> /lib/systemd/system/camilladsp.service
echo '#Requires=local-fs.target sound.target systemd-udev-trigger.service' >> /lib/systemd/system/camilladsp.service
echo '#After=systemd-udev-trigger.service' >> /lib/systemd/system/camilladsp.service
echo 'StartLimitIntervalSec=0.1' >> /lib/systemd/system/camilladsp.service
echo 'StartLimitBurst=771' >> /lib/systemd/system/camilladsp.service
echo '[Service]' >> /lib/systemd/system/camilladsp.service
echo 'Type=simple' >> /lib/systemd/system/camilladsp.service
#echo 'ExecStart=taskset -c 1 /home/mgt/camilladsp/camilladsp -p 1234 /home/mgt/camilladsp/active_config.yml' >> /lib/systemd/system/camilladsp.service
echo 'ExecStart=taskset 2 /home/mgt/camilladsp/camilladsp -p 1234 /home/mgt/camilladsp/active_config.yml' >> /lib/systemd/system/camilladsp.service
echo 'Restart=always' >> /lib/systemd/system/camilladsp.service
echo 'RestartSec=1' >> /lib/systemd/system/camilladsp.service
echo 'User=mgt' >> /lib/systemd/system/camilladsp.service
echo 'Group=mgt' >> /lib/systemd/system/camilladsp.service
echo 'CPUSchedulingPolicy=fifo' >> /lib/systemd/system/camilladsp.service
echo 'CPUSchedulingPriority=99' >> /lib/systemd/system/camilladsp.service
echo '[Install]' >> /lib/systemd/system/camilladsp.service
echo 'WantedBy=multi-user.target' >> /lib/systemd/system/camilladsp.service
systemctl enable camilladsp

# Install pycamilladsp
cd /home/mgt/camilladsp
curl -L -o /home/mgt/camilladsp/pycamilladsp.zip http://mgt.dynu.net:8008/CAMILLADSP/pycamilladsp.zip
unzip pycamilladsp.zip
rm pycamilladsp.zip
cd pycamilladsp
pip3 install .
cd ..

# Install pycamilladsp-plot
cd /home/mgt/camilladsp
curl -L -o /home/mgt/camilladsp/pycamilladsp-plot.zip http://mgt.dynu.net:8008/CAMILLADSP/pycamilladsp-plot.zip
unzip pycamilladsp-plot.zip
rm pycamilladsp-plot.zip
cd pycamilladsp-plot
pip3 install .
cd ..

# Install camilladspgui
cd /home/mgt/camilladsp
#wget https://github.com/HEnquist/camillagui-backend/releases/download/v1.0.1/camillagui.zip
#wget http://mgt.dynu.net:8008/CAMILLADSP/camillagui.zip
curl -L -o /home/mgt/camilladsp/camillagui.zip http://mgt.dynu.net:8008/CAMILLADSP/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%~/camilladsp/camilladsp.log%~/camilladsp/logs/camilladsp.log%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i "s%~/camilladsp/configs%/home/mgt/camilladsp/configs%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i "s%~/camilladsp/coeffs%/home/mgt/camilladsp/coeffs%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i "s%~/camilladsp/default_config.yml%/home/mgt/camilladsp/active_config.yml%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i "s%~/camilladsp/active_config.yml%/home/mgt/camilladsp/active_config.yml%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
sed -i "s%~/camilladsp/active_config.txt%/home/mgt/camilladsp/active_config.txt%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%/home/mgt/camilladsp/logs/camilladsp.log%g" /home/mgt/camilladsp/camillagui/config/camillagui.yml
mkdir /home/mgt/camilladsp/logs
touch /home/mgt/camilladsp/logs/camilladsp.log
ln -s /home/mgt/camilladsp/configs/camilladsp.yml /home/mgt/camilladsp/active_config.yml
rm camillagui.zip

# Setup camillagui service
echo '[Unit]' >> /lib/systemd/system/camillagui.service
echo 'Description=AVDSP Backend and GUI (Powered by CamillaDSP Backend & GUI)' >> /lib/systemd/system/camillagui.service
echo 'After=multi-user.target' >> /lib/systemd/system/camillagui.service
echo '[Service]' >> /lib/systemd/system/camillagui.service
echo 'User=mgt' >> /lib/systemd/system/camillagui.service
echo 'Type=idle' >> /lib/systemd/system/camillagui.service
echo 'ExecStart=python3 /home/mgt/camilladsp/camillagui/main.py' >> /lib/systemd/system/camillagui.service
echo '[Install]' >> /lib/systemd/system/camillagui.service
echo 'WantedBy=multi-user.target' >> /lib/systemd/system/camillagui.service
systemctl enable camillagui

# Assign CPU for lirc, irexec
sed -i "/Wants=lircd-setup.service/c\#Wants=lircd-setup.service" /lib/systemd/system/lircd.service
sed -i "/After=network.target/c\After=network.target" /lib/systemd/system/lircd.service
#sed -i "/ExecStart=/c\ExecStart=taskset -c 2 /usr/sbin/lircd --nodaemon\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=99" /lib/systemd/system/lircd.service
sed -i "/ExecStart=/c\ExecStart=taskset 4 /usr/sbin/lircd --nodaemon\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=99" /lib/systemd/system/lircd.service
#sed -i "/ExecStart=/c\ExecStart=taskset -c 2 /usr/bin/irexec /etc/lirc/irexec.lircrc\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=98"
sed -i "/ExecStart=/c\ExecStart=taskset 4 /usr/bin/irexec /etc/lirc/irexec.lircrc\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=98" /lib/systemd/system/irexec.service
sed -i "/ExecStart=/c\ExecStart=taskset 4 /usr/sbin/lircmd --nodaemon\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=99" /lib/systemd/system/lircmd.service

# Install USB Audio Gadget
# echo 'dtoverlay=dwc2' >> /boot/armbianEnv.txt
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
echo 'g_audio' >> /etc/modules-load.d/g_audio.conf
echo 'dwc2' >> /etc/modules-load.d/dwc2.conf

# Copy preset configs
#cd /home/mgt/camilladsp/configs
#wget http://mgt.dynu.net:8008/CAMILLADSP/configs.zip
#unzip -o configs.zip
#rm /home/mgt/camilladsp/active_config.yml
#ln -s /home/mgt/camilladsp/configs/R_S2_S2C_BW804_v1.yml /home/mgt/camilladsp/active_config.yml
#rm configs.zip
#cd ..

# Change ownership of camilladsp folder to mgt
chown -R mgt:mgt /home/mgt/camilladsp

# Remove installation script
rm /home/mgt/Cam_Ins.sh

# Change hostname
truncate -s 0 /etc/hostname
echo "$DSP_NAME" >> /etc/hostname
sed -i "s%$CURRENT_HOST_NAME%$DSP_NAME%g" /etc/hosts

# Reboot system
echo "$DSP_NAME installation completed, reboot in 10 seconds" # Đọc cách làm đếm ngược đơn giản
sleep 10
reboot
