#!/bin/bash
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018-2021 Linaro Limited
#
# Author: Senthil Kumaran S <senthil.kumaran@linaro.org>
#
# This file is part of LAVA LXC mocker.
#
# Released under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
#
# Mocks lxc-attach command which is used by LAVA.

CMD=$(awk -F'-- ' '{print $2}' <<< "$@")

while getopts "n:v:" opt; do
    case $opt in
        n)
            LXC_NAME="$OPTARG"
            ;;
        v)
            export "$OPTARG"
            ;;
        *)
            ;;
    esac
done

if [ "$CMD" ]; then
    # execute the given command
    if [ "$LXC_MOCKER_USE_OVERLAY" ]; then
        chroot /var/lib/lxc/${LXC_NAME}/rootfs $CMD
    else
        $CMD
    fi
else
    # when no commands are requested, open up a shell
    if [ "$LXC_MOCKER_USE_OVERLAY" ]; then
        chroot /var/lib/lxc/${LXC_NAME}/rootfs /bin/bash
    else
        exec /bin/bash
    fi
fi
