#!/bin/sh

UID_MIN=$(grep '^UID_MIN' /etc/login.defs 2>/dev/null|sed -r 's,UID_MIN[[:space:]]+,,')
[ -z "$UID_MIN" ] && UID_MIN=500

shell_add_or_subst()
{
	local name="$1" && shift
	local value="$1" && shift
	local file="$1" && shift

	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file"; then
		/bin/sed -r -i "s,^$name.*,$name$value," -- "$file"
		return 0
	fi
	printf %s\\n "$name$value" >> "$file"
}


hasher_useradd_all()
{
    local IFS=:
    
    getent passwd|
    while read name password uid gid gecos home shell; do
        [ "$uid" -ge "$UID_MIN" ] || continue
	[ "$shell" == "/sbin/nologin" ] || grep -qs "^$shell$" /etc/shells || continue
	/usr/sbin/hasher-useradd "$name"
    done
}

hasher_conf()
{
    local file=/etc/hasher-priv/system
    shell_add_or_subst "allowed_mountpoints=" "/proc" "$file"
}

apt_conf()
{
    local file=/etc/apt/apt.conf
    
    [ ! -s "$file" ] ||
	sed 's/Acquire::CDROM::Copy.*/Acquire::CDROM::Copy "true";/' -i "$file"

    sed -r 's,(^rpm[[:space:]]+cdrom.*),#\1,' \
	-i /etc/apt/sources.list.d/*.list \
	/etc/apt/sources.list
}

setup_be()
{
    hasher_useradd_all
    hasher_conf
    apt_conf
}

setup_be
