#!/bin/bash


if git --version;
then 
  echo "git installed, ok"
else
  sudo apt update && sudo apt install git
fi

if docker --version; 
then
  echo "Docker installed, ok"
else
  curl -sSL https://get.docker.com/ | sh
  echo "Docker has been installed"
  echo "Configure your user rights:"
  echo "   sudo usermod -a -G docker $USER"
  echo "   or : adduser $(whoami) docker"
fi

echo "---------------script docker-----------------------------"

########################################################################
# Docker path conf
if [ -f "/usr/local/bin/docker" ]
then
  echo "docker alias exists already"
else
  DOCKERBIN="/usr/local/bin/docker"
  sudo touch $DOCKERBIN
  sudo chmod o+w $DOCKERBIN
  sudo echo "#!/bin/sh
exec sudo -E /usr/bin/docker \"\$@\"" >> $DOCKERBIN
  sudo chmod gou+x $DOCKERBIN
  sudo chmod o-w $DOCKERBIN
fi
########################################################################

if stack --version;
then
  echo "Haskell stack installed, ok"
else
  curl -sSL https://get.haskellstack.org/ | sh
  stack update
  stack upgrade
fi

git clone https://gitlab.iscpif.fr/gargantext/haskell-gargantext.git

cd haskell-gargantext
git clone https://gitlab.iscpif.fr/gargantext/purescript-gargantext

../install-deps $(pwd)

pushd devops/docker
docker build --pull -t fpco/stack-build:lts-18.12-garg .
popd

#stack docker pull
stack --docker setup
stack --docker build
stack --docker install


# Database configuration
# CREATE USER gargantua WITH PASSWORD $(grep DB_PASS gargantext.ini)
# GRANT ALL PRIVILEGES ON DATABASE gargandbV4 to gargantua
