#!/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-destroy command which is used by LAVA.

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

if [ "$LXC_NAME" ]; then
    if [ "$LXC_MOCKER_USE_OVERLAY" ]; then
        # At this time we don't care about the LXC container use
        # lazy/force to be sure it gets cleaned.
        umount -lf /var/lib/lxc/${LXC_NAME}/rootfs/dev
        umount -lf /var/lib/lxc/${LXC_NAME}/rootfs/sys
        umount -lf /var/lib/lxc/${LXC_NAME}/rootfs/proc
        umount -lf /var/lib/lxc/${LXC_NAME}/rootfs
    fi

    # Remove lxc rootfs directory if any
    rm -rf /var/lib/lxc/${LXC_NAME}

    # echo container destroyed message
    echo "Destroyed container $LXC_NAME"
    exit 0
fi
