#!/bin/bash -
#
# Package: Reference Standard M
# File:    rsm/bin/docker
# Summary: Run an RSM Docker container
#
# David Wicksell <dlw@linux.com>
# Copyright © 2022-2024 Fourth Watch Software LC
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License (AGPL) 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 Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.
#
# SPDX-FileCopyrightText:  © 2022 David Wicksell <dlw@linux.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
#
# Start the RSM environment, passing the max number of jobs via the
# 'docker run...' command, defaulting to 12

rsm -j "${1:-12}"

echo -e "\nReference Standard M $(rsm -V || true) Docker Container\n"
echo "Copyright © 2020-2024 Fourth Watch Software LC"
echo -e "https://gitlab.com/Reference-Standard-M/rsm\n"

if [[ $# -gt 1 ]]
then
    shift

    # Pass the rest of the arguments from the 'docker run...' command to RSM
    rsm -x "$@"
else
    # Run RSM in direct mode when the container is not detached, keep the container open when it is detached
    rsm

    # Check to see if RSM is running and if not (single-user), keep the container open while in detached mode
    if ! rsm -i &>/dev/null
    then
        read -r
    fi
fi

echo

# Stop and remove the RSM environment
if rsm -i &>/dev/null
then
    rsm -k
fi

exit 0

# ex: filetype=bash
