#!/bin/bash

# disable flags
unset_if_false=(
    par_ignore_symlinks
    par_dirs
    par_full_names
    par_fn_as_s_name
    par_profile_runtime
    par_verbose
    par_quiet
    par_strict
    par_development
    par_require_logs
    par_no_megaqc_upload
    par_no_ansi
    par_flat
    par_interactive
    par_static_plot_export
    par_data_dir
    par_no_data_dir
    par_zip_data_dir
    par_pdf
)

for par in ${unset_if_false[@]}; do
    test_val="${!par}"
    [[ "$test_val" == "false" ]] && unset $par
done

# handle inputs
out_dir=$(dirname "$par_output_report")
output_report_file=$(basename "$par_output_report")
report_name="${output_report_file%.*}"

# handle outputs
[[ -z "$par_output_report" ]] && no_report=true
[[ -z "$par_output_data" ]] && no_data_dir=true
[[ ! -z "$par_output_data" ]] && data_dir=true
[[ ! -z "$par_output_plots" ]] && export=true

# handle multiples
IFS=";" read -ra inputs <<< $par_input

if [[ -n "$par_include_modules" ]]; then
    include_modules=""
    IFS=";" read -ra incl_modules <<< $par_include_modules
    for i in "${incl_modules[@]}"; do
        include_modules+="--include $i "
    done
    unset IFS
fi

if [[ -n "$par_exclude_modules" ]]; then
    exclude_modules=""
    IFS=";" read -ra excl_modules <<< $par_exclude_modules
    for i in "${excl_modules[@]}"; do
        exclude_modules+="--exclude $i"
    done
    unset IFS
fi

if [[ -n "$par_ignore_analysis" ]]; then
    ignore=""
    IFS=";" read -ra ignore_analysis <<< $par_ignore_analysis
    for i in "${ignore_analysis[@]}"; do
        ignore+="--ignore $i "
    done
    unset IFS
fi

if [[ -n "$par_ignore_samples" ]]; then
    ignore_samples=""
    IFS=";" read -ra ign_samples <<< $par_ignore_samples
    for i in "${ign_samples[@]}"; do
        ignore_samples+="--ignore-samples $i"
    done
    unset IFS
fi

# run multiqc
multiqc \
    ${par_output_report:+--filename "$report_name"} \
    ${out_dir:+--outdir "$out_dir"} \
    ${no_report:+--no-report} \
    ${no_data_dir:+--no-data-dir} \
    ${data_dir:+--data-dir} \
    ${export:+--export} \
    ${par_title:+--title "$par_title"} \
    ${par_comment:+--comment "$par_comment"} \
    ${par_template:+--template "$par_template"} \
    ${par_sample_names:+--sample-names "$par_sample_names"} \
    ${par_sample_filters:+--sample-filters "$par_sample_filters"} \
    ${par_custom_css_file:+--custom-css-file "$par_custom_css_file"} \
    ${par_profile_runtime:+--profile-runtime} \
    ${par_dirs:+--dirs} \
    ${par_dirs_depth:+--dirs-depth "$par_dirs_depth"} \
    ${par_full_names:+--full-names} \
    ${par_fn_as_s_name:+--fn-as-s-name} \
    ${par_ignore_names:+--ignore-names "$par_ignore_names"} \
    ${par_ignore_symlinks:+--ignore-symlinks} \
    ${ignore_samples} \
    ${ignore} \
    ${exclude_modules} \
    ${include_modules} \
    ${par_include_modules:+--include-modules "$par_include_modules"} \
    ${par_data_format:+--data-format "$par_data_format"} \
    ${par_cl_config:+--cl-config "$par_cl_config"} \
    ${par_zip_data_dir:+--zip-data-dir} \
    ${par_pdf:+--pdf} \
    ${par_interactive:+--interactive} \
    ${par_flat:+--flat} \
    ${par_verbose:+--verbose} \
    ${par_quiet:+--quiet} \
    ${par_strict:+--strict} \
    ${par_no_megaqc_upload:+--no-megaqc-upload} \
    ${par_no_ansi:+--no-ansi} \
    ${par_profile_runtime:+--profile-runtime} \
    ${par_require_logs:+--require-logs} \
    ${par_development:+--development} \
    --force \
    "${inputs[@]}" 

# Move outputs

if [[ -n "$par_output_data" ]] && [[ -d "${out_dir}/${report_name}_data" ]]; then
    mv "${out_dir}/${report_name}_data" "$par_output_data"
elif [[ -n "$par_output_data" ]] && [[ ! -d "${out_dir}/${report_name}_data" ]]; then
    echo "WARNING: Data could not be saved because data folder was not generated by multiqc. This could be due to filtering out of modules or samples."
fi

if [[ -n "$par_output_plots" ]] && [[ -d "${out_dir}/${report_name}_plots" ]]; then
    mv "${out_dir}/${report_name}_plots" "$par_output_plots"
elif [[ -n "$par_output_plots" ]] && [[ ! -d "${out_dir}/${report_name}_plots" ]]; then
    echo "WARNING: Plots could not be saved because plots folder was not generated by multiqc. This could be due to filtering out of modules or samples."
fi
