#!/bin/bash

source ../bin/common_pipeline.sh
source ../bin/scheduler_common.sh

PID_PIPELINE="PID"
SJ_JOBSTDOUT=""
SJ_JOBSTDERR=""
INPUT_LABEL=""
INPUT_STEPID=""
INPUT_JOBSTR=""
INPUT_SCHEDID="66666"
create_joblog() {
    INPUT_LABEL="${1-*}"
    INPUT_STEPID="${2-*}"
    INPUT_JOBSTR="${3-*}"
    sj_joblog_base "$INPUT_LABEL" "$INPUT_STEPID" "$INPUT_JOBSTR"
    SJ_JOBSTDOUT="JobLog/${SJ_R_JOBLOG_NAME}_${SJ_JOBLOG_STDOUT_EXT}_${INPUT_SCHEDID}.log"
    SJ_JOBSTDERR="JobLog/${SJ_R_JOBLOG_NAME}_${SJ_JOBLOG_STDERR_EXT}_${INPUT_SCHEDID}.log"
}

test_joblog_split() {
    local f="$1"
    local iserr="$2"
    sj_joblog_split "$f"
    printf "%-80s " "$f"
    local nerr=0
    [[ "$SJ_R_TODO_LABEL" == "$INPUT_LABEL"   ]] || nerr=$((nerr+1))
    [[ "$SJ_R_STEPID"     == "$INPUT_STEPID"  ]] || nerr=$((nerr+1))
    [[ "$SJ_R_JOBLABEL"   == "$INPUT_JOBSTR"  ]] || nerr=$((nerr+1))
    [[ "$SJ_R_PID"        == "$PID_PIPELINE"  ]] || nerr=$((nerr+1))
    [[ "$SJ_R_SCHEDID"    == "$INPUT_SCHEDID" ]] || nerr=$((nerr+1))
    [[ "$SJ_R_ISERROR"    == "$iserr"         ]] || nerr=$((nerr+1))
    if (( nerr == 0 )); then
        echo "[OK]"
    else
        echo "[FAIL]"
        echo "SPLIT:"
        echo " SJ_R_TODO_LABEL = $SJ_R_TODO_LABEL"
        echo " SJ_R_STEPID = $SJ_R_STEPID"
        echo " SJ_R_JOBLABEL = $SJ_R_JOBLABEL"
        echo " SJ_R_PID = $SJ_R_PID"
        echo " SJ_R_ISERROR = $SJ_R_ISERROR"
        echo " SJ_R_SCHEDID = $SJ_R_SCHEDID"
    fi
}

create_joblog "todolbl" "ALIGNMENT" "12456_0-678_0"
test_joblog_split "$SJ_JOBSTDOUT" 0
test_joblog_split "$SJ_JOBSTDERR" 1


create_joblog "todolbl" "ALIGNMENT" "12456_0-678"
test_joblog_split "$SJ_JOBSTDOUT" 0
test_joblog_split "$SJ_JOBSTDERR" 1

create_joblog "todolbl" "ALIGNMENT" "12456-678_0"
test_joblog_split "$SJ_JOBSTDOUT" 0
test_joblog_split "$SJ_JOBSTDERR" 1

create_joblog "todolbl" "ALIGNMENT" "12456-678"
test_joblog_split "$SJ_JOBSTDOUT" 0
test_joblog_split "$SJ_JOBSTDERR" 1
