if ! [[ "8 9 10 11 12" == *"$(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

printf "hbues.net post installation script of kvm vm \n"


sudo apt install \
        build-essential \
        libssl-dev \
        zlib1g-dev \
        libncurses5-dev \
	libncursesw5-dev \
	libreadline-dev \
	libsqlite3-dev \
	libgdbm-dev \
	libdb5.3-dev \
	libbz2-dev \
	autoconf \
	automake \
	libtool \
	cmake \
	python3 \
	python3-dev \
	python3-pip \
	python3-venv \
	git \
        vim-nox \
	libssl-dev \
	libcurl4-openssl-dev \
	libxml2-dev \
	libsqlite3-dev \
	gdb \
        curl \
	postgresql \
       	postgresql-contrib \
	pgloader \
	valgrind \
	libc6-dev \
	linux-libc-dev \
	bat \
	kitty \
	alacritty \
	apt-transport-https \
	gnupg \
	lsb-release \
	gpg \
	nodejs \
	npm


printf "Configure git\n"
git_name="support"
git_mail='support@hbues.net'
git_host='source'
git_host_name='git.baizabal.xyz'
git_host_port='23231'
git_port='23231'
git_branch='master'
git_repo='tunnpy'
git_repo_module='rpc'
git_repo_module_branch='gst_api'
git_submodule_dir="restapi/bridge"
git_submodule_base="restapi"
git_submodule_type="bridge"


git config --global user.name "${git_name}"
git config --global user.email "${git_mail}"

mkdir -p $HOME/.ssh
printf " Host ${git_host} \n HostName ${git_host_name} \n Port ${git_port} \n User ${git_name} \n" > $HOME/.ssh/config

#source ~/.bashrc
#ssh-keygen -t ed25519 -C "${git_mail}" 
#ssh-copy-id ${git_host_name}



echo "Installing elasticsearch ..."

if ! dpkg -s 'elasticsearch' &>/dev/null; then


    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/9.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-9.x.list

fi



echo "Installing redis"
if ! dpkg -s 'redis' &>/dev/null; then
    echo "Add keyirings"
    curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
    sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg

    echo "Add repositories"

    echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
fi

echo "Configure Postgresql"

sudo sed -i "/max_connections/c\max_connections = 2000" /etc/postgresql/15/main/postgresql.conf 
sudo sed -i "/shared_buffers/c\shared_buffers = 2GB" /etc/postgresql/15/main/postgresql.conf 
sudo sed -i "/temp_buffers/c\temp_buffers = 256MB"   /etc/postgresql/15/main/postgresql.conf 
sudo sed -i "/work_mem/c\work_mem = 10MB" /etc/postgresql/15/main/postgresql.conf 
sudo sed -i "/max_stack_depth/c\max_stack_depth = 5MB" /etc/postgresql/15/main/postgresql.conf 

sudo systemctl restart postgresql

sudo apt update && sudo apt install elasticsearch redis

sudo systemctl start postgresql && sudo systemctl enable postgresql

sudo systemctl enable redis-server && sudo systemctl start redis-server

echo "install zammad"
if ! dpkg -s 'zammad' &>/dev/null ; then
    curl -fsSL https://dl.packager.io/srv/zammad/zammad/key | gpg --dearmor | sudo tee /etc/apt/keyrings/pkgr-zammad.gpg> /dev/null
    echo "deb [signed-by=/etc/apt/keyrings/pkgr-zammad.gpg] https://dl.packager.io/srv/deb/zammad/zammad/stable/debian 12 main"| sudo tee /etc/apt/sources.list.d/zammad.list > /dev/null

fi
sudo apt update && sudo apt upgrade --yes && sudo apt install zammad
