#!/bin/bash

#
# convert2bed
# Copyright (C) 2014-2016 Alex Reynolds
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

cmd="convert2bed"
input_format="gtf"
output_format="starch"

# general

do_not_sort=false
max_mem_set=false
max_mem="2G"
sort_tmpdir_set=false
sort_tmpdir="/tmp"
starch_bzip2=false
starch_gzip=false
starch_note=""
starch_not_set=false

# format-specific

help()
{
    ${cmd} --help-gtf
    exit $1
}

optspec=":e:zgr:m:dh-:"
while getopts "$optspec" optchar; do
    case "${optchar}" in
        -)
            case "${OPTARG}" in
                starch-bzip2)
                    starch_bzip2=true
                    ;;
                starch-gzip)
                    starch_gzip=true
                    ;;
                sort-tmpdir)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    sort_tmpdir=${val}
                    sort_tmpdir_set=true
                    ;;
                sort-tmpdir=*)
                    val=${OPTARG#*=}
                    sort_tmpdir=${val}
                    sort_tmpdir_set=true
                    ;;
                starch-note)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    starch_note="\"${val}\""
                    starch_note_set=true
                    ;;
                starch-note=*)
                    val=${OPTARG#*=}
                    starch_note="\"${val}\""
                    starch_note_set=true
                    ;;
                max-mem)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    max_mem=${val}
                    max_mem_set=true
                    ;;
                max-mem=*)
                    val=${OPTARG#*=}
                    max_mem=${val}
                    max_mem_set=true
                    ;;
                do-not-sort)
                    do_not_sort=true
                    ;;
                help)
                    val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
                    #echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
                    help 0
                    ;;
                help=*)
                    val=${OPTARG#*=}
                    opt=${OPTARG%=$val}
                    #echo "Parsing option: '--${opt}', value: '${val}'" >&2
                    help 0
                    ;;
                help-gtf)
                    help 0
                    ;;
                *)
                    help 1
                    #if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then echo "Unknown option --${OPTARG}" >&2; fi
                    ;;
            esac;;
        e)
            starch_note="\"${OPTARG}\""
            starch_note_set=true
            ;;
        z)
            starch_bzip2=true
            ;;
        g)
            starch_gzip=true
            ;;
        r)
            sort_tmpdir=${OPTARG}
            sort_tmpdir_set=true
            ;;
        m)
            max_mem=${OPTARG}
            max_mem_set=true
            ;;
        d)
            do_not_sort=true
            ;;
        h)
            help 0
            ;;
        *)
            help 1
            #if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then echo "Non-option argument: '-${OPTARG}'" >&2; fi
            ;;
    esac
done

# base options

options="--input=${input_format} --output=${output_format}"

# general

if [ "${max_mem_set}" = true ]; then options="${options} --max-mem=${max_mem}"; fi
if [ "${sort_tmpdir_set}" = true ]; then options="${options} --sort-tmpdir=\"${sort_tmpdir}\""; fi
if [ "${do_not_sort}" = true ]; then options="${options} --do-not-sort"; fi
if [ "${starch_gzip}" = false ]; then starch_bzip2=true; else options="${options} --starch-gzip"; fi
if [ "${starch_bzip2}" = true ]; then options="${options} --starch-bzip2"; fi
if [ "${starch_note_set}" = true ]; then options="${options} --starch-note=\"${starch_note}\""; fi

# format-specific

${cmd} ${options} - <&0
