#!/bin/sh -e

root="$PWD"
s="source/sc"
c="$root/source/c-precompiled"
cc="source/c"
export SC_LOAD_PATH="$root/$s"

compile_sc() {
  # create the c source code
  cd "$s"
  find . -type f -name "*.sc" | sc --stdin-input-paths --parents "$c"
  cd "$root"
  find "$c" -type f -exec ./exe/format-c -i '{}' \;
  # rename .h.c to .h
  for path in "$c/sph"/*.h.c
  do
    mv "$path" "$(dirname $path)/$(basename $path .h.c).h"
  done
  cp --target-directory="$c" --update --recursive "$cc"/*
}

mkdir -p "$c"
compile_sc