#!/bin/sh
# @(#)$KimmoSuominen: .cfg/home/bin/share/sandbox,v 1.10 2025/08/04 14:52:42 kim Exp $
#
# Copyright (c) 2020-2025 Kimmo Suominen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
# Start a login session inside a sandbox chroot.
#
# 20200530  Kimmo Suominen
#

set -eu

SBBASE=/p/sandbox

PROG="${0##*/}"

die()
{
    echo "${PROG}: ${@}" 1>&2
    exit 1
}

warn()
{
    echo "${PROG}: ${@}" 1>&2
}

usage()
{
    cat <<EOF
Usage:	${PROG} [-Du] [sandbox]
	${PROG} -h
	${PROG} -l

Start a simulated login session inside a sandbox chroot.

Options:
-D	Delete sandbox.
-u	Unmount sandbox.
-h	Show this usage message.
-l	List already created sandboxes.
EOF
}

mode=run
while getopts Dhlu opt
do
    case "${opt}" in
    D) mode=destroy;;
    h) usage; exit 0;;
    l) mode=list;;
    u) mode=unmount;;
    *) usage 1>&2; exit 1;;
    esac
done
shift $((${OPTIND} - 1))

HOST_ARCH="$(uname -m)"

SANDBOX="${1:-${HOST_ARCH}}"

case "${SANDBOX}" in
amd64|*-amd64|i386|*-i386)
    ARCH="${SANDBOX##*-}"
    case "${HOST_ARCH}-${ARCH}" in
    amd64-amd64|amd64-i386|i386-i386)
	;;
    *)
	die "Cannot run ${ARCH} on ${HOST_ARCH}"
	;;
    esac
    ;;
*-*)
    die "Don't know how to setup ${SANDBOX} (arch)"
    ;;
*)
    ARCH="${HOST_ARCH}"
    ;;
esac

VERSION="$(echo "${SANDBOX}" | sed -e 's/^nb//' -e 's/-.*$//')"

case "${VERSION}" in
amd64|i386)
    VERSION="$(uname -r | sed -e 's/_.*$//')"
    ;;
esac

case "${VERSION}" in
# 7|7.2)
#     VERSION=7
#     REL=7.2
#     case "${TERM}" in
#     screen.xterm-256color)
# 	TERM=screen-256color
# 	;;
#     esac
#     ;;
# 8|8.2)
#     VERSION=8
#     REL=8.2
#     ;;
# 9|9.4)
#     VERSION=9
#     REL=9.4
#     ;;
10|10.1)
    VERSION=10
    REL=10.1
    ;;
[123456789][0123456789].99.*)
    REL="${VERSION}"
    ;;
*)
    die "Don't know how to setup ${SANDBOX} (release ${VERSION})"
    ;;
esac

SANDBOX="nb${VERSION}-${ARCH}"

case "${mode}" in
destroy|unmount)
    sudo env ARCH="${ARCH}" REL="${REL}" SANDBOX="${SANDBOX}" SHELL="${SHELL}" \
	sandboxctl "${mode}"
    ;;
list)
    ls -1 "${SBBASE}/chroot"
    ;;
run)
    if [ ! -d "${SBBASE}/chroot/${SANDBOX}" ]
    then
	warn "Creating ${SANDBOX}"
	sudo env ARCH="${ARCH}" REL="${REL}" SANDBOX="${SANDBOX}" SHELL="${SHELL}" \
	    sandboxctl create
    fi

    SBLOGIN=/home/sandbox/bin/sandbox-login

    case "$(uname -r)-$(uname -m)" in
    "${REL}-${ARCH}")
	sudo env ARCH="${ARCH}" REL="${REL}" SANDBOX="${SANDBOX}" SHELL="${SHELL}" \
	    sandboxctl run su "${USER}" -c \
	    "exec '${SBLOGIN}' '${SANDBOX}'"
	;;
    *)
	sudo env ARCH="${ARCH}" REL="${REL}" SANDBOX="${SANDBOX}" SHELL="${SHELL}" \
	    sandboxctl run su "${USER}" -c \
	    "exec kver -r '${REL}' -m '${ARCH}' '${SBLOGIN}' '${SANDBOX}'"
	;;
    esac
    ;;
*)
    die "Unknown mode ${mode}"
    ;;
esac
