#!/bin/bash

# Stop at errors
set -e

# positionne l'environnement de calcul avec povray
source /applis/site/guix-start.sh
refresh_guix ced_cigri

# Set useful variables 
RUN_DIR="${HOME}/povray"

# Check args
if [ $# -ne 5 ]; then
  echo "ERROR: Bad number of parameters"
  echo "USAGE: $0 <output_file> <input_file> <x_size> <y_size> <clock>"
  exit 1
fi

# Compute
cd $RUN_DIR
povray +W${3} +H${4} -K$5 +O${1} ${2}

# Upload the result file into irods and clean
imkdir -p povray_results/$CIGRI_CAMPAIGN_ID
secure_iput -f ${1} povray_results/$CIGRI_CAMPAIGN_ID/
rm -f ${1}

# Clean OAR output files
CURR_DIR=$(pwd)
rm -f $CURR_DIR/OAR.cigri.$CIGRI_CAMPAIGN_ID.$OAR_JOB_ID.stdout
rm -f $CURR_DIR/OAR.cigri.$CIGRI_CAMPAIGN_ID.$OAR_JOB_ID.stderr
