#!/bin/sh

#
# lxc: linux Container library

# 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

. /usr/share/lxc/lxc.functions

usage()
{
	echo "usage: $(basename $0) [--active] [--] [LS_OPTIONS...]" >&2
}

help() {
	usage
	echo >&2
	echo "List containers existing on the system." >&2
	echo >&2
	echo "  --active     list active containers" >&2
	echo "  LS_OPTIONS   ls command options (see \`ls --help')" >&2
}

get_parent_cgroup()
{
	parent_cgroup=""

	# Obtain a list of hierarchies that contain one or more subsystems
	hierarchies=$(tail -n +2 /proc/cgroups | cut -f 2)

	# Iterate through the list until a suitable hierarchy is found
	for hierarchy in $hierarchies; do
		# Obtain information about the init process in the hierarchy
		fields=$(grep -E "^$hierarchy:" /proc/1/cgroup | head -n 1)
		if [ -z "$fields" ]; then continue; fi
		fields=${fields#*:}

		# Get a comma-separated list of the hierarchy's subsystems
		subsystems=${fields%:*}

		# Get the cgroup of the init process in the hierarchy
		init_cgroup=${fields#*:}

		# Get the filesystem mountpoint of the hierarchy
		mountpoint=$(awk -v subsysregex="(^|,)$subsystems(,|\$)" \
			'$3 == "cgroup" && $4 ~ subsysregex {print $2}' /proc/self/mounts)
		if [ -z "$mountpoint" ]; then continue; fi

		# Return the absolute path to the containers' parent cgroup
		# (do not append '/lxc' if the hierarchy contains the 'ns' subsystem)
		case ",$subsystems," in
			*,ns,*) parent_cgroup="${mountpoint}${init_cgroup%/}";;
			*) parent_cgroup="${mountpoint}${init_cgroup%/}/lxc";;
		esac
		break
	done
}

directory=$(readlink -f "$lxc_path")

for i in "$@"; do
	case $i in
		--help)
			help; exit;;
		--active)
			get_parent_cgroup; directory="$parent_cgroup"; shift;;
		--)
			shift; break;;
		*)
			break;;
	esac
done

containers=""
if [ ! -z "$directory" ]; then
	if [ x"$parent_cgroup" = x ]; then
		containers=$(find -L $directory -mindepth 2 -maxdepth 2 -name config -type f |awk -F "/" '{print $(NF-1)}')
	else
		containers=$(find $directory -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sed 's:.*/::')
	fi
	if [ x"$containers" = x ]; then
		exit 0
	fi
fi

cd "$directory"
ls -d $@ -- $containers
