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"

git_name="development"
git_mail='development@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='tms_demo'
git_submodule_dir="restapi/bridge"
git_submodule_base="restapi"
git_submodule_type="bridge"

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 \
	valgrind \
	libc6-dev \
	linux-libc-dev \
	bat \
	kitty \
	alacritty 


#mssqldrv='/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.6.1'

#curl --proto '=https' --tlsv1.2 -sSf https://baizabal.xyz/downloads/Bash/gst_api.sh | bash
#
pkg='msodbcsql17'

if ! dpkg -s $pkg &>/dev/null; then
      printf "$pkg is not installed \n"

    # Download the package to configure the Microsoft repo
    curl -sSL -O https://packages.microsoft.com/config/debian/$(grep VERSION_ID /etc/os-release | cut -d '"' -f 2 | cut -d '.' -f 1)/packages-microsoft-prod.deb
    # Install the package
    sudo dpkg -i packages-microsoft-prod.deb
    # Delete the file
    rm packages-microsoft-prod.deb
    
    sudo apt-get update
    sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
    # optional: for bcp and sqlcmd
    sudo ACCEPT_EULA=Y apt-get install -y mssql-tools
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    # optional: for unixODBC development headers
    sudo apt-get install -y unixodbc-dev
    # optional: kerberos library for debian-slim distributions
    sudo apt-get install -y libgssapi-krb5-2

fi

printf "Configure git\n"


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}

# Clone repos 
cd $HOME

if [[ ! $(which tunnpy) ]] &>/dev/null; then
   # printf "NO TUNNPY : $(which tunnpy)"
    #is safe remove dirs
#&>/dev/null; then
        

    printf "Clone main app :tunnpy master branch\n"
    #git clone -b "${git_branch}" "ssh://${git_host_name}:${git_host_port}/${git_repo}.git"
    git clone -b ${git_branch} "ssh://${git_host_name}:${git_host_port}/${git_repo}.git"

    cd ${HOME}/${git_repo} 
    # check for submodules dir structure
    if [[ ! -f ${git_submodule_base} ]]; then	
	mkdir -p ${git_submodule_base}
    fi
    #cd ${HOME}/${git_repo}/${git_submodule_dir}
    print "INSTALLING SUBMODULE:"
    printf "Add Submodule \n"
    git -C ${git_submodule_base} submodule add  -b "${git_repo_module_branch}" "ssh://${git_host_name}:${git_host_port}/${git_repo_module}.git" "${git_submodule_type}" 

    #Update repo and submodules
#    git -C $HOME/${git_repo}/ pull 
#    git -C $HOME/${git_repo}/ submodule update --remote
fi

# if already has a submodule
#git clone -b ${git_branch} --recurse-submodules ${git_host}:/${git_repo}.git
#
cd ${HOME}

printf "#Update Repository \n"

git -C $HOME/${git_repo}/ pull && git -C $HOME/${git_repo}/ submodule update --remote


# install app
#Prepare Enviroment
#
# Set Cavern Enviroment
mkdir -p ~/.config/pip/

if [[ ! -f ~/.config/pip/pip.conf ]]; then	
   printf "[global] \nbreak-system-packages = true\n" > ~/.config/pip/pip.conf
fi

# Add python3 path 

# TODO search the line and  add if not exists
printf 'export PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc
#exec bash

#source ~/.bashrc

if [[ -f $(which tunnpy) ]]; then
  printf "Previous version of tunnpy found , uninstalling ..." 
  $(which pip3) uninstall tunnpy && make -C $HOME/${git_repo}/ api

#    printf "Install tunnpy bridge app and dependencies\n"
#make -C $HOME/${git_repo}/ uninstall &&
fi

printf "Install tunnpy bridge app and dependencies\n"
make -C $HOME/${git_repo}/ uninstall && make -C $HOME/${git_repo}/ api
# make -C "${HOME}/${git_repo}"
# make -C "${HOME}/${git_repo}/ ${git_repo_module}"

source ~/.bashrc


# # Note  install systemctl configs for tunnpy
# systemctl_service=api.service
# systemctl_service_path=$HOME/${git_repo}/restapi/bridge/systemd/${systemctl_service}
# systemctl_user_dir=$HOME/.config/systemd/user/

# mkdir -p $HOME/.config/systemd/user/

# cp ${systemctl_service_path} ${systemctl_user_dir}

# systemctl --user enable $systemctl_service 
# systemctl --user start $systemctl_service 
# systemctl --user status $systemctl_service







