#!/bin/bash

# Parse command line arguments
for arg in "$@"; do
    case $arg in
        --filename=*)
            filename="${arg#*=}"
            ;;
        --report_date=*)
            report_date="${arg#*=}"
            ;;
        --mail_day=*)
            mail_day="${arg#*=}"
            ;;
        --data_dir=*)
                   data_dir="${arg#*=}"
                   ;;
        --borders=*)
                   borders="${arg#*=}"
                   ;;
               *)
            echo "Unknown option: $arg"
            exit 1
            ;;
    esac
done

# Check if required arguments are set
if [ -z "$filename" ] || [ -z "$report_date" ]|| [ -z "$borders" ] || [ -z "$mail_day" ] || [ -z "$data_dir" ]; then
    echo "Missing arguments. Use: build_reports.sh --filename=hello.txt --report_date=2020-01-01 --mail_day=Friday --data_dir=chemba --borders=TRUE"
    exit 1
fi

# Script logic here
echo "Filename: $filename"
echo "Report date: $report_date"
echo "Mail day: $mail_day"
echo "Data directory: $data_dir"
echo "Borders: $borders"

if [ "$(uname)" == "Darwin" ]; then
    # Commando's voor Mac
    echo 'zx28tb' | /usr/bin/sudo -S launchctl load -w /System/Library/LaunchAgents/com.apple.fontd.useragent.plist
    cd /Users/mfolkerts/smartCane/r_app
elif [ "$(uname)" == "Linux" ]; then
    # Commando's voor Linux
    cd /var/www/vhosts/smartcane.sobit.nl/httpdocs/r_app
else
    echo "Onbekend systeem"
fi
Rscript -e "rmarkdown::render('CI_report_dashboard_planet.Rmd', output_file='$filename', params=list(ref='$ref', report_date='$report_date', mail_day='$mail_day', data_dir='$data_dir', borders='$borders'))"