#!/bin/bash --posix

# Based on findScalaHome() from scalac script
findKotlinHome() {
    local source="${BASH_SOURCE[0]}"
    while [ -h "$source" ] ; do
        local linked="$(readlink "$source")"
        local dir="$(cd -P $(dirname "$source") && cd -P $(dirname "$linked") && pwd)"
        source="$dir/$(basename "$linked")"
    done
    (cd -P "$(dirname "$source")/.." && pwd)
}

KOTLIN_HOME="$(findKotlinHome)"

CP=""
SEP="-cp "
if [ -e classes ]; then
  CP="-cp classes"
  SEP=":"
fi
for f in $KOTLIN_HOME/ext/*.jar; do
  CP="$CP$SEP$f"
  SEP=":"  
done

kotlinc $CP -script "$@"
