#!/bin/bash

#
#	Variables
#
CHROOT_PATH=/opt/chroot/sid

do_mount()
{
	m=$(cat /proc/mounts | cut -d" " -f2 | egrep $3)

	if [ -z "$m" ]; then
		mount $1 $2 $3
	fi
}

mount_special ()
{
	echo "Mounting special filesystems ..."

	do_mount "-t proc"    "proc"     "${CHROOT_PATH}/proc"
	do_mount "-t usbfs"	  "/proc/bus/usb" "${CHROOT_PATH}/proc/bus/usb"
	do_mount "-o rw,bind" "/dev/"    "${CHROOT_PATH}/dev"
	do_mount "-o rw,bind" "/dev/pts" "${CHROOT_PATH}/dev/pts"
	do_mount "-t tmpfs"   "tmpfs"    "${CHROOT_PATH}/dev/shm"
	do_mount "-o rw,bind" "/home"    "${CHROOT_PATH}/home"
	do_mount "-o rw,bind" "/tmp"     "${CHROOT_PATH}/tmp"
	do_mount "-o rw,bind" "/var/run/nut" "${CHROOT_PATH}/var/run/nut"
}

umount_all() 
{
	umount ${CHROOT_PATH}/proc
	umount ${CHROOT_PATH}/proc/bus/usb
	umount ${CHROOT_PATH}/dev
	umount ${CHROOT_PATH}/dev/pts
	umount ${CHROOT_PATH}/dev/shm
	umount ${CHROOT_PATH}/home
	umount ${CHROOT_PATH}/tmp
	umount ${CHROOT_PATH}/var/run/nut
}

case $1 in
start)
	#
	#	Montaje especial
	mount_special

	#	
	#	Copia de archivos de configuración
	#
	echo "Copying NUT configuration ..."
	cp -R -a /etc/nut/* "${CHROOT_PATH}/etc/nut/"
	#chgrp nut ${CHROOT_PATH}/etc/nut/*
	cp -a /etc/default/nut "${CHROOT_PATH}/etc/default/"

	#
	#	Copia de archivos del sistema
	echo "Copying system configuration files ..."
	cp /etc/passwd "${CHROOT_PATH}/etc/passwd"
	cp /etc/shadow "${CHROOT_PATH}/etc/shadow"
	cp /etc/group  "${CHROOT_PATH}/etc/group"
	;;
stop)
	umount_all
	;;
esac
