#!/bin/sh

## This script clones all the different repos.

if [ -z "$BASE_DIR" ]; then
  echo "BASE_DIR wasn't set."
  exit 1
fi

set_up_lime2_repo() {
  repo_name="$1"
  folder="$2"
  echo "Setting up $repo_name..."
  if [ -e ~/"$folder"/"$repo_name" ]; then
    echo "${GREEN}$repo_name is already there.${NC}"
    if [ "$UPDATE" = 'true' ]; then
      echo "Updating..."
      cd ~/"$folder"/"$repo_name" && git pull
    fi
  else
    echo "${ORANGE}Cloning new $repo_name...${NC}"
    cd ~/"$folder" && git clone ssh://flyingscorpio@2px.info.tm/~/repos/"$repo_name".git
  fi

  return 0
}

set_up_gitlab_repo() {
  repo_owner="$1"
  repo_name="$2"
  folder="$3"
  echo "Setting up $repo_owner/$repo_name..."
  if [ -e ~/"$folder"/"$repo_name" ]; then
    echo "${GREEN}$repo_name is already there.${NC}"
    if [ "$UPDATE" = 'true' ]; then
      echo "Updating..."
      cd ~/"$folder"/"$repo_name" && git pull
    fi
  else
    echo "${ORANGE}Cloning new $repo_name...${NC}"
    cd ~/"$folder" && git clone https://gitlab.com/"$repo_owner"/"$repo_name".git
  fi

  return 0
}

# REPOS ON LIME2 {{{

echo "Setting up Lessons..."
if [ ! -e ~/Lessons ]; then
  mkdir ~/Lessons && echo "${ORANGE}Created ~/Lessons${NC}"
fi
set_up_lime2_repo 'E2L' 'Lessons'
set_up_lime2_repo 'OpenClassrooms' 'Lessons'
set_up_lime2_repo 'Keepass'
set_up_lime2_repo 'PersonnalScripts'
set_up_lime2_repo 'RootMe'
set_up_lime2_repo 'SetupCockpit'

# }}}

# REPOS ON GITLAB {{{

if [ ! -e ~/SRC ]; then
  mkdir ~/SRC && echo "${ORANGE}Created ~/SRC${NC}"
fi

set_up_gitlab_repo 'e2li' 'depydoc' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'BlurMySearches' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'JsonToPython' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'MarioSokoban' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'PythonLocalBlockchain' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'Roboc-GUI' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'Roboc-console' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'ScheduleTimer' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'SudokuSolver' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'TicTacToe' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'TorNoVPN' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'WhitespaceInterpreter' 'SRC'
set_up_gitlab_repo 'flyingscorpio' 'WikipediaAcronymScraper' 'SRC'

echo "Setting up 2px..."
if [ -e ~/SRC/2px ]; then
  echo "${GREEN}2px is already there.${NC}"
  if [ "$UPDATE" = 'true' ]; then
    echo "Updating..."
    cd ~/SRC/2px && git pull
  fi
else
  echo "${ORANGE}Cloning new 2px...${NC}"
  cd ~/SRC && git clone git@gitlab.com:flyingscorpio/2px.git
  cd ~/SRC/2px && python3 -m venv env
  echo "
# Set own environment variables for Django
DJANGO_USER='flyingscorpio'
export DJANGO_USER
DJANGO_PASSWORD='ribosome66'
export DJANGO_PASSWORD
DJANGO_SECRET_KEY='q+hci0d5y90q)k07f@1k3qzr&w=@89n(0z0ukp)_iu(g9iws0r'
export DJANGO_SECRET_KEY
systemctl start mysqld.service" >> ~/SRC/2px/env/bin/activate
fi

echo "Setting up AD-website..."
if [ -e ~/SRC/AD-website ]; then
  echo "${GREEN}AD-website is already there.${NC}"
  if [ "$UPDATE" = 'true' ]; then
    echo "Updating..."
    cd ~/SRC/AD-website && git pull
  fi
else
  echo "${ORANGE}Cloning new AD-website...${NC}"
  cd ~/SRC && git clone git@gitlab.com:flyingscorpio/AD-website.git
  cd ~/SRC/AD-website && python3 -m venv env
  echo "
# Set own environment variables for Django
DJANGO_USER='alix_diwan'
export DJANGO_USER
DJANGO_PASSWORD='illustrations'
export DJANGO_PASSWORD
DJANGO_SECRET_KEY='1o&!w6785%#)gdht+o164+utq-jbd!h3_nbo9yvkf9+iq03^m6'
export DJANGO_SECRET_KEY
systemctl start mysqld.service" >> ~/SRC/AD-website/env/bin/activate
fi

echo "Setting up LVEL-website..."
if [ -e ~/SRC/LVEL-website ]; then
  echo "${GREEN}LVEL-website is already there.${NC}"
  if [ "$UPDATE" = 'true' ]; then
    echo "Updating..."
    cd ~/SRC/LVEL-website && git pull
  fi
else
  echo "${ORANGE}Cloning new LVEL-website...${NC}"
  cd ~/SRC && git clone git@gitlab.com:flyingscorpio/LVEL-website.git
  cd ~/SRC/LVEL-website && python3 -m venv env
  echo "
# Set own environment variables for Django
DJANGO_USER='flyingscorpio'
export DJANGO_USER
DJANGO_PASSWORD='ribosome66'
export DJANGO_PASSWORD
DJANGO_SECRET_KEY='t!d6q&g1*lm-p41m=pd!o&kga9jk)wom)sr7#4-=\$oo*\$)ymtc'
export DJANGO_SECRET_KEY
systemctl start mysqld.service" >> ~/SRC/LVEL-website/env/bin/activate
fi

# }}}