env_option=${1:-2}
build_option=${2:-1}
PL_PATH=${3:-"/var/www/html/web"}
DRUPAL_PATH=${4:-"/var/www/html"}
os_option=${5:-1}

if [[ -L "$PL_PATH/uw_wcms_ohana" || -d "$PL_PATH/uw_wcms_ohana" ]]; then
  echo ""
  echo "*************************************************************************"
  echo "Removing old ohana symlink ..."
  rm -rf "$PL_PATH/uw_wcms_ohana"
  echo "Done removing old ohana symlink."
  echo "**************************************************************************"
  echo ""
fi

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

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

git clone https://github.com/chazchumley/ohana.git

if [ "$?" != 0 ]; then
  echo "Error: git clone failed"
  exit 2;
fi

cd ohana

echo "**************************************************************************************"
echo "Installing correct npm version ..."
file=".nvmrc"
version=$(cat "$file")
export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;
nvm install $version
echo "**************************************************************************************"
echo "Done installing correct npm version."

if [[ $build_option == 1 || $build_option == 3 ]]; then
  echo "**************************************************************************************"
  echo "Setting up theming ..."
  cd ..
  if [[ -d dist ]]; then
    rm -rf dist
  fi
  ln -s ohana/dist dist
  cd ohana
  rm -rf src/patterns
  ln -s ../../src/patterns src/patterns
  echo "**************************************************************************************"
  echo "Done setting up theming."
fi

echo "**************************************************************************************"
echo "Copying fonts ..."
cp -r "$DRUPAL_PATH/web/fonts/*" dist/fonts
echo "**************************************************************************************"
echo "Done copying fonts."

echo "**************************************************************************************"
echo "Running npm install ..."
if [[ $os_option == 1 ]]; then
  export CPPFLAGS="-DPNG_ARM_NEON_OPT=0"
fi
npm install
echo "**************************************************************************************"
echo "Done running npm install."

echo "**************************************************************************************"
echo "Running npm run build ..."
npm run build
echo "**************************************************************************************"
echo "Done running npm run build."

if [[ $build_option == 2 || $build_option == 4 ]]; then
  echo "**************************************************************************************"
  echo "Setting up css/js and removing ohana ..."
  cd ..
  if [[ -d dist ]]; then
    rm -rf dist
    mkdir dist
  else
    mkdir dist
  fi
  cp -r ohana/dist/* dist
  cd dist/css
  find . -type f -not -name 'all.min.css' -delete
  cd ../js
  find . -type f -not -name 'all.min.js' -delete
  cd ..
  if [[ -d vendor ]]; then
    rm -rf vendor
  fi
  cd "$DRUPAL_PATH"/web/profiles/uw_base_profile/modules/custom/uw_wcms_ohana/
  rm -rf ohana
  echo "**************************************************************************************"
  echo "Done setting up css/js and removing ohana."
fi

if [[ $env_option == 1 ]]; then
  echo "**************************************************************************************"
  echo "Creating symlink for ohana ..."
  ln -s "$DRUPAL_PATH"/web/profiles/uw_base_profile/modules/custom/uw_wcms_ohana "$PL_PATH"/uw_wcms_ohana
  echo "Done creating symlink for ohana."
  echo "**************************************************************************************"
  echo ""
fi
