#! /bin/sh
# $FreeBSD: ports/www/openacs/files/pkg-install.in,v 1.7 2008/04/03 12:17:25 mm Exp $

if [ "$2" != "POST-INSTALL" ]; then
        exit 0
fi

OPENACSBASE="/usr/local/dotlrn"

openacs_user="dotlrn"
openacs_uid="821"
openacs_group="dotlrn"
openacs_gid="821"
openacs_gecos=".LRN Daemon User"
openacs_home="/nonexistent"
openacs_shell="/usr/sbin/nologin"

CHMOD=/bin/chmod
CHOWN=/usr/sbin/chown
PW=/usr/sbin/pw

create_group() {
    local group gid

    group=$1
    gid=$2


    if ${PW} group show -n $group >/dev/null 2>&1 ; then
        echo "===> Using existing group $group"
    else
        if ${PW} groupadd -n $group -g $gid ; then
            echo "===> Created group $group"
        else
            cat <<-EOERRORMSG
*** Failed to create group $group.

Please add user $user and group $group
manually with the following commands:

    ${PW} groupadd -n $group -g $gid
    ${PW} useradd -n $user -u $uid -g $group -c "$gecos" \\
        -d $home -s $shell -h -

and retry installing this package.
EOERRORMSG
            exit 1
        fi
    fi

}


create_user() {
    local user uid group gecos home shell

    user=$1
    uid=$2
    group=$3
    gecos=$4
    home=$5
    shell=$6

    if ${PW} user show -n $user >/dev/null 2>&1 ; then
        echo "===> Using existing user $user"
    else
        if ${PW} useradd -n $user -u $uid -g $group -c "$gecos" \
            -d "$home" -s "$shell" -h - ; then
            echo "===> Created user $user"
        else
            cat <<-EOERRORMSG
*** Failed to create user $user.

Please add user $user manually with the following command:

    ${PW} useradd -n $user -u $uid -g $group -c "$gecos" \\
       -d $home -s $shell -h -

and retry installing this package.
EOERRORMSG
            exit 1
        fi
    fi
}

create_group $openacs_group $openacs_gid
create_user  $openacs_user $openacs_uid $openacs_group \
    "$openacs_gecos" "$openacs_home" "$openacs_shell"
echo "===> Changing ownership of ${OPENACSBASE}"
${CHOWN} -R ${openacs_user}:${openacs_group} ${OPENACSBASE}
echo "===> Changing permissions of ${OPENACSBASE}"
${CHMOD} -R 770 ${OPENACSBASE}
