build_option=${1:-1}
HTML_PATH=${2:-"/var/www/html"}
DRUPAL_PATH=${3:-/var/www/drupal8}
gesso_version="8.x-3.2-uw_wcms1"

if [[ -L "$HTML_PATH/uw_wcms_gesso" ]]; then
  echo ""
  echo "*************************************************************************"
  echo "Removing old gesso symlink ..."
  rm -rf "$HTML_PATH/uw_wcms_gesso"
  echo "Done removing old gesso symlink."
  echo "**************************************************************************"
  echo ""
fi

cd "$DRUPAL_PATH/web/profiles/uw_base_profile/modules/custom/uw_wcms_gesso"

if [[ -d gesso ]]; then
  echo "**************************************************************************************"
  echo "Removing old gesso ..."
  rm -rf gesso
  echo "Done removing old gesso."
  echo "**************************************************************************************"
  echo ""
fi

git clone --branch $gesso_version https://git.uwaterloo.ca/drupal-org/gesso.git

if [[ $build_option == 1 || $build_option == 3 ]]; then

  if [ -f "gulpfile.js" ]; then
    cp gulpfile.js gesso
  fi

  if [ -f "package.json" ]; then
    cp package.json gesso
  fi

  echo "**************************************************************************************"
  echo "Removing Gesso files ..."
  rm gesso/gesso.layouts.yml
  rm gesso/gesso.info.yml
  rm gesso/gesso.libraries.yml
  rm gesso/gesso.breakpoints.yml
  rm gesso/Dockerfile
  rm gesso/screenshot.png
  rm -rf gesso/templates
  echo "Done removing gesso theme files."
  echo "**************************************************************************************"
  echo ""

  echo "**************************************************************************************"
  echo "Running npm install ..."
  cd gesso
  npm install
  echo "Done npm install."
  echo "**************************************************************************************"
  echo ""

  echo "**************************************************************************************"
  echo "Symbolic linking source directory"
  if [ -d "source" ]; then
    echo ""
    echo "**************************************************************************************"
    echo "Removing old source directory ..."
    rm -rf source
    echo "Done removing old source directory."
    echo "**************************************************************************************"
    echo ""
  fi
  ln -fs "../source" source
  echo "Done symbolic linking source directory."
  echo "**************************************************************************************"
  echo ""

  echo "**************************************************************************************"
  echo "Removing style lint config and replacing with UW config ..."
  rm .stylelintrc.yml
  cp ../.stylelintrc.yml .
  echo "Done removing style lint config and replacing with UW config."
  echo "**************************************************************************************"
  echo ""

  echo "**************************************************************************************"
  echo "Running gulp build ..."
  gulp build
  cd ..
  echo "Done running gulp build."
  echo "**************************************************************************************"
  echo ""

  echo "**************************************************************************************"
  echo "Creating symlink for gesso ..."
  cd $HTML_PATH
  if [[ $build_option == 1 ]]; then
    ln -s "$DRUPAL_PATH"/web/profiles/uw_base_profile/modules/custom/uw_wcms_gesso uw_wcms_gesso
  elif [[ $build_option == 2 ]]; then
    cd /var/www/html/web
    ln -s ./profiles/uw_base_profile/modules/custom/uw_wcms_gesso uw_wcms_gesso
    cd -
  else
    ln -s ../web/profiles/uw_base_profile/modules/custom/uw_wcms_gesso uw_wcms_gesso
  fi
  echo "Done creating symlink for gesso."
  echo "**************************************************************************************"
  echo ""

else
  echo "**************************************************************************************"
  echo "Setting up gesso helper ..."
  mv gesso/gesso_helper .
  rm -rf gesso/*
  mv gesso_helper gesso
  echo "Done setting up gesso helper"
  echo "**************************************************************************************"
fi
