#!/bin/sh
#
# tmpcomp       -- A temporary script to bootstrap the system
#
# Copyright © 2002-2023 Erick Gallesio <eg@stklos.net>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
#           Author: Erick Gallesio [eg@unice.fr]
#    Creation date:  1-Jan-2002 18:57 (eg)
#

# This script is only used to compile the different components of the
# system which need compilation. It will not be installed since the
# user version is the stklos-compile script.

out="a.out"
in=""
Copt=""

for arg in $*
do
    case $1 in
        -o)
            out=$2
            shift 2
            ;;
        -C)
            Copt=--C-code
            shift
            ;;
        -*)
            echo "Unknown option $1"
            exit 1
            ;;
        *)
            break
            ;;
    esac
done

case $# in
    1) in=$1
       ;;
    *) echo "Usage: $0 [-C] [-o bytecode-file] src"
       exit 1
       ;;
esac

prefix="$(cd -- "$(dirname "$0")/.." >/dev/null 2>&1 && pwd)"

STKLOS_LOAD_PATH=".:../lib:${prefix}/lib:"
STKLOS_BUILDING=1
export STKLOS_LOAD_PATH STKLOS_BUILDING

${prefix}/src/stklos -c -q -f ${prefix}/utils/stklos-compile.stk -- \
     --no-time $Copt --output=$out $in

status=$?
if test "$status" -gt 0
then
    exit $status
fi

if test X$Copt = X -a -e $out
then
    chmod a+x $out
fi
