#!/bin/sh
# Installation script for BR_glibc_busybox 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
cd ..
/etc/init.d/S49chrony restart
mv /etc/init.d/S01syslogd /etc/init.d/D_S01syslogd
mv /etc/init.d/S02klogd /etc/init.d/D_S02klogd
mv /etc/init.d/S13irqbalance /etc/init.d/D_S13irqbalance
mv /etc/init.d/S21haveged /etc/init.d/D_S31haveged
mv /etc/init.d/S60fail2ban /etc/init.d/D_S60fail2ban
mv /etc/init.d/S70inadyn /etc/init.d/D_S70inadyn
mv /etc/init.d/S10triggerhappy /etc/init.d/D_S39triggerhappy
mv /etc/init.d/S05avahi-setup.sh /etc/init.d/S35avahi-setup.sh
mv /etc/init.d/S30dbus /etc/init.d/D_S20dbus
mv /etc/init.d/S02sysctl /etc/init.d/D_S02sysctl
echo "Updating system clock"
# sleep 1s

# 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

# Create Loopback
echo '#!/bin/sh' >> /etc/init.d/S91modules
echo '#modprobe snd &' >> /etc/init.d/S91modules
echo 'modprobe snd-timer &' >> /etc/init.d/S91modules
echo '#modprobe snd-pcm &' >> /etc/init.d/S91modules
echo 'modprobe snd-aloop &' >> /etc/init.d/S91modules
echo 'ir-keytable -p all &' >> /etc/init.d/S91modules
echo 'modprobe 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/init.d/S91modules
mv /etc/init.d/S91modules /etc/init.d/S12modules
chmod +x /etc/init.d/S12modules

# 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

# 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/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
#wget http://mgt.dynu.net:8008/CAMILLADSP/camilladsp /home/mgt/camilladsp/camilladsp
curl -L -o /home/mgt/camilladsp/camilladsp http://mgt.dynu.net:8008/CAMILLADSP/camilladsp
chmod +x camilladsp

# Create camilladsp start/stop script
# With current BR_OPZ_config CPU (run on OPC) number for taskset should be (count from 0) 1-0, 2-1, 3-0, 4-2, 5-0/5-2, 6-1, 7-0, 8-3
echo '#!/bin/sh' >> /etc/init.d/S11camilladsp
echo '' >> /etc/init.d/S11camilladsp
echo 'start() {' >> /etc/init.d/S11camilladsp
echo '        printf "Check if CamillaDSP running: " &' >> /etc/init.d/S11camilladsp
echo '        CamillaDSP_State=$(pgrep -f '/camilladsp/camilladsp') &' >> /etc/init.d/S11camilladsp
echo '        [ "$CamillaDSP_State" = "" ] && echo -e "\nStating CamillaDSP" || echo -e "\nCamillaDSP is running" &' >> /etc/init.d/S11camilladsp
echo '        (while [ "$CamillaDSP_State" = "" ]; do' >> /etc/init.d/S11camilladsp
echo '        chrt 99 start-stop-daemon -S -c mgt -n camilladsp --startas /usr/bin/taskset 2 /home/mgt/camilladsp/camilladsp /home/mgt/camilladsp/active_config.yml -- -p 1234 -o /home/mgt/camilladsp/logs/cammilladsp.log -l off' >> /etc/init.d/S11camilladsp
echo '        CamillaDSP_State=$(pgrep -f '/camilladsp/camilladsp')' >> /etc/init.d/S11camilladsp
echo '        done) &' >> /etc/init.d/S11camilladsp
echo '        [ $? = 0 ] && echo "OK" || echo "FAIL"' >> /etc/init.d/S11camilladsp
echo '}' >> /etc/init.d/S11camilladsp
echo 'stop() {' >> /etc/init.d/S11camilladsp
echo '        echo "Can not stop/restart CamillaDSP due to setting as system service. Reboot system if needed" &' >> /etc/init.d/S11camilladsp
echo '        #killall camilladsp' >> /etc/init.d/S11camilladsp
echo '        #[ $? = 0 ] && echo "OK" || echo "FAIL"' >> /etc/init.d/S11camilladsp
echo '}' >> /etc/init.d/S11camilladsp
echo 'restart() {' >> /etc/init.d/S11camilladsp
echo '        stop' >> /etc/init.d/S11camilladsp
echo '        start' >> /etc/init.d/S11camilladsp
echo '}' >> /etc/init.d/S11camilladsp
echo 'case "$1" in' >> /etc/init.d/S11camilladsp
echo '  start)' >> /etc/init.d/S11camilladsp
echo '        start' >> /etc/init.d/S11camilladsp
echo '        ;;' >> /etc/init.d/S11camilladsp
echo '  stop)' >> /etc/init.d/S11camilladsp
echo '        stop' >> /etc/init.d/S11camilladsp
echo '        ;;' >> /etc/init.d/S11camilladsp
echo '  restart|reload)' >> /etc/init.d/S11camilladsp
echo '        restart' >> /etc/init.d/S11camilladsp
echo '        ;;' >> /etc/init.d/S11camilladsp
echo '  *)' >> /etc/init.d/S11camilladsp
echo '        echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/S11camilladsp
echo '        exit 1' >> /etc/init.d/S11camilladsp
echo 'esac' >> /etc/init.d/S11camilladsp
echo 'exit $?' >> /etc/init.d/S11camilladsp
chmod +x /etc/init.d/S11camilladsp
# mv /etc/init.d/S11camilladsp /etc/init.d/S11camilladsp

