#! /bin/sh
# $FreeBSD: www/openacs/files/pkg-install.in 300897 2012-07-14 14:29:18Z beat $

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

OPENACSBASE="/usr/local/openacs"

openacs_user="openacs"
openacs_uid="820"
openacs_group="openacs"
openacs_gid="820"
openacs_gecos="OpenACS 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}
