#!/bin/bash

AST_ETC_PATH="/etc/asterisk"
AST_USR_PATH="/usr/share/asterisk"
AST_SPOOL_PATH="/var/spool/asterisk"
AST_VARLIB_PATH="/var/lib/asterisk"
XIVO_VARLIB_PATH="/var/lib/xivo"

AST_VARLIB_MOH_PATH="${AST_VARLIB_PATH}/moh"
AST_USR_MOH_PATH="${AST_USR_PATH}/moh"
XIVO_MOH_PATH="${XIVO_VARLIB_PATH}/moh"

XIVO_WEBI_MAKE_DIRS="${AST_SPOOL_PATH}/monitor
                ${AST_VARLIB_MOH_PATH}/default
                ${AST_VARLIB_PATH}/sounds/custom
                ${XIVO_VARLIB_PATH}/sounds/acd
                ${XIVO_VARLIB_PATH}/sounds/features
                ${XIVO_VARLIB_PATH}/sounds/playback
                ${XIVO_VARLIB_PATH}/ivrsounds"

XIVO_WEBI_FIX_PATH_RIGHTS="${AST_ETC_PATH}
               ${AST_USR_PATH}/moh
               ${AST_SPOOL_PATH}/fax
               ${AST_SPOOL_PATH}/monitor
               ${AST_VARLIB_MOH_PATH}
               ${AST_VARLIB_PATH}/sounds/custom
               ${XIVO_VARLIB_PATH}
               ${XIVO_VARLIB_PATH}/sounds
               ${XIVO_VARLIB_PATH}/ivrsounds"

XIVO_AGI_FIX_PATH_RIGHTS="${AST_VARLIB_PATH}/agi-bin"

create_webi_dirs() {
    echo "${XIVO_WEBI_MAKE_DIRS}"|while read XIVO_WEBI_DIR;
    do
        mkdir -p "${XIVO_WEBI_DIR}";
    done;
}

create_symlinks() {
    if [ ! -L "${AST_USR_MOH_PATH}" ]; then
        if [ ! -L "${AST_USR_MOH_PATH}" ]; then
            ln -s "${AST_VARLIB_MOH_PATH}" "${AST_USR_MOH_PATH}"
        fi
        if [ ! -L "${XIVO_MOH_PATH}" ]; then
            ln -s "${AST_VARLIB_MOH_PATH}" "${XIVO_MOH_PATH}"
        fi
    fi
}

give_rights_for_dirs() {
    local path_owner="${1}"; shift
    local path_group="${1}"; shift
    local path_dir_mode="${1}"; shift
    local path_file_mode="${1}"; shift
    local paths_to_fix="${1}"; shift

    local path_to_fix

    echo "${paths_to_fix}"|while read path_to_fix;
    do
        chown -R "${path_owner}:${path_group}" "${path_to_fix}"
        find "${path_to_fix}" -type d -exec chmod "${path_dir_mode}" '{}' \;
        find "${path_to_fix}" -type f -exec chmod "${path_file_mode}" '{}' \;
    done;
}

give_rights_for_files() {
    local path_owner="${1}"; shift
    local path_group="${1}"; shift
    local path_file_mode="${1}"; shift
    local paths_to_fix="${1}"; shift

    local path_to_fix

    echo "${paths_to_fix}"|while read path_to_fix;
    do
        chown -R "${path_owner}:${path_group}" "${path_to_fix}"
        find "${path_to_fix}" -type f -exec chmod "${path_file_mode}" '{}' \;
    done;
}

main() {
    # By default we do everything (old behavior)
    local module="${1:-all}"
    if [ $# -eq 1 ]; then
        module="${1}";
    fi
    # We currently only handle option asteriskconf
    if [ "${module}" != "asteriskconf" ]; then
        module="all"
    fi

    # Default rights and ownership for files linked to webi
    local webi_paths_owner=asterisk
    local webi_paths_group=www-data
    local webi_paths_dir_mode=2775
    local webi_paths_file_mode=660

    # Default rights and ownership for AGI scripts
    local asterisk_agi_owner=asterisk
    local asterisk_agi_group=asterisk
    local asterisk_agi_file_mode=775

    if [ "${module}" == "all" ]; then
        create_webi_dirs
        create_symlinks

        give_rights_for_dirs ${webi_paths_owner} ${webi_paths_group} ${webi_paths_dir_mode} ${webi_paths_file_mode} "${XIVO_WEBI_FIX_PATH_RIGHTS}"
        give_rights_for_files ${asterisk_agi_owner} ${asterisk_agi_group} ${asterisk_agi_file_mode} "${XIVO_AGI_FIX_PATH_RIGHTS}"
    elif [ "${module}" = "asteriskconf" ]; then
        # Applies rights only to /etc/asterisk/ dir
        give_rights_for_dirs ${webi_paths_owner} ${webi_paths_group} ${webi_paths_dir_mode} ${webi_paths_file_mode} "${AST_ETC_PATH}"
    fi
}

main "${@}"
