#!/bin/bash 
set -e

fasta=$1
outputfasta=$2
classif=$3
outputlog=$4
outputclassif=$7
outputconfig=$5
outputstats=$6

projectname=$(date "+%Y%m%d")


add=''

if [ $classif == yes ]
then
  add='-c'
fi
`dirname $0`'/'TEdenovo_lite.py -i $fasta -o $outputfasta $add > $outputlog
projectname_complete=$(ls $(pwd)|grep $projectname)
working_dir=$(pwd)/$projectname_complete
sed -i 's@'"$working_dir"'@'$projectname'@g' $outputlog 
mv $outputfasta-$projectname-denovoLibTEs_filtered.fa $outputfasta
mv $outputfasta-$projectname-classif_stats.txt $outputstats
if [ $classif == yes ]
then
  mv $outputfasta-$projectname.classif $outputclassif
fi


workingconfigfile=$working_dir/TEdenovo_Galaxy_config_$projectname_complete
sed -i 's|repet_host:.*|repet_host:|g' $workingconfigfile
sed -i 's|repet_user:.*|repet_user:|g' $workingconfigfile
sed -i 's|repet_pw:.*|repet_pw:|g' $workingconfigfile
sed -i 's|repet_db:.*|repet_db:|g' $workingconfigfile
sed -i 's|repet_port:.*|repet_port:|g' $workingconfigfile
sed -i 's|repet_job_manager:.*|repet_job_manager:|g' $workingconfigfile
sed -i 's|project_name:.*|project_name: '$projectname'|g' $workingconfigfile
sed -i 's|project_dir:.*|project_dir:|g' $workingconfigfile
sed -i 's|tmpDir:.*|tmpDir:|g' $workingconfigfile
mv $workingconfigfile $outputconfig
 
