create_oar_group() {
    if ! getent group | grep -q "^${OAROWNERGROUP}:"; then 
        echo -n "Adding group ${OAROWNERGROUP}.."
        case "$TARGET_DIST" in
            "debian")
                addgroup --quiet --system ${OAROWNERGROUP} 2>/dev/null || true
                ;;
            *)
                groupadd --system ${OAROWNERGROUP} 2>/dev/null  >/dev/null || true
                ;;
        esac
        echo "..done"
    fi
}

create_oar_home() {
    test -d "${OARHOMEDIR}" || mkdir -p ${OARHOMEDIR}
}

create_oar_user() {    
    # Create oar user
    if ! getent passwd | grep -q "^${OAROWNER}:"; then
        echo -n "Adding system user ${OAROWNER}.."
        case "$TARGET_DIST" in
            "debian")
                adduser --quiet \
                        --system \
                        --ingroup ${OAROWNERGROUP} \
                        --shell /bin/bash \
                        --no-create-home \
                        --disabled-password \
                        ${OAROWNER} 2>/dev/null || true
                ;;
            *)
                useradd --system \
                        --gid ${OAROWNERGROUP} \
                        --shell /bin/bash \
                        --password "*" \
                        ${OAROWNER} >/dev/null 2>&1
                passwd -u ${OAROWNER} >/dev/null 2>&1 ||true
                ;;
        esac
        echo "..done"
    fi

    # Adjust the password entry
    usermod -d ${OARHOMEDIR} \
            -g ${OAROWNERGROUP} \
               ${OAROWNER}

    # Adjust the file and directory permissions
    case "$SETUP_TYPE" in
        "deb")
            if ! dpkg-statoverride --list ${OARHOMEDIR} >/dev/null; then
                chown $OAROWNER:$OAROWNERGROUP $OARHOMEDIR
                chmod u=rwx,g=rxs,o= $OARHOMEDIR
            fi
            ;;
        *)
            chown $OAROWNER:$OAROWNERGROUP $OARHOMEDIR
            chmod u=rwx,g=rxs,o= $OARHOMEDIR
            ;;
    esac

    # set OAR shell
    if [ "`getent passwd ${OAROWNER} |cut -f7 -d:`" != "${OARDIR}/oarsh_shell" ]; then 
        chsh -s ${OARDIR}/oarsh_shell ${OAROWNER} 
    fi 

    # Fix the bash profile
    cat > ${OARHOMEDIR}/.bash_oar <<EOF
#
# OAR bash environnement file for the oar user
#
# /!\ This file is automatically created at update installation/upgrade. 
#     Do not modify this file.
#

bash_oar() {
    # Prevent to be executed twice or more
    [ -n "\$OAR_BASHRC" ] && return

    export PATH="%%OARDIR%%/oardodo:\$PATH"
    OAR_BASHRC=yes
}

bash_oar
EOF
    # Default bash sourced file in a batch job (BASH_ENV)
    [ ! -f "${OARHOMEDIR}/.batch_job_bashrc" ] && cat > ${OARHOMEDIR}/.batch_job_bashrc <<EOF
#
# OAR bash environnement file for only the batch job users
#

source ~/.bashrc

EOF
    touch ${OARHOMEDIR}/.bash_profile
    touch ${OARHOMEDIR}/.bashrc
    if ! grep -q "${OARHOMEDIR}/.bash_oar" ${OARHOMEDIR}/.bash_profile 2> /dev/null; then
        echo '' >> ${OARHOMEDIR}/.bash_profile
        echo "[ -f ${OARHOMEDIR}/.bash_oar ] && . ${OARHOMEDIR}/.bash_oar" >> ${OARHOMEDIR}/.bash_profile
    fi
    if ! grep -q "${OARHOMEDIR}/.bash_oar" ${OARHOMEDIR}/.bashrc 2> /dev/null; then
        echo '' >> ${OARHOMEDIR}/.bashrc
        echo "[ -f ${OARHOMEDIR}/.bash_oar ] && . ${OARHOMEDIR}/.bash_oar" >> ${OARHOMEDIR}/.bashrc
    fi

    set_rights ${OARHOMEDIR} 0755 ${OARUSER} ${OAROWNERGROUP}
    chown ${OAROWNER}:${OAROWNERGROUP} ${OARHOMEDIR}/.bash_oar
    chown ${OAROWNER}:${OAROWNERGROUP} ${OARHOMEDIR}/.bash_profile
    chown ${OAROWNER}:${OAROWNERGROUP} ${OARHOMEDIR}/.bashrc
}

install_run_dir() {
    # nothing to do
    :
}

create_log_file() {
    # Log file
    touch ${LOGDIR}/oar.log && chown ${OAROWNER}:${ROOTGROUP} ${LOGDIR}/oar.log && chmod 0644 ${LOGDIR}/oar.log || true
}

common_setup() {

    # Update before 2.5.2: Fix a bug causing an empty ${OARHOMEDIR}/.bash_oar
    if [ -e ${OARHOMEDIR}/.bash_oar ] && [ -z "$(cat ${OARHOMEDIR}/.bash_oar)" ]; then
        rm -f ${OARHOMEDIR}/.bash_oar
    fi

    create_oar_group
    create_oar_home
    create_oar_user
    install_run_dir
    create_log_file

    mkdir -p ${OARCONFDIR}/

    install_conffile \
        ${SHAREDIR}/oar.conf \
        ${OARCONFDIR}/oar.conf \
        0600 ${OAROWNER}:${ROOTGROUP}

    install_conffile \
        ${SHAREDIR}/oarnodesetting_ssh \
        ${OARCONFDIR}/oarnodesetting_ssh \
        0755

    install_conffile \
        ${SHAREDIR}/update_cpuset_id.sh \
        ${OARCONFDIR}/update_cpuset_id.sh \
        0755

    set_rights ${BINDIR}/oarsh           6755 ${OARDO_DEFAULTUSER} ${OARDO_DEFAULTGROUP}
    set_rights ${SBINDIR}/oarnodesetting 6750 ${OARDO_DEFAULTUSER} ${OARDO_DEFAULTGROUP}

    set_rights ${OARDIR}/oardodo/oardodo 6750 ${ROOTUSER} ${OAROWNERGROUP}
    set_rights ${OARDIR}/oardodo         0755 ${ROOTUSER} ${OAROWNERGROUP}
}
