#!/usr/bin/env bash

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../etc/bash/common.lib.sh"

# Argument 1: Package directory
install_package() {
    [[ "$(echo "$2" | grep -c "no-cache")" = "1" ]] && local cache_support=false
    [[ "$(echo "$2" | grep -c "reset-cache")" = "1" ]] || local reset_cache=false
    local cache_key="$(get_package_cache_key "$1")"
    local package_path="$(cast_package_argument_to_package_path "$1")"
    local exit_code=0 pwd="$(pwd)"

    print_header "Installing" "$(package_path_to_package_name "$1")"

    if ! ${reset_cache} && ${cache_support}; then
        inform_about_sylius_cache
    fi

    # Change current directory to the package one
    cd "$1" 2>/dev/null
    exit_on_error "Cannot change current directory to $1"

    # In case some local composer.lock exists
    rm composer.lock 2>/dev/null

    if ${reset_cache} && ${cache_support}; then
        rm -fr "${SYLIUS_CACHE_DIR}/composer-${cache_key}.lock" 2>/dev/null
        rm -fr "${SYLIUS_CACHE_DIR}/composer-${cache_key}.json.md5sum" 2>/dev/null
    fi

    if [ ! -z "${SYMFONY_VERSION}" ]; then
        $(dirname "${BASH_SOURCE[0]}")/require-symfony-version composer.json "${SYMFONY_VERSION}"
    fi

    if ${cache_support} && has_sylius_cache && is_package_cache_fresh "${package_path}"; then
        print_info "Restoring composer.lock from cache (cache key: ${cache_key})"
        restore_composer_lock_from_cache "${cache_key}"
    fi

    composer_install "$1" || exit_code=$?
    composer show --ansi --no-interaction

    if ${cache_support} && has_sylius_cache && ! is_package_cache_fresh "${package_path}" && [[ ${exit_code} -eq 0 ]]; then
        print_info "Storing composer.lock in cache (cache key: ${cache_key})"
        store_composer_lock_in_cache "${cache_key}"
    fi

    cd "${pwd}" 2>/dev/null
    exit_on_error "Cannot change current directory to ${pwd}"

    return ${exit_code}
}

# Argument 1: Cache key
store_composer_lock_in_cache() {
    cp composer.lock "${SYLIUS_CACHE_DIR}/composer-$1.lock"
    file_md5sum composer.json > "${SYLIUS_CACHE_DIR}/composer-$1.json.md5sum"
}

# Argument 1: Cache key
restore_composer_lock_from_cache() {
    cp "${SYLIUS_CACHE_DIR}/composer-$1.lock" composer.lock
}


# Argument 1: Package path
composer_install() {
    local command="composer install --ansi --no-interaction --prefer-dist --quiet"

    if [ "$(package_path_to_package_name "$1")" = "ResourceBundle" ]; then
        command="${command} --ignore-platform-reqs"
    fi

    run_command "${command}"
}

display_help_message() {
    print_error "Usage: $0 [--no-cache] [--reset-cache] <package paths or names>"
}

main() {
    local packages=() options=() package_path

    while [[ $# -gt 0 ]]; do
        case "$1" in
            --no-cache)
                options+=("no-cache")
            ;;
            --reset-cache)
                options+=("reset-cache")
            ;;
            --help)
                display_help_message
                exit 0
            ;;
            -*)
                print_error "Unknown option \"$1\""
                exit 1
            ;;
            *)
               packages+=("$1")
            ;;
        esac

        shift
    done

    if [[ "${packages[@]}" = "" ]]; then
        display_help_message
        exit 1
    fi

    for package in "${packages[@]}"; do
        package_path="$(cast_package_argument_to_package_path "${package}")"
        exit_on_error "Package \"${package}\" is not found"

        install_package "${package_path}" "${options[*]}"
    done
}

main "$@"
