#!/bin/bash

set +e

export PATH=$PATH:$GOPATH/bin
ROOT_DIR=$(cd $(dirname $(dirname $0)) && pwd)

function killed {
  exit 1
}
trap killed SIGINT

echo -e "\n Updating translation files"

if [ ! $(which goi18n) ];then
  echo -e "\n Installing go-i18n..."
  go get github.com/nicksnyder/go-i18n/goi18n
fi

echo -e "\n Updating i18n4go..."
go get -u github.com/krishicks/i18n4go/i18n4go
if [ $? -ne 0 ]; then
  printf "Failed to run `go get -u github.com/krishicks/i18n4go/i18n4go`"
  exit 1
fi

i18n4go -c fixup
if [ $? -ne 0 ]; then
  printf "Failed to run `i18n4go -c fixup`"
  exit 1
fi

goi18n -outdir cf/i18n/resources cf/i18n/resources/*.all.json
if [ $? -ne 0 ]; then
  printf "Failed to run `goi18n -outdir cf/i18n/resources cf/i18n/resources/*.all.json`"
  exit 1
fi

if [[ $(git status --porcelain cf/i18n/resources) ]]; then
  bin/generate-language-resources
  if [ $? -ne 0 ]; then
    printf "Failed to run `bin/generate-language-resources`"
    exit 1
  fi
fi