<commit_before>#!/usr/bin/env bash

koopa::find_app_version() { # {{{1
    # """
    # Find the latest application version.
    # @note Updated 2020-11-22.
    # """
    local name prefix x
    koopa::assert_has_args "$#"
    name="${1:?}"
    prefix="$(koopa::app_prefix)"
    koopa::assert_is_dir "$prefix"
    prefix="${prefix}/${name}"
    koopa::assert_is_dir "$prefix"
    x="$( \
        find "$prefix" \
            -mindepth 1 \
            -maxdepth 1 \
            -type d \
        | sort \
        | tail -n 1 \
    )"
    koopa::assert_is_dir "$x"
    x="$(basename "$x")"
    koopa::print "$x"
    return 0
}

# FIXME THIS NEEDS AN LDCONFIG UPDATE CALL AT THE END.
koopa::install_app() { # {{{1
    # """
    # Install application into a versioned directory structure.
    # @note Updated 2021-04-27.
    # """
    local include_dirs jobs link_args link_app make_prefix name name_fancy \
        prefix reinstall script script_name script_prefix tmp_dir version
    koopa::assert_has_args "$#"
    koopa::assert_has_no_envs
    include_dirs=
    link_app=1
    name_fancy=
    reinstall=0
    script_name=
    script_prefix="$(koopa::prefix)/include/install"
    version=
    while (("$#"))
    do
        case "$1" in
            --include-dirs=*)
                include_dirs="${1#*=}"
                shift 1
                ;;
            --name=*)
                name="${1#*=}"
                shift 1
                ;;
            --name-fancy=*)
                name_fancy="${1#*=}"
                shift 1
                ;;
            --no-link)
                link_app=0
                shift 1
                ;;
            --reinstall|--force)
                reinstall=1
                shift 1
                ;;
            --script-name=*)
                script_name="${1#*=}"
                shift 1
                ;;
            --script-prefix=*)
                script_prefix="${1#*=}"
                shift 1
                ;;
            --version=*)
                version="${1#*=}"
                shift 1
                ;;
            --verbose)
                set -x
                shift 1
                ;;
            "")
                shift 1
                ;;
            *)
                koopa::invalid_arg "$1"
                ;;
        esac
    done
    koopa::assert_has_no_args "$#"
    [[ -z "$name_fancy" ]] && name_fancy="$name"
    [[ -z "$script_name" ]] && script_name="$name"
    [[ -z "$version" ]] && version="$(koopa::variable "$name")"
    if koopa::is_macos
    then
        koopa::assert_is_installed brew
        link_app=0
    fi
    prefix="$(koopa::app_prefix)/${name}/${version}"
    make_prefix="$(koopa::make_prefix)"
    [[ "$reinstall" -eq 1 ]] && koopa::sys_rm "$prefix"
    if [[ -d "$prefix" ]]
    then
        koopa::alert_note "${name_fancy} already installed at '${prefix}'."
        return 0
    fi
    koopa::install_start "$name_fancy" "$version" "$prefix"
    tmp_dir="$(koopa::tmp_dir)"
    (
        koopa::cd "$tmp_dir"
        # FIXME REWORK THIS, DEFINING PER SCRIPT INSTEAD.
        jobs="$(koopa::cpu_count)"
        export INSTALL_JOBS="$jobs"
        export INSTALL_LINK_APP="$link_app"
        export INSTALL_NAME="$name"
        export INSTALL_PREFIX="$prefix"
        export INSTALL_REINSTALL="$reinstall"
        export INSTALL_VERSION="$version"
        script="${script_prefix}/${script_name}.sh"
        koopa::assert_is_file "$script"
        # shellcheck source=/dev/null
        . "$script"
    ) 2>&1 | tee "$(koopa::tmp_log_file)"
    koopa::rm "$tmp_dir"
    koopa::sys_set_permissions -r "$prefix"
    # FIXME This will cause issues with r, python?
    koopa::link_into_opt "$prefix" "$name"
    if [[ "$link_app" -eq 1 ]]
    then
        koopa::delete_broken_symlinks "$make_prefix"
        link_args=(
            "--name=${name}"
            "--version=${version}"
        )
        if [[ -n "$include_dirs" ]]
        then
            link_args+=("--include-dirs=${include_dirs}")
        fi
        # We're including the 'true' catch here to avoid cp issues on Arch.
        koopa::link_app "${link_args[@]}" || true
    fi
    # Ensure ldconfig is current, if necessary.
    if [[ "$make_prefix" == '/usr/local' ]]
    then
        sudo ldconfig
    fi
    koopa::install_success "$name_fancy" "$prefix"
    return 0
}

