#!/bin/sh
#
# Copyright (c) 2017, The Linux Foundation. All rights reserved.
#
# lxc: linux Container library
#
# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# lxc-qsdk: Template for linux container support

source /lib/config/uci.sh
# Make sure the usual locations are in PATH
export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin

OPT=$(getopt -o p:n:h -l path:,name:,rootfs:,help -- "$@")
eval set -- "$OPT"

usage()
{
	
echo " \"$1\" -h|--help -p|--p=<path>"
return 0
}


while true
do
    case "$1" in
        -h|--help)	usage $0 && exit 0;;
	-p|--path)      path=$2; shift 2;;
        --rootfs)       rootfs=$2; shift 2;;
        -n|--name)      name=$2; shift 2;;
       --)             shift 1; break ;;
        *)              break ;;
    esac
done


rootfs=$path/rootfs

mkdir -p $rootfs && cd $rootfs && tar -xzf $(uci_get lxc.@common[0].image)
grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
grep -q "^lxc.haltsignal" $path/config 2>/dev/null || echo "lxc.haltsignal= SIGUSR1"  >> $path/config
grep -q "^lxc.include" $path/config 2>/dev/null || echo "lxc.include =/usr/share/lxc/config/openwrt.common.conf" >> $path/config
