#! /bin/csh -f

# 1 = mex script filepath
# 2 = mex include path
# 3 = script path (containing this file)
# 4 = setup script filepath
# 5 = input filepath
# 6 = option
# 7 = intermediate filepath
# 8 = output filepath

source $4

if ($CUDA != 1) exit 0

if (! -e $5) then
    echo cannot find $5
    exit 1
endif

if ($6 == compile) then
    nvcc -cuda -I $2 -use_fast_math -o $7 $5
else if ($6 == preprocess) then
    nvcc -E -I $2 -use_fast_math -o $7 $5
else if ($6 == info) then
    nvcc -cubin -I $2 -use_fast_math -o $7 $5
endif

if (-e $7) then
    echo CUDA preprocessing successful
else
    echo 'CUDA preprocessing [nvcc] failed'
    exit 1
endif

if ($6 == compile) then
    $1 -output $8 $7 $CUDA_LINK_LIB
else
    exit 0
endif

if (-e $8) then
    echo CUDA compilation successful
    exit 0
else
    echo 'CUDA compilation [mex] failed'
    exit 1
endif
