#!/bin/sh

QURANTXT=quran-uthmani.txt
TRANSLATIONS="en.shakir ur.jawadi hi.hindi id.indonesian"

abort() {
  echo
  echo "  $@" 1>&2
  echo
  exit 1
}

log() {
  echo "  ○ $@"
}

download() {
  log "Downloading $2"
  if test -e "$2"; then
    log "$2 already downloaded, skipping"
  else
    curl $1 > $2 || abort "failed while downloading $2"
  fi
}

mkdir -p contrib data

if test -e contrib/$QURANTXT; then
  log 'already downloaded - skipping'
else
  curl -d 'quranType=uthmani&marks=true&sajdah=true&alef=true&outType=txt-2&agree=true' http://tanzil.net/pub/download/download.php  > contrib/$QURANTXT || abort 'download failed'
fi


download http://tanzil.net/res/text/metadata/quran-data.js contrib/quran-data.js
grep -q module.exports contrib/quran-data.js || echo 'module.exports = QuranData;' >> contrib/quran-data.js

node quran/loader contrib/$QURANTXT arabic

for translation in $TRANSLATIONS
do
  download http://tanzil.net/trans/$translation contrib/$translation.txt
  node quran/loadtrans contrib/$translation.txt
done
