#!/bin/bash

KUMABASE=$(dirname $(dirname $0))
CSSDIR=$KUMABASE/media/redesign/css
STYLUSDIR=$KUMABASE/media/redesign/stylus

for opt in "$@"; do
  case $opt in
    --watch|-w) WATCH=true;;
    --quiet|-q) QUIET=true;;
  esac
done

if [ ! -d "$CSSDIR" ]; then
  mkdir $CSSDIR
fi

STYLESHEETS=($STYLUSDIR/*.styl)
if [ $WATCH ]; then
  echo Watching and automatically compiling stylesheets

  # Using $STYLESHEETS directly so that stylus only watches each stylesheet
  # once, even if it is @included multiple times.
  if [ $QUIET ]; then
    (stylus ${STYLESHEETS[@]} --out $CSSDIR --compress --watch &) &> /dev/null
  else
    stylus ${STYLESHEETS[@]} --out $CSSDIR --compress --watch
  fi
else
  # Iterating through stylesheets so that shell output is immediate.
  for ss in ${STYLESHEETS[@]}; do
    stylus $ss --out $CSSDIR --compress
  done
fi
