#!/bin/bash

perform_smgplayer_install_operation() {
    # SMGplayer install
    current_user="smg"
    path="/home/smg"

    #update source.list
    tee /etc/apt/sources.list<<EOF
    deb http://deb.debian.org/debian/ bookworm main non-free-firmware
    deb-src http://deb.debian.org/debian/ bookworm main non-free-firmware

    deb http://security.debian.org/debian-security bookworm-security main non-free-firmware
    deb-src http://security.debian.org/debian-security bookworm-security main non-free-firmware

    # bookworm-updates, to get updates before a point release is made;
    # see https://www.debian.org/doc/manuals/debian-reference/ch02.en.html#_updates_and_backports
    deb http://deb.debian.org/debian/ bookworm-updates main non-free-firmware
    deb-src http://deb.debian.org/debian/ bookworm-updates main non-free-firmware
EOF
    echo "Updated etc/source.list!"

    sleep 3
    
    echo "Update PC & SMGPlayer install for CLI Linux PC"
   
    # Apps for first setup
    apt update
    apt install -y sudo unzip alsa-utils dos2unix psmisc net-tools sqlite3 iftop git mc htop acl curl usbutils build-essential gcc shc bluetooth bluez bluez-tools alsa-ucm-conf network-manager dialog whiptail ncdu httrack rsync tmux ufw bmon jq lsof

    # Add Permissions
    sudo usermod -a -G sudo,root,audio,tty,plugdev,bluetooth,video,netdev,dialout,systemd-journal "$current_user"

    #date & time settings
    date
    sudo hwclock -w
    sudo hwclock -r

    # Create a temporary directory
    temp_dir=$(mktemp -d)
    cd "$temp_dir" || exit

    # Downloading process
    echo "SMGPlayer installation"
    wget -O Linux-8712-x64_CLI.zip https://install.smg.com.tr/AIOSetup/Linux-8712-x64_CLI.zip
    if [ $? -ne 0 ]; then
        whiptail --title "Error" --msgbox "Failed to download Linux-8712-x64_CLI.zip" 10 50
        exit 1
    fi

    unzip Linux-8712-x64_CLI.zip
    mv smgplayer "$path/smgplayer"
    chown "$current_user:$current_user" -R "$path/smgplayer"
    chmod 755 -R "$path/smgplayer"

    # Config.ini edit
    file_to_edit="$path/smgplayer/config.ini"

    whiptail --title "SMGPlayer installation" --yesno "Do you want to edit the config file?" 10 50

    if [ $? -eq 0 ]; then
        # "Yes" - edit file
        sudo nano "$file_to_edit"
        whiptail --title "SMGPlayer installation" --msgbox "Config file edited" 10 50
    else
        # "No" - No edit
        whiptail --title "SMGPlayer installation" --msgbox "Config file not edited" 10 50
    fi

    # smg.service Process
    wget -O smgservice.zip https://install.smg.com.tr/AIOSetup/smgservice.zip
    if [ $? -ne 0 ]; then
        whiptail --title "Error" --msgbox "Failed to download smgservice.zip" 10 50
        exit 1
    fi

    unzip smgservice.zip
    sed -i "s/User=.*/User=$current_user/" smg.service
    sed -i "s|ExecStart=.*|ExecStart=$path/smgplayer/SMGPlayer|" smg.service
    chown root:root smg.service
    chmod 644 smg.service
    mv smg.service /etc/systemd/system

    # smg.service -> daemon process
    systemctl daemon-reload
    systemctl enable smg.service
    systemctl start smg.service
    whiptail --title "SMGPlayer installation" --msgbox "Service install complete" 10 50

    # Remove temporary files
    cd - || exit
    rm -rf "$temp_dir"

    # smgstats download & install
    wget -O "$path/smgstatsLinuxPC.zip" https://install.smg.com.tr/AIOSetup/smgstatsLinuxPC.zip
    if [ $? -ne 0 ]; then
        whiptail --title "Error" --msgbox "Failed to download smgstatsLinuxPC" 10 50
        exit 1
    fi

    unzip "$path/smgstatsLinuxPC.zip" -d "$path"
    if [ $? -ne 0 ]; then
        whiptail --title "Error" --msgbox "Failed to unzip smgstatsLinuxPC" 10 50
        exit 1
    fi

    chmod 755 "$path/smgstatsLinuxPC"
    chown root:root "$path/smgstatsLinuxPC"
    rm -rf "$path/smgstatsLinuxPC.zip"

    mv "$path/smgstatsLinuxPC" /usr/bin/smgstats

    whiptail --msgbox "SMGplayer installation completed" 8 60
}

perform_smgplayer_install_operation
