#! /bin/bash

source script/show_solutions

function update_openfpm()
{
  echo -e "\033[1;5;31m WARNING: the update will destroy any changes in the OpenFPM source code  \033[0m"
  
  commands[0]="Continue"
  commands[0]="${commands[@]}"
  possible_solutions "${commands[@]}"

  ## Get the name of the updating branch

  url_update=$(cat $1)
  echo "Checking for update from $url_update"
  rm tmp_brc
  wget -O tmp_brc $url_update
  branch=$(cat tmp_brc)
  echo "Update branch $branch"
  if [ x"$branch" == x"" ]; then
    echo -e "\033[1m There are no-updates \033[0m"
    return
  fi

  actual_branch=$(git branch | grep \* | cut -d ' ' -f2)

  if [ x"$branch" == x"$actual_branch" ]; then

   git remote update
   n_commit=$(git rev-list --count origin/$branch...$branch)

    if [ $n_commit -eq 0 ]; then
      echo -e "\033[1m There are no-updates \033[0m"
      return
    fi
  fi

  git stash -u
  git submodule foreach 'git stash'

  git pull origin
  git checkout $branch
  git submodule update

  sh autogen.sh
  ./config.status
  if [ $? -ne 0 ]; then
    echo -e "Update summary\033[91;5;1m FAILED \033[0m"
  fi
  make
  if [ $? -ne 0 ]; then
    echo -e "Update summary\033[91;5;1m FAILED \033[0m"
  else
    echo -e "Update summary\033[92;5;1m SUCCEED \033[0m"
    echo -e "To make the update active do: \033[1m make install \033[0m"
  fi

}