#!/bin/sh
# Script to run tmux inside toolbox
# Copyright (C) 2023 Georg Pfuetzenreuter <mail+opensuse@georg-pfuetzenreuter.net>
#
# 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/>.

set -Ceu

command -v toolbox >/dev/null

get_toolboxes() {
  podman ps -qf name=toolbox
}

run_inside() {
  podman exec "$container" "$@"
}

if [ "$(get_toolboxes | wc -l)" == 0 ]
then
  reinit=yes
  toolbox -n true
fi

container="$(get_toolboxes | head -n1)"

if [ "${reinit-:no}" == 'yes' ]
then
  podman exec "$container" zypper mr -d repo-non-oss repo-update-non-oss
  podman exec "$container" zypper -n in tmux
fi
podman exec -it "$container" tmux new-session -As toolmux
