#!/bin/bash -e

usage()
{
    echo "usage: $0 [minimal|full|status]"
    exit 1
}

if [ ! "$(CreoleGet test_activer_resolvconf_template)" = "oui" ]; then
    echo "SKIP"
    exit 0
fi

level=$1
if [ -z "$level" ]; then
    usage
fi

if [ ! "$level" = "minimal" ] && [ ! "$level" = "full" ] && [ ! "$level" = "status" ]; then
    usage
fi

if [ "$level" = "status" ]; then
    status="$(cat /etc/resolv.conf | awk -F ":" '/# level:/ {print $2}')"
    if [ ! "$status" = "minimal" ] && [ ! "$status" = "full" ]; then
        exit 1
    fi
    echo $status
    exit 0
fi

cp -a --remove-destination /etc/resolv.conf.$level /etc/resolv.conf
if [ "$(CreoleGet mode_conteneur_actif)" = "oui" ]; then
    CreoleRun "cp -a --remove-destination /etc/resolv.conf.$level /etc/resolv.conf" all
elif [ -d /var/lib/lxc/addc/rootfs ]; then
    cp -a /var/lib/lxc/addc/rootfs/etc/resolv.conf.$level /var/lib/lxc/addc/rootfs/etc/resolv.conf
fi
exit 0
