#! /bin/bash

main() {
   log flags...
   flags "$@"
   log install...
   install
   log build...
   build
}

flags() {
   set -e

   export THEMES="${1:-"${THEME:-"${THEMES:-"onepage"}"}"}"
   export PAGE_SIZE="${PAGE_SIZE:-A4}"

   if [ "$THEMES" == "all" ]; then
      export THEMES="jsonresume-theme-ace","jsonresume-theme-actual","jsonresume-theme-apage","jsonresume-theme-autumn","jsonresume-theme-caffeine","jsonresume-theme-class","jsonresume-theme-classy","jsonresume-theme-cora","jsonresume-theme-dave","jsonresume-theme-elegant","jsonresume-theme-elite","jsonresume-theme-eloquent","jsonresume-theme-even","jsonresume-theme-flat","jsonresume-theme-flat-fr","jsonresume-theme-full","jsonresume-theme-github","jsonresume-theme-jacrys","jsonresume-theme-kards","jsonresume-theme-keloran","jsonresume-theme-kendall","jsonresume-theme-kwan","jsonresume-theme-kwan-linkedin","jsonresume-theme-latex","jsonresume-theme-macchiato","jsonresume-theme-mantra","jsonresume-theme-mocha-responsive","jsonresume-theme-modern","jsonresume-theme-msresume","jsonresume-theme-onepage","jsonresume-theme-onepageresume","jsonresume-theme-orbit","jsonresume-theme-paper","jsonresume-theme-paper-plus-plus","jsonresume-theme-papirus","jsonresume-theme-pumpkin","jsonresume-theme-rocketspacer","jsonresume-theme-short","jsonresume-theme-simple-red","jsonresume-theme-slick","jsonresume-theme-spartan","jsonresume-theme-srt","jsonresume-theme-stackoverflow","jsonresume-theme-standard-resume","jsonresume-theme-tachyons-clean","jsonresume-theme-tan-responsive","jsonresume-theme-techlead","jsonresume-theme-verbum","jsonresume-theme-wraypro"
      export THEMES="${THEMES//jsonresume-theme-/}"
   fi
}

install() {
   install_cli
   install_themes

   if ! which wkhtmltopdf &> /dev/null; then
      log installing wkhtmltopdf
      local d=$(mktemp -d)
      apt -y install xfonts-75dpi
      wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb -O "$d/wkhtmltox.deb"
      if ! dpkg -i "$d/wkhtmltox.deb"; then
         apt-get -f install
         dpkg -i "$d/wkhtmltox.deb"
      fi
      rm -rf "$d"
   fi
}

install_cli() {
   if which resume &> /dev/null; then
      return
   fi

   _npm install -g resume-cli
   which resume
}

install_themes() {
   for theme in ${THEMES//,/ }; do
      if [ -d node_modules/jsonresume-theme-$theme ]; then
         return
      fi
      log installing theme $theme
      if ! _npm install jsonresume-theme-$theme; then
         log failed
         export THEMES="${THEMES//,$theme,/,}"
      else
         log ok
      fi
   done
}

build() {
   if ! [ -f ./resume.json ]; then
      resume init
   fi

   for theme in ${THEMES//,/ }; do
      THEME=$theme build_theme
   done
}

build_theme() {
   log building theme $THEME

   (
      resume validate
      mkdir -p ./out
      resume export ./out/resume.$THEME.html --theme $THEME || true
      if [ -f ./out/resume.$THEME.html ]; then
         wkhtmltopdf --page-size $PAGE_SIZE ./out/resume.$THEME.html ./out/resume.$THEME.pdf
      fi
   ) || true
}

_npm() {
   which npm || exit 1
   echo "> $(which npm) "$@"" >&2
   $(which npm) "$@"
}


log() {
   echo "$(date) > $*" >&2
}

if [ "$0" == "$BASH_SOURCE" ]; then
   main "$@"
fi
