#!/bin/bash

# Define paths
SCRIPT_DIR="/home/r-environment"
LOG_DIR="${SCRIPT_DIR}/logs"
DATA_DIR="${SCRIPT_DIR}/downloads"
DASH_DIR="${SCRIPT_DIR}/dashboard"

# Create necessary directories if they don't exist
mkdir -p $LOG_DIR
mkdir -p $DATA_DIR
mkdir -p $DASH_DIR

# Export PATH
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/sbin:/bin:/usr/sbin

# Change to the script directory
cd $SCRIPT_DIR

# Run the R script to download data
/usr/local/bin/Rscript $SCRIPT_DIR/script.R >> $SCRIPT_DIR/logs/cron.log 2>&1

# Render the R Markdown file
/usr/local/bin/Rscript -e "rmarkdown::render('${SCRIPT_DIR}/template.Rmd', output_file='${DASH_DIR}/dashboard.html')" >> ${LOG_DIR}/cron_rmarkdown.log 2>&1
