#########################################################################
#                                                                       #
#                            Objective Caml                             #
#                                                                       #
#         Damien Doligez, projet Moscova, INRIA Rocquencourt            #
#                                                                       #
#   Copyright 2000 Institut National de Recherche en Informatique et    #
#   en Automatique.  All rights reserved.  This file is distributed     #
#   under the terms of the Q Public License version 1.0.                #
#                                                                       #
#########################################################################

# $Id: ocamlmkappli,v 1.2 2001/03/13 16:37:33 doligez Exp $


# ocamlmkappli -- build a standalone application

# usage: ocamlmkappli [option] file
# options:
#   -creator <code>   use this creator code (default '????')
#   -ocamlc <command> use <command> as O'Caml compiler (default ocamlc)
#   -d <def>          pass "-d <def>" option to Rez
#   -prefsize <int>   set preferred memory size (kilobytes, default 4000)
#   -lib <folder>     use library files from <folder> (default {CAMLLIB})
#   -minsize <int>    set minimum memory size (megabytes, default 2000)
#   -name <name>      set the name of the application (default a.out)
#   -r <file>         add resources from this file (or rez source file)

set echo 0

set creator '????'
set ocamlc ocamlc
set rezopt ''
set prefsize 4000
set lib "{{CAMLLIB}}"
set minsize 2000
set name a.out
set rezfiles ''

set files ''

loop
  break if {#} == 0
  if "{{1}}" == "-creator"
    set creator "{{2}}"
    shift
  else if "{{1}}" == "-ocamlc"
    set ocamlc "{{2}}"
    shift
  else if "{{1}}" == "-d"
    set rezopt "{{rezopt}} -d ``quote "{{2}}"``"
    shift
  else if "{{1}}" == "-prefsize"
    set prefsize {2}
    shift
  else if "{{1}}" == "-lib"
    set lib "{{2}}"
    shift
  else if "{{1}}" == "-minsize"
    set minsize {2}
    shift
  else if "{{1}}" == "-name"
    set name "{{2}}"
    shift
  else if "{{1}}" == "-r"
    set rezfiles "{{rezfiles}} ``quote "{{2}}"``"
    shift
  else
    set files "{{files}} ``quote "{{1}}"``"
  end
  shift
end

if {prefsize} < {minsize}
  set prefsize {minsize}
end

set tmp "{{tempfolder}}ocamlmkappli-`date -n`"
delete -y -ay -i "{{tmp}}"

duplicate -y "{{lib}}appli" "{{name}}"
rez -d SystemSevenOrLater=1 -d PREFSIZE="{prefsize}" -d MINSIZE="{minsize}" 
    -d APPLNAME=""{{name}}"" -d CREATOR="'{{creator}}'" 
    -a -o "{{name}}" "{{lib}}appli.r" {rezopt} {rezfiles}
{ocamlc} -use-prims "{{lib}}appliprims" {files} -o "{{tmp}}"
catenate "{{tmp}}" >> "{{name}}"
setfile -t APPL -c "{{creator}}" -a iB "{{name}}"

delete -i "{{tmp}}"
