#!/bin/bash

HOME_DBI=/home/dbi
DBI_SSH="$HOME_DBI/.ssh"
HOME_ROOT=/root
source /opt/scripts_remote/shared_functions.sh
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
type -p git >/dev/null || (sudo apt install -y git)
sudo -u dbi mkdir -p "$DBI_SSH"
echo "Install_github_config.sh: Downloading ssh keys."
[[ ! -f "$DBI_SSH/id_github.pub"  ]] && download_template "/mypool1/data/shared/home/dbi/.ssh/id_github*" "$DBI_SSH/"
echo "Install_github_config.sh: Testing connection with Github.com."
ssh -F /dev/null -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -i "$DBI_SSH/id_github" -T git@github.com
echo "Install_github_config.sh: Starting ssh agent."
eval "$(ssh-agent)"
echo "Install_github_config.sh: Adding key to ssh agent."
chmod 700 "$DBI_SSH/id_github"
ssh-add "$DBI_SSH/id_github"
echo "Install_github_config.sh: Cloning repository."
sudo rm -rf /opt/.my_git_repos/denis_mbp > /dev/null
git clone git@github.com:cartache/denis_mbp.git /opt/.my_git_repos/denis_mbp
echo "Install_github_config.sh: Sourcing files with .bash_aliases."
! _isStringInFile "source /opt/.my_git_repos/denis_mbp/.bash_aliases" "$HOME_DBI/.bashrc" && echo "source /opt/.my_git_repos/denis_mbp/.bash_aliases" | tee -a "$HOME_DBI/.bashrc"
! _isStringInFile "source /opt/.my_git_repos/denis_mbp/.bash_aliases" "$HOME_ROOT/.bashrc" && echo "source /opt/.my_git_repos/denis_mbp/.bash_aliases" | sudo tee -a "$HOME_ROOT/.bashrc"
! _isStringInFile "source /opt/.my_git_repos/denis_mbp/.bash_aliases" /etc/profile && echo "source /opt/.my_git_repos/denis_mbp/.bash_aliases"  | sudo tee -a /etc/profile
echo "Install_github_config.sh: Creating .bash_aliases_local."
sudo -u dbi touch "$HOME_DBI/.bash_aliases_local"
sudo touch "$HOME_ROOT/.bash_aliases_local"
echo "Install_github_config.sh: Setting right security."
sudo chown dbi:dbi -R "$HOME_DBI"
sudo chmod 600 "$HOME_DBI/.bashrc" "$HOME_DBI/.bash_aliases_local"

source /opt/.my_git_repos/denis_mbp/.bash_aliases




