set -e
export NXF_ANSI_LOG=false

#
# create the `hello.sh` executable script
#
mkdir -p bin
echo "echo Hola mundo" > bin/hello.sh
chmod +x bin/hello.sh

#
# create the nextflow config file 
#
cat << EOF > nextflow.config
env.FOO = 'HOLA'
process.container = 'quay.io/nextflow/bash'
EOF

#
# create the nextflow script 
#
cat << EOF > foo.nf
process foo {
  debug true
  input: 
  env 'BAR'
  script:
  '''
  env | grep -E "^(FOO|BAR)" | sort
  hello.sh
  '''
}

workflow {
  channel.value('Hello World!') | foo
}
EOF

#
# not the testing part 
#

# NF plain run  
$NXF_CMD run foo.nf | tee stdout

# run tests 
[[ `grep 'INFO' .nextflow.log | grep -c 'Submitted process'` == 1 ]] || false
[[ `< stdout grep -c 'FOO=HOLA'` == 1 ]] || false
[[ `< stdout grep -c 'BAR=Hello World!'` == 1 ]] || false
[[ `< stdout grep -c 'Hola mundo'` == 1 ]] || false

# NF run with trace
$NXF_CMD run foo.nf -with-trace | tee stdout

# run tests 
[[ `grep 'INFO' .nextflow.log | grep -c 'Submitted process'` == 1 ]] || false
[[ `< stdout grep -c 'FOO=HOLA'` == 1 ]] || false
[[ `< stdout grep -c 'BAR=Hello World!'` == 1 ]] || false
[[ `< stdout grep -c 'Hola mundo'` == 1 ]] || false

# NF run with docker
$NXF_CMD run foo.nf -with-docker | tee stdout

# run tests 
[[ `grep 'INFO' .nextflow.log | grep -c 'Submitted process'` == 1 ]] || false
[[ `< stdout grep -c 'FOO=HOLA'` == 1 ]] || false
[[ `< stdout grep -c 'BAR=Hello World!'` == 1 ]] || false
[[ `< stdout grep -c 'Hola mundo'` == 1 ]] || false


# NF run with docker and trace
$NXF_CMD run foo.nf -with-trace -with-docker | tee stdout

# run tests 
[[ `grep 'INFO' .nextflow.log | grep -c 'Submitted process'` == 1 ]] || false
[[ `< stdout grep -c 'FOO=HOLA'` == 1 ]] || false
[[ `< stdout grep -c 'BAR=Hello World!'` == 1 ]] || false
[[ `< stdout grep -c 'Hola mundo'` == 1 ]] || false
