#!/bin/bash

# Fonction pour gérer l'interruption du script
cleanup() {
  printf "\nInterruption du script...\n"
  exit 0
}

# Capturer le signal SIGINT (Ctrl+C)
trap cleanup SIGINT

install() {
  printf "INSTALLATION DE VIMSCODE\n"
  printf 'Installation en cours : [--------------------](0%%)\n'
  
  sudo apt update > /dev/null 2>&1
  sudo apt install figlet -y > /dev/null 2>&1
  printf 'Installation en cours : [#####---------------](25%%)\n'

  if [ ! -d "$HOME/.vim" ]; then
      mkdir ~/.vim && mv * ~/.vim       
  fi
  
  cd ~/.vim && mv vimrc-conf ~/.vimrc && mv vimrc2-conf ~/.vimrc2 
  cd bundle/ && git clone https://github.com/VundleVim/Vundle.vim.git > /dev/null 2>&1
  sudo mv ~/.vim/vimscode.sh /bin/
  
  printf 'Installation en cours : [##########----------](50%%)\n'

  printf "Utilisez-vous bash ou zsh ?\n"
  read -r term

  if [ "$term" = "bash" ]; then
    if ! grep -q 'alias vimscode="/bin/vimscode.sh"' ~/.bashrc; then
      echo 'alias vimscode="/bin/vimscode.sh"' >> ~/.bashrc
    fi
  else
    if ! grep -q 'alias vimscode="/bin/vimscode.sh"' ~/.zshrc; then 
      echo 'alias vimscode="/bin/vimscode.sh"' >> ~/.zshrc
    fi
  fi
  
  printf 'Configuration en cours : [###############-----](75%%)\n'

  vim -u ~/.vimrc2 -c PluginInstall -c q -c q > /dev/null 2>&1
  rm -rf ~/.vimrc2
  
  rm -rf ../VimsCode
  
  printf 'Configuration en cours : [####################](100%%)\n'
  printf "Installation terminée.\n"
}

# Fonction pour la mise à jour de VimsCode
maj() {
  printf "Mise à jour en cours...\n"
  printf 'Début de la mise à jour : [-----------------](0%%)\n'
  
  if [ -d "$HOME/VimsCode" ]; then
      rm -rf VimsCode       
  fi

  rm -rf ~/.vim ~/.vimrc
  git clone https://git.cohabit.fr/bastien/VimsCode.git > /dev/null 2>&1
  cd VimsCode

  if [ ! -d "$HOME/.vim" ]; then
      mkdir ~/.vim && mv * ~/.vim       
  fi
  
  cd ~/.vim && mv vimrc-conf ~/.vimrc && mv vimrc2-conf ~/.vimrc2 
  cd bundle/ && git clone https://github.com/VundleVim/Vundle.vim.git > /dev/null 2>&1
  sudo mv ~/.vim/vimscode.sh /bin/
  
  printf 'Mise à jour en cours : [#####---------------](25%%)\n'

  printf "Utilisez-vous bash ou zsh ?\n"
  read -r term

  if [ "$term" = "bash" ]; then
    if ! grep -q 'alias vimscode="/bin/vimscode.sh"' ~/.bashrc; then
      echo 'alias vimscode="/bin/vimscode.sh"' >> ~/.bashrc
    fi
  else
    if ! grep -q 'alias vimscode="/bin/vimscode.sh"' ~/.zshrc; then 
      echo 'alias vimscode="/bin/vimscode.sh"' >> ~/.zshrc
    fi
  fi
  
  printf 'Mise à jour en cours : [##########----------](50%%)\n'
  
  vim -u ~/.vimrc2 -c PluginInstall -c q -c q > /dev/null 2>&1
  rm -rf ~/.vimrc2
  printf 'Mise à jour en cours : [###############-----](75%%)\n'
 
  rm -rf ../../VimsCode
  
  printf 'Mise à jour fini : [####################](100%%)\n'
  printf "Mise à jour terminée.\n"
}

# Gestion des arguments
case "$1" in
  -maj)
    maj
    ;;
  -install)
    install
    ;;
  -a | -arduino)
    vim -p "$2" ~/.vim/help/En/help-arduino
    ;;
  -e | -esp)
    vim -p "$2" ~/.vim/help/En/help-esp32
    ;;
  -p | -platform.io)
    vim -p "$2" ~/.vim/help/En/help-platform.io
    ;;
  '')
    cat ~/.vim/help/help-vimscode
    ;;
  *)
    echo "Commande invalide"
    ;;
esac