#! /bin/sh -
# Copyright (c) 2016-2018 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2020-2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

# Exit immediately on any error
set -e

# Override locale settings
LC_ALL=C
export LC_ALL

# Get current working directory
CWD="$(dirname -- "$0")"

# Functions
unmountSeries()
{
    for node in "${rootdir}/usr/src/qi/archive" \
                "${rootdir}/usr/src/qi/patches" \
                "${rootdir}/usr/src/qi/recipes" \
                "${rootdir}/usr/src/qi/sources" \
                "${rootdir}/sys" \
                "${rootdir}/proc" \
                "${rootdir}/dev/pts" \
                "${rootdir}/dev" ; \
    do
        if mount | grep -q "$node"
        then
            echo "Unmounting ${node} ..."
            if ! umount "$node"
            then
                echo "Doing a lazy unmount for $node ..."
                umount -l "$node"
            fi
        fi
    done
    unset node
}

# Autodetermine the path of /tools

if test ! -L /tools
then
    echo "${PROGRAM}: cannot determine ${rootdir}: /tools does not exist as symlink" 1>&2
    exit 1
fi
if test -L /tools -a ! -e /tools
then
    echo "${PROGRAM}: cannot access /tools: Dangling symlink" 1>&2
    exit 1
fi

# Set canonical name to compose the root directory, removing
# the last component from the path name "/tools"

rootdir="$(CDPATH='' cd -P -- /tools && pwd -P)"
rootdir="${rootdir%/*}"

# Unmount directories and file systems before the next mounting

unmountSeries

# Create required directory for destination
mkdir -p -- "${rootdir}/dev"                 \
            "${rootdir}/dev/pts"             \
            "${rootdir}/proc"                \
            "${rootdir}/sys"                 \
            "${rootdir}/usr/src/qi/archive"  \
            "${rootdir}/usr/src/qi/patches"  \
            "${rootdir}/usr/src/qi/recipes"  \
            "${rootdir}/usr/src/qi/sources"

echo "Mounting virtual file systems ..."
mount -o bind /dev      "${rootdir}/dev"
mount -t devpts devpts  "${rootdir}/dev/pts"
mount -t proc proc      "${rootdir}/proc"
mount -t sysfs sysfs    "${rootdir}/sys"

echo "Binding directories to mount: archive, patches, recipes, sources ..."
mount -o bind "${CWD}/archive" "${rootdir}/usr/src/qi/archive"
mount -o bind "${CWD}/patches" "${rootdir}/usr/src/qi/patches"
mount -o bind "${CWD}/recipes" "${rootdir}/usr/src/qi/recipes"
mount -o bind "${CWD}/sources" "${rootdir}/usr/src/qi/sources"

# Change reported architecture for an appropriate environment
_machine_type=""

if file /tools/bin/busybox | grep -q "32-bit"
then
    echo "Using the linux32 command for this machine type: $(uname -m) ..."
    if command -v linux32 > /dev/null
    then
        _machine_type="linux32"
    else
        echo "    The linux32 command is not available on your system." 1>&2
    fi
fi

# Add trap for unmount the series, signalled or not
trap 'echo ""; unmountSeries' EXIT HUP QUIT ABRT TERM

echo "Entering via chroot $rootdir ..."
$_machine_type chroot "$rootdir" /tools/bin/env -i \
  PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin:/opt/trinity/bin \
  SHELL=/bin/sh \
  HOME=/root \
  TERM=linux \
  HOSTNAME=dragora \
  LC_ALL=C \
  PKG_CONFIG=/usr/bin/pkgconf \
  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig:/opt/trinity/lib/pkgconfig \
  QTDIR=/opt/trinity/lib/tqt3 \
 /bin/sh -

