#!/bin/bash


tags=""

tmp_list=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
tmp_list="tmp_${tmp_list}.list"
rm $tmp_list > /dev/null 2>&1

#https://stackoverflow.com/questions/19408649/pipe-input-into-a-script
if [ -p /dev/stdin ]; then
    while read tag; do
        echo $tag
        # tags+="$tag\n"
        # tags+="$tag\n"
    done > $tmp_list

fi

for tag in "$@"; do
    echo $tag
done >> $tmp_list


if [ ! -f $tmp_list ]; then
    echo >&2 ""
    echo >&2 "silentfrompdbs by bcov - a tool to make a silent file from pdbs"
    echo >&2 "Usage:"
    echo >&2 "        cat list_of_tags.list | silentfrompdbs > silent.silent"
    echo >&2 "                             or"
    echo >&2 "        silentfrompdbs tag1 tag2 tag3 > silent.silent"
    exit 1
fi


jd2_program=score_jd2

#https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script/677212#677212
command -v score_jd2 >/dev/null 2>&1 || { 
    #echo >&2 "You must have extract_pdbs on your path!"; 
    #echo >&2 "Try doing cd silent_tools; ln -s /software/rosetta/latest/bin/extract_pdbs ."
    #exit 1; 
    jd2_program=pyjd2
}



tmp_file=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
tmp_file="tmp_${tmp_file}.silent"

$jd2_program -l $tmp_list -out:file:silent $tmp_file -out:file:silent_struct_type binary 1>&2

silentls $tmp_file | sed 's/_0001$//g' | silentrename $tmp_file

rm $tmp_list
rm $tmp_file
rm $tmp_file.idx



