d0811f
#!/bin/bash
d0811f
d0811f
# This script turns sysuser.d files into scriptlets mandated by Fedora
d0811f
# packaging guidelines. The general idea is to define users using the
d0811f
# declarative syntax but to turn this into traditional scriptlets.
d0811f
d0811f
user() {
d0811f
    user="$1"
d0811f
    uid="$2"
d0811f
    desc="$3"
d0811f
    group="$4"
d0811f
    home="$5"
d0811f
    shell="$6"
d0811f
ae4880
    [ "$desc" = '-' ] && desc=
ae4880
    { [ "$home" = '-' ] || [ "$home" = '' ]; } && home=/
ae4880
    { [ "$shell" = '-' ] || [ "$shell" = '' ]; } && shell=/sbin/nologin
d0811f
ae4880
    if [ "$uid" = '-' ] || [ "$uid" = '' ]; then
ae4880
        cat <
d0811f
getent passwd '$user' >/dev/null || \\
d0811f
    useradd -r -g '$group' -d '$home' -s '$shell' -c '$desc' '$user'
d0811f
EOF
ae4880
    else
ae4880
        cat <
d0811f
if ! getent passwd '$user' >/dev/null ; then
d0811f
    if ! getent passwd '$uid' >/dev/null ; then
d0811f
        useradd -r -u '$uid' -g '$group' -d '$home' -s /sbin/nologin -c '$desc' '$user'
d0811f
    else
d0811f
        useradd -r -g '$group' -d '$home' -s /sbin/nologin -c '$desc' '$user'
d0811f
    fi
d0811f
fi
d0811f
d0811f
EOF
ae4880
    fi
d0811f
}
d0811f
d0811f
group() {
d0811f
    group="$1"
d0811f
    gid="$2"
ae4880
    if [ "$gid" = '-' ]; then
ae4880
        cat <<-EOF
ae4880
	getent group '$group' >/dev/null || groupadd -r '$group'
ae4880
	EOF
ae4880
    else
ae4880
        cat <<-EOF
ae4880
	getent group '$group' >/dev/null || groupadd -f -g '$gid' -r '$group'
ae4880
	EOF
ae4880
    fi
d0811f
}
d0811f
d0811f
parse() {
ae4880
    while read -r line || [ -n "$line" ] ; do
ae4880
        { [ "${line:0:1}" = '#' ] || [ "${line:0:1}" = ';' ]; } && continue
d0811f
        line="${line## *}"
d0811f
        [ -z "$line" ] && continue
ae4880
        eval "arr=( $line )"
d0811f
        case "${arr[0]}" in
d0811f
            ('u')
d0811f
                group "${arr[1]}" "${arr[2]}"
d0811f
                user "${arr[1]}" "${arr[2]}" "${arr[3]}" "${arr[1]}" "${arr[4]}" "${arr[5]}"
d0811f
                # TODO: user:group support
d0811f
                ;;
d0811f
            ('g')
d0811f
                group "${arr[1]}" "${arr[2]}"
d0811f
                ;;
d0811f
            ('m')
d0811f
                group "${arr[2]}" "-"
d0811f
                user "${arr[1]}" "-" "" "${arr[2]}"
d0811f
                ;;
d0811f
        esac
d0811f
    done
d0811f
}
d0811f
d0811f
for fn in "$@"; do
d0811f
    [ -e "$fn" ] || continue
ae4880
    echo "# generated from $(basename "$fn")"
ae4880
    parse <"$fn"
d0811f
done