#!/bin/bash

cd /cromwell_root
tmpDir=$(mkdir -p "/cromwell_root/tmp.9bb15ce3" && echo "/cromwell_root/tmp.9bb15ce3")
chmod 777 "$tmpDir"
export _JAVA_OPTIONS=-Djava.io.tmpdir="$tmpDir"
export TMPDIR="$tmpDir"
export HOME="$HOME"
(
cd /cromwell_root
touch /cromwell_root/monitoring.log
chmod u+x /cromwell_root/monitoring.sh
/cromwell_root/monitoring.sh > /cromwell_root/monitoring.log &
)
out573e2181="${tmpDir}/out.$$" err573e2181="${tmpDir}/err.$$"
mkfifo "$out573e2181" "$err573e2181"
trap 'rm "$out573e2181" "$err573e2181"' EXIT
tee '/cromwell_root/stdout' < "$out573e2181" &
tee '/cromwell_root/stderr' < "$err573e2181" >&2 &
(
cd /cromwell_root


set -e
python3 $(which encode_task_jsd.py) \
    /cromwell_root/caper_out/placenta_20201030/atac/573e2181-742f-49ff-819e-35f6509ce64f/call-filter/shard-0/glob-3bcbe4e7489c90f75e0523ac6f3a9385/K7_1B_1.nodup.no_chrM_MT.bam \
    --mapq-thresh 30 \
    --blacklist /cromwell_root/encode-pipeline-genome-data/hg38/hg38.blacklist.bed.gz \
    --nth 2
)  > "$out573e2181" 2> "$err573e2181"
echo $? > /cromwell_root/rc.tmp
(
# add a .file in every empty directory to facilitate directory delocalization on the cloud
cd /cromwell_root
find . -type d -exec sh -c '[ -z "$(ls -A '"'"'{}'"'"')" ] && touch '"'"'{}'"'"'/.file' \;
)
(
cd /cromwell_root
sync
# make the directory which will keep the matching files
mkdir /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd

# create the glob control file that will allow for the globbing to succeed even if there is 0 match
echo "This file is used by Cromwell to allow for globs that would not match any file.
By its presence it works around the limitation of some backends that do not allow empty globs.
Regardless of the outcome of the glob, this file will not be part of the final list of globbed files." > /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd/cromwell_glob_control_file

# hardlink or symlink all the files into the glob directory
( ln -L /cromwell_root/*.jsd.qc /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd 2> /dev/null ) || ( ln /cromwell_root/*.jsd.qc /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd )

# list all the files (except the control file) that match the glob into a file called glob-[md5 of glob].list
ls -1 /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd | grep -v cromwell_glob_control_file > /cromwell_root/glob-9c8d296ce3c847878b53bb751aed36dd.list

# make the directory which will keep the matching files
mkdir /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4

# create the glob control file that will allow for the globbing to succeed even if there is 0 match
echo "This file is used by Cromwell to allow for globs that would not match any file.
By its presence it works around the limitation of some backends that do not allow empty globs.
Regardless of the outcome of the glob, this file will not be part of the final list of globbed files." > /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4/cromwell_glob_control_file

# hardlink or symlink all the files into the glob directory
( ln -L /cromwell_root/*.png /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4 2> /dev/null ) || ( ln /cromwell_root/*.png /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4 )

# list all the files (except the control file) that match the glob into a file called glob-[md5 of glob].list
ls -1 /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4 | grep -v cromwell_glob_control_file > /cromwell_root/glob-fc36854b6867c1581ab159b09dd7e2f4.list


)
mv /cromwell_root/rc.tmp /cromwell_root/rc
