# Short script to configure Earl Grey to run on any system in a portable manner

# check presence of RepeatMasker and RepeatModeler
echo "Checking RepeatMasker and RepeatModeler configuration"
if [ -x "$(command -v RepeatMasker)" ]; then
        echo "Success! RepeatMasker is installed and in PATH"
else
        echo "Error: RepeatMasker is not installed and in PATH"
        exit 1
fi

if [ -x "$(command -v RepeatModeler)" ]; then
        echo "Success! RepeatModeler is installed and in PATH"
else
        echo "Error: RepeatModeler is not installed and in PATH"
        exit 1
fi

# create new conda environment

if [ ! -z $(conda env list | awk '{OFS="\t"}{print $1}' | grep -w "earlgrey") ]; then
        conda env update --name earlgrey -f ./earlGrey.yml --prune
else
        conda env create -f ./earlgrey.yml
fi
eval "$(conda shell.bash hook)"
# conda update -n base -c defaults conda
conda activate earlgrey

# make Earl Grey Script executable

chmod +x ./earlGrey

# set paths in Earl Grey Script

echo "Setting path variables in script"
SCRIPT_DIR=$(realpath ./scripts/)
MOD_DIR=$(realpath ./modules/)
sed -i "s|SCRIPT_DIR=.*|SCRIPT_DIR=${SCRIPT_DIR}|g" ./earlGrey
sed -i "s|SCRIPT_DIR=.*|SCRIPT_DIR=${SCRIPT_DIR}|g" ./scripts/rcMergeRepeat*
sed -i "s|SCRIPT_DIR=.*|SCRIPT_DIR=${SCRIPT_DIR}|g" ./scripts/headSwap.sh
sed -i "s|SCRIPT_DIR=.*|SCRIPT_DIR=${SCRIPT_DIR}|g" ./scripts/autoPie.sh
sed -i "s|INSERT_FILENAME_HERE|${SCRIPT_DIR}/TEstrainer/scripts/|g" ./scripts/TEstrainer/TEstrainer_for_earlGrey.sh
chmod +x ${SCRIPT_DIR}/TEstrainer/TEstrainer_for_earlGrey.sh
chmod +x ${SCRIPT_DIR}/* > /dev/null 2>&1
chmod +x ${SCRIPT_DIR}/bin/LTR_FINDER.x86_64-1.0.7/ltr_finder
chmod a+w ${SCRIPT_DIR}/repeatCraft/example/
chmod +x ${MOD_DIR}/trf409.linux64
sleep 3
echo "Path variables set"
sleep 3

# install sa-ssr
if [ ! -f "${SCRIPT_DIR}/sa-ssr" ]; then
  echo "Installing sa-ssr"
  cd ${SCRIPT_DIR} && chmod a+w ${SCRIPT_DIR}
  git clone https://github.com/ridgelab/SA-SSR && cd ./SA-SSR/
  sed -i "s|PREFIX=/usr/local/bin|PREFIX=${SCRIPT_DIR}|g" Makefile && make && make install
  sed -i "153s|.* -e|${SCRIPT_DIR}/sa-ssr -e|g" ${SCRIPT_DIR}/TEstrainer/TEstrainer_for_earlGrey.sh
  sed -i "s|txt trf|txt ${MOD_DIR}/trf409.linux64|g" ${SCRIPT_DIR}/TEstrainer/TEstrainer_for_earlGrey.sh
  cd ${SCRIPT_DIR}
  else
  cd ${SCRIPT_DIR}/SA-SSR/
  sed -i "s|PREFIX=/usr/local/bin|PREFIX=${SCRIPT_DIR}|g" Makefile && make && make install
  sed -i "153s|.* -e|${SCRIPT_DIR}/sa-ssr -e|g" ${SCRIPT_DIR}/TEstrainer/TEstrainer_for_earlGrey.sh
  sed -i "s|txt trf|txt ${MOD_DIR}/trf409.linux64|g" ${SCRIPT_DIR}/TEstrainer/TEstrainer_for_earlGrey.sh
  cd ${SCRIPT_DIR}
fi

# Install R packages - DEPRECATED
# echo "Installing R packages"
# Rscript ${SCRIPT_DIR}/install_r_packages.R

# Extract tRNAdb
echo "Extracting zip archives"
tar -zxf ${SCRIPT_DIR}/bin/LTR_FINDER.x86_64-1.0.7/tRNAdb.tar.gz --directory ${SCRIPT_DIR}/bin/LTR_FINDER.x86_64-1.0.7/
echo "Extracted required archives"
sleep 3

# Message Complete
echo "Remember to activate the earl grey conda environment before running earlGrey"
sleep 3
export PATH=$PATH:$(realpath .)
echo "earlGrey is ready to use. To execute from any directory, add earlGrey to path by pasting the code (minus the square brackets) below..."
echo '[export PATH=$PATH:$(realpath .)]'
