#!/bin/bash

cd $(dirname $(realpath -s ${0}))

RULES=()

function generate_rule() {
  file=${1}
  filename=${2}

  RULES+=("${filename}.css")
  echo "${filename}.css: ${file}" >> Makefile.wip
  echo "	\$(SASS) ${file} -o ." >> Makefile.wip
}

function finalize() {
  echo -n "all:" >> Makefile
  for rule in "${RULES[@]}"; do
    echo -n " $rule" >> Makefile
  done
  echo >> Makefile
  cat Makefile.wip >> Makefile
  rm Makefile.wip
}

cat <<EOF > Makefile
SASS = \$(shell pwd)/../../node_modules/node-sass/bin/node-sass --output-style compressed
EOF
for file in *.sass; do
  generate_rule ${file} ${file%.*}
done
for file in *.scss; do
  generate_rule ${file} ${file%.*}
done

finalize