# Install pycamilladsp
cd /home/mgt/camilladsp
#sudo git clone https://github.com/HEnquist/pycamilladsp
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
#sudo git clone https://github.com/HEnquist/pycamilladsp-plot
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 -p /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

# Create camilladspgui start/stop script
echo '#!/bin/sh' >> /etc/init.d/S92camilladspgui
echo '' >> /etc/init.d/S92camilladspgui
echo 'start() {' >> /etc/init.d/S92camilladspgui
echo '        printf "Starting CamillaDSP_GUI: "' >> /etc/init.d/S92camilladspgui
echo '        #start-stop-daemon -S -c mgt -n python3 --startas /usr/bin/taskset 4 /usr/bin/python3 /home/mgt/camilladsp/camillagui/main.py &' >> /etc/init.d/S92camilladspgui
echo '        start-stop-daemon -S -c mgt -n python3 --startas /usr/bin/python3 /home/mgt/camilladsp/camillagui/main.py &' >> /etc/init.d/S92camilladspgui
echo '        [ $? = 0 ] && echo "OK" || echo "FAIL"' >> /etc/init.d/S92camilladspgui
echo '}' >> /etc/init.d/S92camilladspgui
echo 'stop() {' >> /etc/init.d/S92camilladspgui
echo '        printf "Stopping CamillaDSP_GUI: "' >> /etc/init.d/S92camilladspgui
echo '        pkill -f camillagui/main.py' >> /etc/init.d/S92camilladspgui
echo '        #killall python3' >> /etc/init.d/S92camilladspgui
echo '        [ $? = 0 ] && echo "OK" || echo "FAIL"' >> /etc/init.d/S92camilladspgui
echo '}' >> /etc/init.d/S92camilladspgui
echo 'restart() {' >> /etc/init.d/S92camilladspgui
echo '        stop' >> /etc/init.d/S92camilladspgui
echo '        start' >> /etc/init.d/S92camilladspgui
echo '}' >> /etc/init.d/S92camilladspgui
echo 'case "$1" in' >> /etc/init.d/S92camilladspgui
echo '  start)' >> /etc/init.d/S92camilladspgui
echo '        start' >> /etc/init.d/S92camilladspgui
echo '        ;;' >> /etc/init.d/S92camilladspgui
echo '  stop)' >> /etc/init.d/S92camilladspgui
echo '        stop' >> /etc/init.d/S92camilladspgui
echo '        ;;' >> /etc/init.d/S92camilladspgui
echo '  restart|reload)' >> /etc/init.d/S92camilladspgui
echo '        restart' >> /etc/init.d/S92camilladspgui
echo '        ;;' >> /etc/init.d/S92camilladspgui
echo '  *)' >> /etc/init.d/S92camilladspgui
echo '        echo "Usage: $0 {start|stop|restart}"' >> /etc/init.d/S92camilladspgui
echo '        exit 1' >> /etc/init.d/S92camilladspgui
echo 'esac' >> /etc/init.d/S92camilladspgui
echo 'exit $?' >> /etc/init.d/S92camilladspgui
chmod +x /etc/init.d/S92camilladspgui
mv /etc/init.d/S92camilladspgui /etc/init.d/S99camilladspgui

## 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 -c 2 /usr/bin/irexec /etc/lirc/irexec.lircrc\nCPUSchedulingPolicy=fifo\nCPUSchedulingPriority=98"
## 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

# 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
