#!/bin/bash

################################################################################
# @file		remote-access
# @author	A. Pietsch <a.pietsch@onsolinno.de>
# @brief	Configures remote access options via mender-configure
# @version	1.0.0
# @date		15.03.2023
# 
# @copyright GPLv3 (c) 2023 A. Pietsch <a.pietsch@onsolinno.de>
# 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 <https://www.gnu.org/licenses/>.
################################################################################


if [ $# -ne 1 ]; then
    echo "Must be invoked with exactly one argument: The JSON configuration." 1>&2
    exit 2
fi

CONFIG="$1"

if ! [ -e "$CONFIG" ]; then
    echo "Error: $CONFIG does not exist." 1>&2
    exit 0
fi

# get MQTT Data
MENDER_CONNECT_SERVICE="$(jq -r -e .enable-mender-connect <"$CONFIG")"
SSH_SERVICE="$(jq -r -e .enable-ssh <"$CONFIG")"


case "$MENDER_CONNECT_SERVICE" in
  "true") systemctl enable mender-connect && systemctl start mender-connect
  ;;
  "false") systemctl disable mender-connect && systemctl stop mender-connect
  ;;
  *) echo "no valid setting"
  ;;
esac

case "$SSH_SERVICE" in
  "true") systemctl enable ssh && systemctl start ssh
  ;;
  "false") systemctl disable ssh && systemctl stop ssh
  ;;
  *) echo "no valid setting"
  ;;
esac
