#!/bin/sh -eu
# ###########################################################################
#             Thomas Dreibholz's R Simulation Scripts Collection
#                  Copyright (C) 2005-2025 Thomas Dreibholz
#
#               Author: Thomas Dreibholz, thomas.dreibholz@gmail.com
# ###########################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

# ====== Check parameters ===================================================
if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 [Simulation]"
   exit 1
fi
if [ ! -e "$1".R ] ; then
   echo "ERROR: $1.R does not exist!"
   exit
fi
if [ ! -e plot-"$1".R ] ; then
   echo "ERROR: plot-$1.R does not exist!"
   exit
fi
if [ ! -e "$1" ] ; then
   echo "ERROR: $1 directory does not exist!"
   exit
fi


if [ $# -gt 1 ] ; then
   DESTINATION="$2"
else
   DESTINATION="."
fi


R --vanilla <plot-"$1".R && \
echo "Step 1: Archiving results only to $DESTINATION/$1.tar.bz2 ..." && \
tar cjf "$DESTINATION"/"$1".tar.bz2 "$1".R plot-"$1".R "$1".pdf "$1"/Results && \
echo "Step 2: Archiving everything to $DESTINATION/$1-complete.tar.bz2 ..." && \
tar cjf "$DESTINATION"/"$1"-complete.tar.bz2 "$1".R plot-"$1".R "$1".pdf "$1"
