blob: cab54e4fa69943825c5d2b6e2af185d2d4072d31 [file] [log] [blame]
garciadeblas70461c52024-07-03 09:17:56 +02001#!/bin/bash
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16# "Debug mode" variable
17DEBUG="${DEBUG:-}"
18[[ "${DEBUG,,}" == "true" ]] && set -x
19
20# If there is an input stream, dumps it into a temporary file and sets it as INFILE
21if [[ -n "${INSTREAM}" ]];
22then
23 # Save input stream to temporary file
24 TMPFILE=$(mktemp /tmp/INSTREAM.XXXXXXXXXX) || exit 1
25 echo "${INSTREAM}" > "${TMPFILE}"
26 export INFILE="${TMPFILE}"
27fi
28
29# Sets default INPUT and OUTPUT
30INFILE="${INFILE:-/dev/stdin}"
31OUTFILE="${OUTFILE:-/dev/stdout}"
32
33# Loads helper functions and KRM functions
34source /app/scripts/library/helper-functions.rc
35source /app/scripts/library/krm-functions.rc
36
37# If applicable, loads additional environment variables
38if [[ -n "${CUSTOM_ENV}" ]];
39then
40 set -a
41 source <(echo "${CUSTOM_ENV}")
42 set +a
43fi
44
45# In case INFILE and OUTFILE are the same, it uses a temporary output file
46if [[ "${INFILE}" == "${OUTFILE}" ]];
47then
48 TMPOUTFILE="$(mktemp "/results/OUTFILE.XXXXXXXXXX")" || exit 1
49else
50 TMPOUTFILE="${OUTFILE}"
51fi
52
53#################### EXECUTION ####################
54# Debug mode:
55if [[ "${DEBUG,,}" == "true" ]];
56then
57 "$@" < "${INFILE}" | tee "${TMPOUTFILE}"
58# Normal mode:
59else
60 "$@" < "${INFILE}" > "${TMPOUTFILE}"
61fi
62###################################################
63
64# In case INFILE and OUTFILE are the same, it renames the temporary file over the OUTFILE (i.e., the same as INFILE)
65if [[ "${INFILE}" == "${OUTFILE}" ]];
66then
67 mv -f "${TMPOUTFILE}" "${OUTFILE}"
68fi