#!/bin/bash

# Generate a random string
#
# usage: ynh_string_random [--length=string_length]
# | arg: -l, --length=  - the string length to generate (default: 24)
# | ret: the generated string
#
# example: pwd=$(ynh_string_random --length=8)
#
# Requires YunoHost version 2.2.4 or higher.
ynh_string_random() {
    # Declare an array to define the options of this helper.
    local legacy_args=l
    local -A args_array=( [l]=length= )
    local length
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"
    length=${length:-24}

    dd if=/dev/urandom bs=1 count=1000 2> /dev/null \
        | tr --complement --delete 'A-Za-z0-9' \
        | sed --quiet 's/\(.\{'"$length"'\}\).*/\1/p'
}

# Substitute/replace a string (or expression) by another in a file
#
# usage: ynh_replace_string --match_string=match_string --replace_string=replace_string --target_file=target_file
# | arg: -m, --match_string=    - String to be searched and replaced in the file
# | arg: -r, --replace_string=  - String that will replace matches
# | arg: -f, --target_file=     - File in which the string will be replaced.
#
# As this helper is based on sed command, regular expressions and references to
# sub-expressions can be used (see sed manual page for more information)
#
# Requires YunoHost version 2.6.4 or higher.
ynh_replace_string () {
    # Declare an array to define the options of this helper.
    local legacy_args=mrf
    local -A args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
    local match_string
    local replace_string
    local target_file
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    local delimit=@
    # Escape the delimiter if it's in the string.
    match_string=${match_string//${delimit}/"\\${delimit}"}
    replace_string=${replace_string//${delimit}/"\\${delimit}"}

    sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$target_file"
}

# Substitute/replace a special string by another in a file
#
# usage: ynh_replace_special_string --match_string=match_string --replace_string=replace_string --target_file=target_file
# | arg: -m, --match_string=    - String to be searched and replaced in the file
# | arg: -r, --replace_string=  - String that will replace matches
# | arg: -t, --target_file=     - File in which the string will be replaced.
#
# This helper will use ynh_replace_string, but as you can use special
# characters, you can't use some regular expressions and sub-expressions.
#
# Requires YunoHost version 2.7.7 or higher.
ynh_replace_special_string () {
    # Declare an array to define the options of this helper.
    local legacy_args=mrf
    local -A args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
    local match_string
    local replace_string
    local target_file
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    # Escape any backslash to preserve them as simple backslash.
    match_string=${match_string//\\/"\\\\"}
    replace_string=${replace_string//\\/"\\\\"}

    # Escape the & character, who has a special function in sed.
    match_string=${match_string//&/"\&"}
    replace_string=${replace_string//&/"\&"}

    ynh_replace_string --match_string="$match_string" --replace_string="$replace_string" --target_file="$target_file"
}

# Sanitize a string intended to be the name of a database
#
# usage: ynh_sanitize_dbid --db_name=name
# | arg: -n, --db_name=     - name to correct/sanitize
# | ret: the corrected name
#
# example: dbname=$(ynh_sanitize_dbid $app)
#
# Underscorify the string (replace - and . by _)
#
# Requires YunoHost version 2.2.4 or higher.
ynh_sanitize_dbid () {
    # Declare an array to define the options of this helper.
    local legacy_args=n
    local -A args_array=( [n]=db_name= )
    local db_name
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    # We should avoid having - and . in the name of databases. They are replaced by _
    echo ${db_name//[-.]/_}
}

# Normalize the url path syntax
#
# [internal]
#
# Handle the slash at the beginning of path and its absence at ending
# Return a normalized url path
#
# examples:
#     url_path=$(ynh_normalize_url_path $url_path)
#     ynh_normalize_url_path example    # -> /example
#     ynh_normalize_url_path /example   # -> /example
#     ynh_normalize_url_path /example/  # -> /example
#     ynh_normalize_url_path /          # -> /
#
# usage: ynh_normalize_url_path --path_url=path_to_normalize
# | arg: -p, --path_url=    - URL path to normalize before using it
#
# Requires YunoHost version 2.6.4 or higher.
ynh_normalize_url_path () {
    # Declare an array to define the options of this helper.
    local legacy_args=p
    local -A args_array=( [p]=path_url= )
    local path_url
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    test -n "$path_url" || ynh_die --message="ynh_normalize_url_path expect a URL path as first argument and received nothing."
    if [ "${path_url:0:1}" != "/" ]; then    # If the first character is not a /
        path_url="/$path_url"    # Add / at begin of path variable
    fi
    if [ "${path_url:${#path_url}-1}" == "/" ] && [ ${#path_url} -gt 1 ]; then    # If the last character is a / and that not the only character.
        path_url="${path_url:0:${#path_url}-1}"	# Delete the last character
    fi
    echo $path_url
}