#!/usr/bin/env bash
#
# This script replaces ids from FTAXIDS with <id>_1 to <id>_2 .
# It can be used as a base for replacing

echo
echo "This is a template script for handling renaming of taxids in an existing pipeline."
echo "Make a copy and edit according to needs."
echo
exit 0

#---------------------------------------------------------------------------------
#FTAXIDS=allnew.todo
FTAXIDS=ttt.todo
[[ -f $FTAXIDS ]] || { echo "error: missing taxid file $FTAXIDS"; exit 255; }
declare -a ALLTODO=( $(cat $FTAXIDS) )
#---------------------------------------------------------------------------------

export DRY_RUN=1
export LOG_FILE_BASE=./update/
[[ -d $LOG_FILE_BASE ]] || { echo "error: missing log file target dir $LOG_FILE_BASE"; exit 255; }


move() {
    local f1=$1
    local f2=$2
    if (( DRY_RUN == 1 )); then
        echo "DRYRUN MOVE: $f1  ->  $f2"
        return 0
    fi
    [[ -f $f1 ]] || { echo "move: missing source file $f1"; return 255; }
    [[ -f $f2 ]] && return 0
    echo "MOVE: $f1  ->  $f2"
    mv -v $f1 $f2
}

replace12() {
    local f2=$1
    if (( DRY_RUN == 1 )); then
        echo "DRYRUN REPLACE12: $f2"
        return 0
    fi
    [[ -f $f2 ]] || { echo "replace12: missing source file $f2"; return 255; }
    echo "REPLACE12: $f2"
    replace _1 _2 -- $f2
}

makedir() {
    local d=$1
    if (( DRY_RUN == 1 )); then
        echo "DRYRUN MAKEDIR: $d"
        return 0
    fi
    echo "MAKEDIR: $d"
    mkdir -p $d
}

export T0=
start_timer() {
    T0=$(date '+%s')
}
stop_timer() {
    local t=$(date '+%s')
    local dt=$((t-T0))
    echo "processed in $dt secs"
}
#
# Update files in Sequences.
# Note the formatdb files need to be rerun.
#
update_sequences() {
    [[ $# -eq 1     ]] || { echo "SEQ:error bad nr of args ($#)"; return 255; }
    local T1=$1
    [[ ${#T1} -gt 0 ]] || { echo "SEQ:error arg is empty string"; return 255; }

    local T2=${T1//_1/_2}
    if [[ $T1 == $T2 ]]; then
        echo "SEQ:skip input label $T1 (already _2)"
        return 0
    fi
    local fbase1=Sequences/${T1}
    local fbase2=Sequences/${T2}
    local f1=
    local f2=
    for ext in fs_clean fs_masked fs_selected fs_selected.clstr fs_selected.seqlen; do
        f1=${fbase1}.$ext
        f2=${fbase2}.$ext
        #[[ -f $f1 ]] || { echo "SEQ:error($T1) missing file $f1"; return 255; }
        #[[ -f $f2 ]] && continue
        move $f1 $f2   || { echo "SEQ:error($T1) move failed $f1 -> $f2"; return 255; }
        replace12 $f2  || { echo "SEQ:error($T1) replace12 failed $f2";   return 255; }
    done
    return 0
}

# Update PWC
update_pwc() {
    [[ $# -eq 1     ]] || { echo "PWC:error bad nr of args ($#)"; return 255; }
    local T1=$1
    [[ ${#T1} -gt 0 ]] || { echo "PWC:error arg is empty string"; return 255; }
    local T2=${T1//_1/_2}
    if [[ $T1 == $T2 ]]; then
        echo "PWC:skip input label $T1 (already _2)"
        return 0
    fi
    local dir1=PWC/$T1
    local dir2=PWC/$T2
    [[ -d $dir1 ]] || { echo "PWC:error($T1) missing source directory $dir1"; return 255; }
    [[ -d $dir2 ]] || {        
        makedir "$dir2" || { echo "PWC:error($T1) failed making target directory $dir2"; return 255; }
    }
    local f1=
    local f2=
    local Ntot=0
    for ext in align brh inpar; do
        local N=0
        for f1 in $(find "$dir1" -name "${T1}_*.${ext}"); do
            f2="${f1//_1/_2}"
            #[[ -f $f1 ]] || { echo "PWC:error($T1) - missing file $f1"; return 255; }
            #[[ -f $f2 ]] && continue
            move $f1 $f2   || { echo "PWC:error($T1) - move failed $f1 -> $f2"; return 255; }
            replace12 $f2  || { echo "PWC:error($T1) - replace12 failed $f2";   return 255; }
            N=$((N+1))
        done
        if (( N==0 )); then
            echo "PWC:warn($T1) - no files found with extension <$ext>"
        fi
        Ntot=$(( Ntot+N ))
    done
}

echo "===> Will update ${#ALLTODO[@]} taxids"
for taxid in ${ALLTODO[@]}; do
    LOG_FILE=$LOG_FILE_BASE/update_${taxid}.log
    echo "---------------------"
    echo "Start taxid $taxid"
    echo "---------------------"
    start_timer
    # update files in Rawdata
    # already done by means of import file

    {
        # update files in Sequences
        update_sequences "$taxid" || exit 255
    
        # update files in PWC
        update_pwc "$taxid" || exit 255
    } >& $LOG_FILE
    stop_timer
done

