# this common script includes common utilites 
# to improve Bash scripts logging messages. 


this_script="${0##*/}"
platform=`uname`
pid=$$
tmp_file_name="$PQBHOME/sm/server/bin/$pid.txt"

# delete temporary file if exist
delete_tmp_file () {
    rm -f $tmp_file_name &>/dev/null
}

# copy message to standard output and append to temporary file if exist
append_to_tmp_file () {
    tee -a $tmp_file_name
}

# redirect messagte to temporary file if exist
redirect_to_tmp_file () {
    append_to_tmp_file &>/dev/null 
}

# create unique temporary file under the
# name <process-id>.txt if not exist
create_tmp_file () {
    if [ -e "$tmp_file_name" ]; then
	echo "tmp file $tmp_file_name already exist" | redirect_to_tmp_file
    else
        cat /dev/null >> $tmp_file_name &>/dev/null
    fi
}

# copy message to standard output and append to temporary file if exist
echo_and_append_to_tmp_file () {
    echo "$*" | append_to_tmp_file
}

# write temporary file to clu log 
write_tmp_file_to_clu_log () {
    if [ -e "$tmp_file_name" ]; then
        $PQBHOME/sm/server/bin/p3log --write-file-to-clu-log -f $tmp_file_name
    fi
}

# write temporary file to clu log, and delete the temporary file
write_and_delete_tmp_file_to_clu_log () {
#   echo "write and delete tmp file $tmp_file_name to clu log" | redirect_to_tmp_file
    write_tmp_file_to_clu_log &>/dev/null
    delete_tmp_file
}

# write temporary file to clu log, and delete the temporary file and exit
write_and_delete_tmp_file_to_clu_log_and_exit () {
#    echo "write and delete tmp file $tmp_file_name to clu log" | redirect_to_tmp_file
    write_tmp_file_to_clu_log &>/dev/null
    delete_tmp_file
    exit "$1"
}


