
api_setup() {
    mkdir -p ${OARCONFDIR}/apache2 || true
    # Check for an old configuration file
    if [ -f ${OARCONFDIR}/apache-api.conf ]; then
        mv ${OARCONFDIR}/apache-api.conf ${OARCONFDIR}/apache2/oar-restful-api.conf
    fi
    install_conffile \
        ${SHAREDIR}/apache2.conf \
        ${OARCONFDIR}/apache2/oar-restful-api.conf \
        0600 ${WWWUSER}

    install_conffile \
        ${SHAREDIR}/api_html_header.pl \
        ${OARCONFDIR}/api_html_header.pl \
        0600 ${OAROWNER}

    install_conffile \
        ${SHAREDIR}/api_html_postform.pl \
        ${OARCONFDIR}/api_html_postform.pl \
        0644 ${OAROWNER}

    install_conffile \
        ${SHAREDIR}/api_html_postform_resources.pl \
        ${OARCONFDIR}/api_html_postform_resources.pl \
        0644 ${OAROWNER}

    install_conffile \
        ${SHAREDIR}/api_html_postform_rule.pl \
        ${OARCONFDIR}/api_html_postform_rule.pl \
        0644 ${OAROWNER}

    install_conffile \
        ${SHAREDIR}/stress_factor.sh \
        ${OARCONFDIR}/stress_factor.sh \
        0700 ${OAROWNER}

    # Install the apache2 configuration file
    #
    # Starting with Debian8, Apache configs are managed using a2enconf/a2disconf/a2query -c, 
    # and stored in the conf-{available,enabled} directories
    if a2query -c > /dev/null 2>&1; then 
        if [ -L  ${APACHECONFDIR}/conf-available/oar-api.conf ]; then
            rm ${APACHECONFDIR}/conf-available/oar-api.conf
        elif [ -f ${APACHECONFDIR}/conf-available/oar-api.conf ]; then
            mv ${APACHECONFDIR}/conf-available/oar-api.conf ${APACHECONFDIR}/conf-available/oar-restful-api.conf
        fi
        if [ ! -e ${APACHECONFDIR}/conf-available/oar-restful-api.conf ]; then
            ln -s ${OARCONFDIR}/apache2/oar-restful-api.conf ${APACHECONFDIR}/conf-available/oar-restful-api.conf
        fi
    # UnTil Debian7, Centos6, Apache configs are stored in the conf.d directory
    elif [ -d ${APACHECONFDIR}/conf.d ]; then
        if [ -L  ${APACHECONFDIR}/conf.d/oar-api.conf ]; then
            rm ${APACHECONFDIR}/conf.d/oar-api.conf
        elif [ -f ${APACHECONFDIR}/conf.d/oar-api.conf ]; then
            mv ${APACHECONFDIR}/conf.d/oar-api.conf ${APACHECONFDIR}/conf.d/oar-restful-api.conf
        fi
        if [ ! -e ${APACHECONFDIR}/conf.d/oar-restful-api.conf ]; then
            ln -s ${OARCONFDIR}/apache2/oar-restful-api.conf ${APACHECONFDIR}/conf.d/oar-restful-api.conf
        fi 
    fi
    
    set_rights ${CGIDIR}/oarapi                  0755 ${OAROWNER} ${OAROWNERGROUP}
    set_rights ${CGIDIR}/oarapi/oarapi.cgi       0755 ${OAROWNER} ${OAROWNERGROUP}
    set_rights ${CGIDIR}/oarapi/oarapi-debug.cgi 0755 ${OAROWNER} ${OAROWNERGROUP}
}
