#! /bin/csh -f

set T = "."
if ("$argv[1]" == "--target-dir") then
    set T = "$argv[2]"
    shift
    shift
endif

if ($#argv != 2 && $#argv != 3) then
    echo "Usage: $0 [ --target-dir <target-directory> ] <release> <machine-type> [ <workarea> ]"
    exit 1
endif

set R = $argv[1]
set M = $argv[2]
if ($#argv == 2) then
  set W = $M
else
  set W = $argv[3]
endif

if (!(-d $W)) then
    echo "Error: work area $W does not exist or is not a directory"
    exit 1
endif

if (!(-e $W/boot/$M)) then
    echo "Error: "$
    exit 1
endif

if (-e $R) then
    echo "Error: $R already exists"
    exit 1
endif

onintr error

mkdir $R || goto error
( cd $R ; ln -s ../LICENSE ../NOTICE . ) || goto error
( cd $R ; ln -s ../$W/scheme.1.in ../$W/installsh ../$W/examples . ) || goto error
( cd $R ; ln -s ../$W/Mf-install Makefile ) || goto error
mkdir -p $R/boot/$M $R/bin/$M || goto error
( cd $R/boot/$M ; ln -s ../../../$W/boot/$M/{scheme.h,petite.boot,scheme.boot} . ) || goto error

switch ($M)
    case a6nt:
    case ta6nt:
    case ti3nt:
    case i3nt:
        ( cd $R/boot/$M ; ln -s ../../../$W/boot/$M/{csv951md.lib,csv951mt.lib,mainmd.obj,mainmt.obj,scheme.res} . ) || goto error
        ( cd $R/bin/$M ; ln -s ../../../$W/bin/$M/{scheme.exe,csv951.dll,csv951.lib,vcruntime140.lib} . ) || goto error
        breaksw
    default:
        ( cd $R/boot/$M ; ln -s ../../../$W/boot/$M/{main.o,kernel.o} . ) || goto error
        ( cd $R/bin/$M ; ln -s ../../../$W/bin/$M/{scheme} . ) || goto error
        breaksw
endsw

set broken = `find -L $R -type l`
if ($#broken != 0) then
    echo "Error: missing $broken"
    goto error
endif

tar -czhf $T/csv$R-$M.tar.gz $R || goto error
rm -rf $R
exit

error:
rm -rf $R
exit 1
