#!/bin/bash

# decoil-viz version
VERSION="1.0.3"

COVERAGE=""
SUMMARY=""
REFERENCE=""
GTF=""
BED=""
LINKS=""
OUTDIR=""
NAME=""
WINDOW=""

singularity_flag=false
docker_flag=false
phelp=false
pversion=false

# Function to print usage information
print_usage_short() {
    echo "Usage: decoil-viz [--docker|--singularity|--version|--help] --coverage <coverage_file> --summary <summary_file> --reference <reference_file> --gtf <gtf_file> --bed <bed_file> --links <links_file> --outdir <output_directory> --name <output_name>"
}

display_help(){
    print_usage_short
    echo ""
    echo "Options:"
    echo "	-h,--help	      		Display this help message"
    echo "	--version	    		Display version"
    echo "	--docker        		Flag: run decoil-viz using docker (set by default if --singularity not active)"
    echo "	--singularity			Flag: run decoil-viz using singularity"
    echo "	-o,--outdir <output_directory> Output directory (absolute path)"
    echo "	--name				Sample name"
    echo "	--coverage <coverage_file>	Coverage file in .bw format (absolute path)"
    echo "	--bed <bed_file>		Reconstruction regions file in .bed like format (absolute path)"
    echo "	--links <links_file>		Reconstruction links file in .txt format (absolute path)"
    echo "	--summary <summary_file>	Reconstructions summary (absolute path)"
    echo "	-r,--reference <reference_file>	Reference genome in .fasta format (absolute path)" 
    echo "	-g,--gtf <gtf_file>		Genes annotation file in .gtf format (absolute path)"
}


# Parse command line arguments
while [[ $# -gt 0 ]]; do
     case $1 in
	--version)
            pversion=true
            shift 1
            ;;
        -h|--help)
            phelp=true
            shift 1
            ;;
        --coverage)
            COVERAGE="$2"
            shift 2
            ;;
        --summary)
            SUMMARY="$2"
            shift 2
            ;;
        -r|--reference)
            REFERENCE="$2"
            shift 2
            ;;
        -g|--gtf)
            GTF="$2"
            shift 2
            ;;
        --bed)
            BED="$2"
            shift 2
            ;;
        --links)
            LINKS="$2"
            shift 2
            ;;
        -o|--outdir)
            OUTDIR="$2"
            shift 2
            ;;
        --name)
            NAME="$2"
            shift 2
            ;;
	--window-file)
	    WINDOW="$2"
            shift 2
            ;;
         --docker)
            if [ "$singularity_flag" = true ]; then
                echo "Error: --docker and --singularity cannot be set at the same time"
                exit 1
            fi
            docker_flag=true
	    shift 1
            ;;
        --singularity)
            if [ "$docker_flag" = true ]; then
                echo "Error: --docker and --singularity cannot be set at the same time"
                exit 1
            fi
            singularity_flag=true
	    shift 1
            ;;
	*)
            echo "Unknown option: $1"
            print_usage_short
            exit 1
            ;;
    esac
done

#  Check if help option is specified
if [ "$phelp" = true ]; then
    display_help
    exit 0
fi


#  Check if versionn is specified
if [ "$pversion" = true ]; then
    echo "decoil-viz $VERSION"
    exit 0
fi

# Check if all required parameters are provided
if [[ -z $COVERAGE || -z $SUMMARY || -z $REFERENCE || -z $GTF || -z $BED || -z $LINKS || -z $OUTDIR || -z $NAME ]]; then
    echo "Error: Missing required parameter(s)."
    print_usage_short
    exit 1
fi

# Your script logic goes here...
echo "Coverage file: $COVERAGE"
echo "Summary file: $SUMMARY"
echo "Reference file: $REFERENCE"
echo "GTF file: $GTF"
echo "BED file: $BED"
echo "Links file: $LINKS"
echo "Output directory: $OUTDIR"
echo "Output name: $NAME"

# List of files to check
files=($REFERENCE $GTF $COVERAGE $SUMMARY $BED $LINKS)

# Iterate over the list of files
for file in "${files[@]}"; do
    # Check if the file exists
    if [ -e "$file" ]; then
        echo "File '$file' exists."
    else
        echo "File '$file' does not exist."
    fi
done


if [ [ "$singularity_flag" = false ] && [ "$docker_flag" = false ] ] ||  [ "$docker_flag" = true ]; then

	echo "Running decoil-viz with docker..."

	docker run --platform=linux/amd64 \
    		-v $REFERENCE:$REFERENCE \
    		-v $GTF:$GTF \
    		-v $COVERAGE:$COVERAGE \
    		-v $BED:$BED \
    		-v $LINKS:$LINKS \
    		-v $SUMMARY:$SUMMARY \
   		-v $OUTDIR:$OUTDIR \
	decoil-viz:${VERSION} decoil-viz \
    		--coverage $COVERAGE \
    		--bed $BED \
    		--links $LINKS \
    		-r $REFERENCE \
    		-g $GTF \
    		-o $OUTDIR \
    		--summary $SUMMARY \
    		--name $NAME
	

elif [ "$singularity_flag" = true ]; then

	echo "Running decoil-viz with singularity..."

	singularity run \
		--bind $OUTDIR:/mnt:rw \
		--bind $REFERENCE:/tmp/ref.fa \
		--bind $GTF:/tmp/anno.gtf \
		--bind $COVERAGE:/mnt/coverage.bw \
		--bind $BED:/mnt/reconstruct.bed \
		--bind $LINKS:/mnt/reconstruct.links.txt \
		--bind $SUMMARY:/mnt/summary.txt \
	decoil-viz.sif exec decoil-viz \
		--coverage /mnt/coverage.bw \
		--bed /mnt/reconstruct.bed \
		--links /mnt/reconstruct.links.txt \
		-r /tmp/ref.fa \
		-g /tmp/anno.gtf \
		--summary /mnt/summary.txt \
		--name $NAME \
		-o /mnt

fi
