#!/bin/sh

# Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# 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/>.


usage()
{
	echo "Usage: $0"
	exit 1
}

if [ $# -eq 1 ] ; then
	usage
fi

root="/srv/distros/trisquel9-i686"
username=gnutoo

for d in dev dev/pts dev/shm etc/machine-id proc sys tmp/.X11-unix ; do
    sudo mount | grep " on ${root}/$d" > /dev/null || sudo mount -o bind /$d ${root}/$d
done

if [ -f ${XAUTHORITY} ] ; then
    sudo mount | grep " on ${root}/${XAUTHORITY}" > /dev/null || \
	    sudo mount -o bind ${XAUTHORITY} ${root}/${XAUTHORITY}
fi

sudo chroot "${root}" /bin/bash --login -c \
	"/usr/bin/sudo XAUTHORITY=${XAUTHORITY} DISPLAY=${DISPLAY} WAYLAND_DISPLAY=${WAYLAND_DISPLAY} -i -u ${username};"
