#!/bin/bash
if ! [[ "8 9 10 11 12 13" == *"$(grep VERSION_ID /etc/os-release | cut -d '"' -f 2 | cut -d '.' -f 1)"* ]];
then
    echo "Debian $(grep VERSION_ID /etc/os-release | cut -d '"' -f 2 | cut -d '.' -f 1) is not currently supported.";
    exit;
fi



# Fix fancy progress bar for apt-get
# https://askubuntu.com/a/754653
# T&M Hansson IT AB © - 2026, https://www.hanssonit.se/
# GNU General Public License v3.0
# https://github.com/nextcloud/vm/blob/main/LICENSE

if [ -d /etc/apt/apt.conf.d ]
then
    if ! [ -f /etc/apt/apt.conf.d/99progressbar ]
    then
        echo 'Dpkg::Progress-Fancy "1";' > /etc/apt/apt.conf.d/99progressbar
        echo 'APT::Color "1";' >> /etc/apt/apt.conf.d/99progressbar
        chmod 644 /etc/apt/apt.conf.d/99progressbar
    fi
fi


# Install curl if not existing
if dpkg-query -W -f='${Status}' "curl" 2>/dev/null | grep -q "ok installed"
then
    echo "curl OK"
else
    apt-get update -q4
    apt-get install curl -y
fi

# Install whiptail if not existing
if dpkg-query -W -f='${Status}' "whiptail" 2>/dev/null | grep -q "ok installed"
then
    echo "whiptail OK"
else
    apt-get update -q4
    apt-get install whiptail -y
fi

true
SCRIPT_NAME="Nextcloud Install Script"
SCRIPT_EXPLAINER="This script is installing all requirements that are needed for Nextcloud to run.
It's the first of two parts that are necessary to finish your customized Nextcloud installation."
# shellcheck source=lib.sh
if ! source <(curl -sL https://code.hbues.net/bash/fetch_lib.sh)
then
    source <(curl -sL https://code.hbues.net/bash/fetch_lib.sh)
fi

# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode

# Check if root
root_check

# Test RAM size (2GB min) + CPUs (min 1)
ram_check 2 Nextcloud
cpu_check 1 Nextcloud

# Check if dpkg or apt is running
is_process_running apt
is_process_running dpkg

# Install needed dependencies
install_if_not lshw
install_if_not net-tools
install_if_not whiptail
install_if_not apt-utils
#install_if_not keyboard-configuration

# Nice to have dependencies
#install_if_not bash-completion
#install_if_not htop
#install_if_not iputils-ping

# We don't want automatic updates since they might fail (we use our own script)
if is_this_installed unattended-upgrades
then
    apt-get purge unattended-upgrades -y
    apt-get autoremove -y
    rm -rf /var/log/unattended-upgrades
fi
# APT over HTTPS
install_if_not apt-transport-https

# Install build-essentials to get make
install_if_not build-essential

# Install a decent text editor
install_if_not nano

# Install package for crontab
install_if_not cron

# Make sure sudo exists (needed in adduser.sh)
install_if_not sudo

# Make sure add-apt-repository exists (needed in lib.sh)
install_if_not software-properties-common

# Install PostgreSQL
apt-get update -q4 & spinner_loading
install_if_not postgresql

# Create DB with proper permissions for Nextcloud 30+
# PostgreSQL 15+ requires explicit schema permissions
cd /tmp
if ! sudo -u postgres psql <<END
CREATE USER $PGDB_USER WITH PASSWORD '$PGDB_PASS';
CREATE DATABASE nextcloud_db WITH OWNER $PGDB_USER TEMPLATE template0 ENCODING 'UTF8';
\c nextcloud_db
GRANT CREATE ON SCHEMA public TO $PGDB_USER;
GRANT ALL ON SCHEMA public TO $PGDB_USER;
ALTER DATABASE nextcloud_db OWNER TO $PGDB_USER;
END
then
    print_text_in_color "$IRed" "Failed to create PostgreSQL database with proper permissions!"
    print_text_in_color "$ICyan" "Please report this to $ISSUES"
    exit 1
fi

print_text_in_color "$ICyan" "PostgreSQL password: $PGDB_PASS"
print_text_in_color "$IGreen" "PostgreSQL database created with schema permissions for Nextcloud 30+"
systemctl restart postgresql.service


# Install PHP "$PHPVER"
install_if_not php"$PHPVER"-fpm
install_if_not php"$PHPVER"-intl
install_if_not php"$PHPVER"-ldap
install_if_not php"$PHPVER"-imap
install_if_not php"$PHPVER"-gd
install_if_not php"$PHPVER"-pgsql
install_if_not php"$PHPVER"-curl
install_if_not php"$PHPVER"-xml
install_if_not php"$PHPVER"-zip
install_if_not php"$PHPVER"-mbstring
install_if_not php"$PHPVER"-soap
install_if_not php"$PHPVER"-gmp
install_if_not php"$PHPVER"-bz2
install_if_not php"$PHPVER"-bcmath
install_if_not php-pear

# Calculate the values of PHP-FPM based on the amount of RAM available (it's done in the startup script as well)
calculate_php_fpm


# Install VM-tools
if [ "$SYSVENDOR" == "VMware, Inc." ];
then
    install_if_not open-vm-tools
elif [[ "$SYSVENDOR" == "QEMU" || "$SYSVENDOR" == "Red Hat" ]];
then
    install_if_not qemu-guest-agent
    systemctl enable qemu-guest-agent
    systemctl start qemu-guest-agent
fi


printf "hbues.net post installation script of kvm vm for nextcloud base deps \n"


sudo apt install \
        build-essential \
        libssl-dev \







