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