#!/bin/bash
# Copyright 2026 ANNE Media - https://anne.media
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
#
# chroot-target -- mount a target filesystem and chroot into it.
#
# Usage:
#   chroot-target                   List candidate devices
#   chroot-target <device>          Mount and enter
#   chroot-target --umount          Tear down a previous mount
#
# For LUKS: open the container first, then pass the mapper device:
#   cryptsetup open /dev/sdaN cryptroot
#   chroot-target /dev/mapper/cryptroot
#
# For LVM: activate the volume group, then pass the LV:
#   vgchange -ay
#   chroot-target /dev/vg0/root
#
# On exit the target stays mounted. Re-run with a device to enter
# again (previous mounts are torn down first), or --umount to clean.

set -u
TARGET=/target

list_devices() {
    echo "Block devices:"
    echo
    if command -v lsblk >/dev/null 2>&1; then
        lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT 2>/dev/null
    else
        cat /proc/partitions
    fi
    echo
    echo "Open device-mapper devices (LUKS, LVM):"
    if command -v dmsetup >/dev/null 2>&1; then
        dmsetup ls 2>/dev/null | sed 's/^/  /' || echo "  (none)"
    else
        echo "  (dmsetup not available)"
    fi
    echo
    echo "Usage: chroot-target <device>"
    echo "  e.g. chroot-target /dev/sda1"
    echo "       chroot-target /dev/mapper/cryptroot"
    echo "       chroot-target /dev/vg0/root"
}

umount_target() {
    local m
    for m in "$TARGET/run" "$TARGET/sys" "$TARGET/proc" "$TARGET/dev" "$TARGET"; do
        if mountpoint -q "$m" 2>/dev/null; then
            umount "$m" 2>/dev/null || umount -l "$m" 2>/dev/null || true
        fi
    done
    rmdir "$TARGET" 2>/dev/null || true
    echo "Target unmounted."
}

case "${1:-}" in
    ""|"-h"|"--help")
        list_devices
        exit 0
        ;;
    "-u"|"--umount")
        umount_target
        exit 0
        ;;
esac

DEV="$1"
shift || true

[ -b "$DEV" ] || { echo "ERROR: not a block device: $DEV" >&2; exit 1; }

mountpoint -q "$TARGET" 2>/dev/null && umount_target

mkdir -p "$TARGET"
if ! mount "$DEV" "$TARGET"; then
    echo "ERROR: mount failed for $DEV" >&2
    echo "       If the device is LUKS, open it first:" >&2
    echo "         cryptsetup open $DEV cryptroot" >&2
    echo "       If LVM, activate the volume group first:" >&2
    echo "         vgchange -ay" >&2
    exit 1
fi

for m in dev proc sys run; do
    mkdir -p "$TARGET/$m"
    mount --bind "/$m" "$TARGET/$m" 2>/dev/null || true
done

echo "Mounted $DEV at $TARGET."
echo "Entering chroot. Type 'exit' to leave; the mount stays up."
echo "Run 'chroot-target --umount' when done."
echo

if [ -x "$TARGET/bin/bash" ]; then
    exec chroot "$TARGET" /bin/bash -l
elif [ -x "$TARGET/bin/sh" ]; then
    exec chroot "$TARGET" /bin/sh -l
else
    echo "ERROR: no shell found in $TARGET." >&2
    exit 1
fi
