#!/bin/bash

apt-get update && apt-get install -y curl dialog htop net-tools

curl -Lo /usr/bin/astra https://cdn.cesbo.com/astra/archive/5.64/x86_64/astra
chmod +x /usr/bin/astra
astra init
systemctl enable astra
mkdir -p /etc/astra

LICENSE=$(dialog --title "Astra License Setup" --inputbox "A valid license is required to launch Astra on your server. You may obtain a demo license for free or purchase a subscription on Cesbo Astra website: https://cesbo.com/astra-license\n\nPlease paste your license key here:" 15 70 3>&1 1>&2 2>&3 3>&-)
echo "$LICENSE" > /etc/astra/license.txt
if [ -s /etc/astra/license.txt ]; then
    dialog --title "License Installed" --msgbox "Your Astra license has been successfully installed." 6 50
else
    dialog --title "License Installation Failed" --msgbox "Failed to install the Astra license. Please check your input and try again." 6 50
    exit 1
fi

echo '{
  "users": {
    "admin": {
      "cipher": "3fed7a346e430ea4c2aa10250928f4de",
      "created": 1711897449,
      "enable": true,
      "firstrun": true,
      "type": 1
    }
  },
  "gid": 466560,
  "dvb_tune": [' > /etc/astra/astra.conf

NUM_TUNERS=$(dialog --title "Configure SAT>IP vTuners" --inputbox "Enter the number of SAT>IP vTuners to configure (1-8):" 8 50 3>&1 1>&2 2>&3 3>&-)
if ! [[ "$NUM_TUNERS" =~ ^[1-8]$ ]]; then
    dialog --title "Input Error" --msgbox "Invalid entry. Please enter a number between 1 and 8." 6 50
    exit 1
fi

for i in $(seq 1 $NUM_TUNERS); do
    SATIP_IP=$(dialog --title "SAT>IP vTuner $i Configuration" --inputbox "Enter the IPv4 address for SAT>IP vTuner $i:\nFormat: x.x.x.x where x is 0-255" 8 50 3>&1 1>&2 2>&3 3>&-)
    
    SATIP="http://${SATIP_IP}:8875"

    while true; do
        FREQUENCY=$(dialog --title "Tuner $i Frequency" --inputbox "Enter the Frequency for Tuner $i (Range: 950 - 40000 MHz):" 8 60 3>&1 1>&2 2>&3 3>&-)
        if [[ "$FREQUENCY" -ge 950 && "$FREQUENCY" -le 40000 ]]; then
            break
        else
            dialog --title "Invalid Frequency" --msgbox "The frequency entered is out of range. Please enter a value between 950 and 40000 MHz." 6 50
        fi
    done

    while true; do
        SYMBOLRATE=$(dialog --title "Tuner $i Symbol Rate" --inputbox "Enter the Symbol Rate for Tuner $i (Range: 1000 - 50000):" 8 60 3>&1 1>&2 2>&3 3>&-)
        if [[ "$SYMBOLRATE" -ge 1000 && "$SYMBOLRATE" -le 50000 ]]; then
            break
        else
            dialog --title "Invalid Symbol Rate" --msgbox "The symbol rate entered is out of range. Please enter a value between 1000 and 50000." 6 50
        fi
    done
	

    TYPE=$(dialog --menu "Select Tuner Type for Tuner $i:" 15 50 4 \
        "S" "DVB-S" \
        "S2" "DVB-S2" \
        "T" "DVB-T" \
        "T2" "DVB-T2" 3>&1 1>&2 2>&3 3>&-)

		    FEC=$(dialog --menu "Select FEC for Tuner $i:" 20 60 15 \
		        "1/2" "1/2" \
		        "2/3" "2/3" \
		        "3/4" "3/4" \
		        "4/5" "4/5" \
		        "5/6" "5/6" \
		        "6/7" "6/7" \
		        "7/8" "7/8" \
		        "8/9" "8/9" \
		        "3/5" "3/5" \
		        "9/10" "9/10" \
		        "2/5" "2/5" 3>&1 1>&2 2>&3 3>&-)

		    MODULATION=$(dialog --menu "Select Modulation for Tuner $i:" 20 60 15 \
		        "QPSK" "QPSK" \
                "PSK8" "PSK8" \
		        "16-QAM" "16-QAM" \
		        "32-QAM" "32-QAM" \
		        "64-QAM" "64-QAM" \
		        "128-QAM" "128-QAM" \
		        "256-QAM" "256-QAM" \
		        "VSB8" "VSB8" \
		        "VSB16" "VSB16" \
		        "APSK16" "APSK16" \
		        "APSK32" "APSK32" \
		        "DQPSK" "DQPSK" \
		        "APSK64" "APSK64" \
		        "APSK128" "APSK128" \
		        "APSK256" "APSK256" 3>&1 1>&2 2>&3 3>&-)

		    POLARIZATION=$(dialog --menu "Select Polarization for Tuner $i:" 20 60 4 \
		        "H" "Horizontal" \
		        "V" "Vertical" \
		        "R" "Right" \
		        "L" "Left" 3>&1 1>&2 2>&3 3>&-)

		    if [ $i -gt 1 ]; then
		        echo "," >> /etc/astra/astra.conf
		    fi
		    echo "      {
		      \"virtual\": \"satip\",
		      \"frequency\": \"$FREQUENCY\",
		      \"type\": \"$TYPE\",
		      \"budget\": true,
		      \"id\": \"vTuner-$i\",
		      \"fec\": \"$FEC\",
		      \"enable\": true,
		      \"symbolrate\": \"$SYMBOLRATE\",
		      \"polarization\": \"$POLARIZATION\",
		      \"satip\": \"$SATIP\",
		      \"name\": \"SAT>IP vTuner $i\",
		      \"modulation\": \"$MODULATION\",
		      \"adapter\": \"0\"
		    }" >> /etc/astra/astra.conf
		done

		echo "  ]
		}" >> /etc/astra/astra.conf

		systemctl restart astra

		clear

		echo "Astra has been successfully installed and started."
		echo "Access the Astra web interface at http://your-server-address:8000."
		echo "Default login and password are 'admin'. Change the default password to secure your installation."