#!/bin/bash
# NOTE installs nginx, postgresql ,python , php , node  , quota for homes 
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


true
# shellcheck source=lib.sh
if ! source <(curl -sL https://code.hbues.net/bash/core.sh)
then
    source <(curl -sL https://code.hbues.net/bash/core.sh)
fi

printf "Install needed dependencies \n"
install_if_not lshw
install_if_not net-tools
install_if_not apt-utils

printf "Nice to have dependencies \n"
install_if_not iputils-ping

printf "We don't want automatic updates since they might fail (we use our own script) \n"
if is_this_installed unattended-upgrades
then
    sudo apt-get purge unattended-upgrades -y
    sudo apt-get autoremove -y
    sudo 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 package for crontab
install_if_not cron

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

printf "Install development base \n"
install_if_not build-essential 
install_if_not libssl-dev 
install_if_not zlib1g-dev 
install_if_not libncurses5-dev 
install_if_not libncursesw5-dev 
install_if_not libreadline-dev 
install_if_not libsqlite3-dev 
install_if_not libgdbm-dev 
install_if_not libdb5.3-dev 
install_if_not libbz2-dev 
install_if_not autoconf 
install_if_not automake 
install_if_not libtool 
install_if_not cmake 
install_if_not python3 
install_if_not python3-dev 
install_if_not python3-pip 
install_if_not python3-venv 
install_if_not git 
install_if_not vim-nox 
install_if_not libssl-dev 
install_if_not libcurl4-openssl-dev 
install_if_not libxml2-dev 
install_if_not libsqlite3-dev 
install_if_not gdb 
install_if_not curl 
install_if_not valgrind 
install_if_not libc6-dev 
install_if_not linux-libc-dev 
install_if_not bat 
install_if_not kitty 
install_if_not alacritty 
#install_if_not nfs-kernel-server 
#install_if_not quota

install_if_not mariadb-server










install_if_not postgresql
install_if_not postgresql-contrib
install_if_not pgloader

#sudo apt update
# config postgresql
#
sudo systemctl start postgresql
sudo systemctl enable postgresql



#download pass repo 
#
 






# 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


