#!/sbin/openrc-run

description="Startup and shutdown ONBOOT vzgot containers"

extra_started_commands="reload"

depend() {
    need localmount net
    need cgroups
    after firewall udev mdev
    use logger
}

start_pre() {
    # 0 We need loop when working in 'ext4' mode
    modprobe -q loop 
    # 1. Need a bride interface specifi to container
    if ! ip link show dev vz-br0 >/dev/null 2>&1; then
        ebegin "Bridge vz-br0 creation"
        ip link add name vz-br0 type bridge
        ip addr add 192.0.2.1/24 dev vz-br0
        ip link set dev vz-br0 up
        sysctl -q net.ipv4.ip_forward=1
        eend $?
    fi
}

stop_post() {
    # 2. cleaning bridge
    if ip link show dev vz-br0 >/dev/null 2>&1; then
        ebegin "Cleaning bridge vz-br0"
        ip link set dev vz-br0 down
        ip link delete vz-br0 type bridge
        eend $?
    fi
}

WEIGHT=10000
CGROUP="/sys/fs/cgroup"
VZGROUP="${CGROUP}/vzgot"
CONFDIR="/etc/vzgot"

# Chargement de la configuration globale
if [ -f "$CONFDIR/vzgot_config" ]; then
    . "$CONFDIR/vzgot_config"
fi

start() {
    ebegin "Starting vzgot containers"

    # Préparation système et cgroups v2
    sysctl --system -q >/dev/null 2>&1
    modprobe fuse >/dev/null 2>&1
    # mounting  loop device (if any)
    mount -a -O loop 2>/dev/null || true

    #cgroup configuration
    #Caution! # CONFIG_RT_GROUP_SCHED need to be set in kernel config
    if [ -f "${CGROUP}/cgroup.controllers" ]; then
        echo "+cpu +cpuset +memory +pids" > "${CGROUP}/cgroup.subtree_control" 2>/dev/null || true
    fi

    mkdir -p "${VZGROUP}"
    if [ -f "${VZGROUP}/cpu.weight" ]; then
        echo "${WEIGHT}" > "${VZGROUP}/cpu.weight"
    fi
    if [ -f "${VZGROUP}/cgroup.controllers" ]; then
        echo "+cpu +cpuset +memory +pids" > "${VZGROUP}/cgroup.subtree_control" 2>/dev/null || true
    fi

    # Balayage des conteneurs à démarrer
    count=0
    if [ -d "$CONFDIR/names" ]; then
        for cont_file in "$CONFDIR/names/"*; do
            [ -e "$cont_file" ] || continue
            cont="${cont_file##*/}"

            ONBOOT=""
            HOMEFS=""
            . "$cont_file"

            onboot=$(echo "$ONBOOT" | tr '[:upper:]' '[:lower:]')
            [ "$onboot" = "yes" ] || continue

            CONTDIR="${VZDIR:-/var/lib/vzgot}/$cont"
            [ -n "$HOMEFS" ] && CONTDIR="$HOMEFS"

            CONTPID=""
            if [ -f "$CONTDIR/first.pid" ]; then
                CONTPID=$(cat "$CONTDIR/first.pid")
            fi
            if [ -n "$CONTPID" ] && [ -e "/proc/$CONTPID" ]; then
                continue
            fi

            delay=$((count * 3 + 10))
            (
                sleep "$delay"
                BOOTLOG="$CONTDIR/bootlog"
                vzgot boot "$cont" > "$BOOTLOG" 2>&1
                date >> "$BOOTLOG"
            ) &

            count=$((count + 1))
        done
    fi

    einfo "vzgot: ${count} container(s) scheduled on starting block"
    eend 0
}

stop() {
    ebegin "Stopping all vzgot containers"

    MAXT=30
    count=0

    if [ ! -d "$CONFDIR/names" ]; then
        eend 0
        return 0
    fi

    tostop=$(ls "$CONFDIR/names" | sort -r)

    # Envoi du signal d'arrêt
    for cont in $tostop; do
        cont_file="$CONFDIR/names/$cont"
        [ -f "$cont_file" ] || continue

        HOMEFS=""
        . "$cont_file"

        CONTDIR="${VZDIR:-/var/lib/vzgot}/$cont"
        [ -n "$HOMEFS" ] && CONTDIR="$HOMEFS"

        CONTPID=""
        if [ -f "$CONTDIR/first.pid" ]; then
            CONTPID=$(cat "$CONTDIR/first.pid")
        fi

        if [ -n "$CONTPID" ] && [ -e "/proc/$CONTPID" ]; then
            sleep 0.2
            vzgot shutdown "$cont" &
            count=$((count + 1))
        fi
    done

    # Boule d'attente d'extinction (max 30s)
    t=1
    remain=0
    while [ "$t" -lt "$MAXT" ]; do
        remain=0
        for cont in $tostop; do
            cont_file="$CONFDIR/names/$cont"
            [ -f "$cont_file" ] || continue

            HOMEFS=""
            . "$cont_file"

            CONTDIR="${VZDIR:-/var/lib/vzgot}/$cont"
            [ -n "$HOMEFS" ] && CONTDIR="$HOMEFS"

            CONTPID=""
            if [ -f "$CONTDIR/first.pid" ]; then
                CONTPID=$(cat "$CONTDIR/first.pid")
            fi

            if [ -n "$CONTPID" ] && [ -e "/proc/$CONTPID" ]; then
                remain=$((remain + 1))
            fi
        done

        if [ "$remain" -eq 0 ]; then
            break
        fi

        einfo "Waiting for containers shutdown, remaining: ${remain}/${count}..."
        sleep 3
        t=$((t + 1))
    done

    if [ "$remain" -eq 0 ]; then
        eend 0 "vzgot: all containers down"
    else
        eend 1 "vzgot: ${remain} container(s) still up after ${MAXT}s"
    fi
}

reload() {
    ebegin "Reloading vzgot configuration"
    eend 0 "Nothing to do"
}

force_reload() {
    ebegin "Force-reload vzgot configuration"
    eend 0 "Nothing to do"
}

status() {
    ebegin "vzgot status"
    eend 0 "Nothing to do"
}

restart() {
    ebegin "Restarting vzgot"
    stop
    sleep 2
    start
    eend $?
}