koopa::link_app() { # {{{1
    # """
    # Symlink application into build directory.
    # @note Updated 2021-04-26.
    #
    # If you run into permissions issues during link, check the build prefix
    # permissions. Ensure group is not 'root', and that group has write access.
    #
    # This can be reset easily with 'koopa::sys_set_permissions'.
    #
    # Note that Debian symlinks 'man' to 'share/man', which is non-standard.
    # This is currently corrected in 'install-debian-base', but top-level
    # symlink checks may need to be added here in a future update.
    #
    # @section cp flags:
    # * -f, --force
    # * -R, -r, --recursive
    # * -s, --symbolic-link
    #<commit_msg>Improve config<commit_after>#!/usr/bin/env bash

koopa::find_app_version() { # {{{1
    # """
    # Find the latest application version.
    # @note Updated 2020-11-22.
    # """
    local name prefix x
    koopa::assert_has_args "$#"
    name="${1:?}"
    prefix="$(koopa::app_prefix)"
    koopa::assert_is_dir "$prefix"
    prefix="${prefix}/${name}"
    koopa::assert_is_dir "$prefix"
    x="$( \
        find "$prefix" \
            -mindepth 1 \
            -maxdepth 1 \
            -type d \
        | sort \
        | tail -n 1 \
    )"
    koopa::assert_is_dir "$x"
    x="$(basename "$x")"
    koopa::print "$x"
    return 0
}

# FIXME THIS NEEDS AN LDCONFIG UPDATE CALL AT THE END.
koopa::install_app() { # {{{1
    # """
    # Install application into a versioned directory structure.
    # @note Updated 2021-04-27.
    # """
    local include_dirs jobs link_args link_app make_prefix name name_fancy \
        prefix reinstall script script_name script_prefix tmp_dir version
    koopa::assert_has_args "$#"
    koopa::assert_has_no_envs
    include_dirs=
    link_app=1
    name_fancy=
    reinstall=0
    script_name=
    script_prefix="$(koopa::prefix)/include/install"
    version=
    while (("$#"))
    do
        case "$1" in
            --include-dirs=*)
                include_dirs="${1#*=}"
                shift 1
                ;;
            --name=*)
                name="${1#*=}"
                shift 1
                ;;
            --name-fancy=*)
                name_fancy="${1#*=}"
                shift 1
                ;;
            --no-link)
                link_app=0
                shift 1
                ;;
            --reinstall|--force)
                reinstall=1
                shift 1
                ;;
            --script-name=*)
                script_name="${1#*=}"
                shift 1
                ;;
            --script-prefix=*)
                script_prefix="${1#*=}"
                shift 1
                ;;
            --version=*)
                version="${1#*=}"
                shift 1
                ;;
            --verbose)
                set -x
                shift 1
                ;;
            "")
                shift 1
                ;;
            *)
                koopa::invalid_arg "$1"
                ;;
        esac
    done
    koopa::assert_has_no_args "$#"
    [[ -z "$name_fancy" ]] && name_fancy="$name"
    [[ -z "$script_name" ]] && script_name="$name"
    [[ -z "$version" ]] && version="$(koopa::variable "$name")"
    if koopa::is_macos
    then
        koopa::assert_is_installed brew
        link_app=0
    fi
    prefix="$(koopa::app_prefix)/${name}/${version}"
    make_prefix="$(koopa::make_prefix)"
    [[ "$reinstall" -eq 1 ]] && koopa::sys_rm "$prefix"
    if [[ -d "$prefix" ]]
    then
        koopa::alert_note "${name_fancy} already installed at '${prefix}'."
        return 0
    fi
    koopa::install_start "$name_fancy" "$version" "$prefix"
    tmp_dir="$(koopa::tmp_dir)"
    (
        koopa::cd "$tmp_dir"
        # FIXME REWORK THIS, DEFINING PER SCRIPT INSTEAD.
        jobs="$(koopa::cpu_count)"
        export INSTALL_JOBS="$jobs"
        export INSTALL_LINK_APP="$link_app"
        export INSTALL_NAME="$name"
        export INSTALL_PREFIX="$prefix"
        export INSTALL_REINSTALL="$reinstall"
        export INSTALL_VERSION="$version"
        script="${script_prefix}/${script_name}.sh"
        koopa::assert_is_file "$script"
        # shellcheck source=/dev/null
        . "$script"
    ) 2>&1 | tee "$(koopa::tmp_log_file)"
    koopa::rm "$tmp_dir"
    koopa::sys_set_permissions -r "$prefix"
    # FIXME This will cause issues with r, python?
    koopa::link_into_opt "$prefix" "$name"
    if [[ "$link_app" -eq 1 ]]
    then
        koopa::delete_broken_symlinks "$make_prefix"
        link_args=(
            "--name=${name}"
            "--version=${version}"
        )
        if [[ -n "$include_dirs" ]]
        then
            link_args+=("--include-dirs=${include_dirs}")
        fi
        # We're including the 'true' catch here to avoid cp issues on Arch.
        koopa::link_app "${link_args[@]}" || true
    fi
    if koopa::is_shared_install
    then
        sudo ldconfig
    fi
    koopa::install_success "$name_fancy" "$prefix"
    return 0
}

koopa::link_app() { # {{{1
    # """
    # Symlink application into build directory.
    # @note Updated 2021-04-26.
    #
    # If you run into permissions issues during link, check the build prefix
    # permissions. Ensure group is not 'root', and that group has write access.
    #
    # This can be reset easily with 'koopa::sys_set_permissions'.
    #
    # Note that Debian symlinks 'man' to 'share/man', which is non-standard.
    # This is currently corrected in 'install-debian-base', but top-level
    # symlink checks may need to be added here in a future update.
    #
    # @section cp flags:
    # * -f, --force
    # * -R, -r, --recursive
    # * -s, --symbolic-link
    #
{'fraction_non_alphanumeric': 0.14335490293678446, 'fraction_numerical': 0.01214534594325535, 'mean_word_length': 1.719545208446129, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '927603', 'n_tokens_mistral': 3364, 'n_tokens_neox': 3075, 'n_words': 714}