#!/bin/bash

outputtype="ttf"

instanceparam="-i"

generateufo=true

xslfile="fix_wdth_axis.xsl"

for arg in $@
do
  if [ $arg == "otf" ]
  then
    outputtype=$arg
  elif [ $arg == "variable" ]
  then
    outputtype=$arg
    instanceparam=""
  elif [ $arg == "variable-cff2" ]
  then
    outputtype=$arg
    instanceparam=""
  elif [ $arg == "u" ]
  then
    generateufo=false
  elif [ $arg == "s"  ]
  then
    xslfile="fix_wdth_axis-shorter.xsl"
fi

done

if [ "$generateufo" = true ]
then
  echo "Generating UFOs and designspace file from Glyphs source"
  glyphs2ufo --no-preserve-glyphsapp-metadata --propagate-anchors --generate-GDEF \
   --write-public-skip-export-glyphs Junicode-Italic.glyphs
  if [ $? -ne 0 ]
  then
    echo "glyphs2ufo failed"
    exit 1
  fi
  echo "Cleaning up designspace file"
  xsltproc -o Junicode-Italic-fixed.designspace $xslfile Junicode-Italic.designspace
  if [ $? -ne 0 ]
  then
   echo "xsltproc failed"
   exit 1
  fi
fi

echo "building font(s)"
fontmake -o $outputtype $instanceparam -m "Junicode-Italic-fixed.designspace"
if [ $? -ne 0 ]
then
 echo "fontmake failed"
 exit 1
fi
