diff options
Diffstat (limited to 'hyperterm/core/update.sh')
| -rw-r--r-- | hyperterm/core/update.sh | 178 |
1 files changed, 114 insertions, 64 deletions
diff --git a/hyperterm/core/update.sh b/hyperterm/core/update.sh index a914901..650c2f9 100644 --- a/hyperterm/core/update.sh +++ b/hyperterm/core/update.sh @@ -1,5 +1,13 @@ #!/bin/bash # shellcheck source=/dev/null + +# Constants +HYPERTERM_DIR="${HYPERTERM_DIR:-$HOME/.hyperterm}" +URL_1="${URL_1:-https://git.fridu.us/heckyel/hyperterm}" +URL_2="${URL_2:-https://c.fridu.us/software/hyperterm.git}" +REPO_ERROR_MSG_ES="${REPO_ERROR_MSG_ES:-El repositorio no está disponible o no hay conexión a Internet.}" +REPO_ERROR_MSG_EN="${REPO_ERROR_MSG_EN:-The repository is unavailable or there\'s no internet connection.}" + #---------------------------- # Check if a command exists #---------------------------- @@ -15,22 +23,16 @@ function _url_exists() { } #------------------ -# Set working URLs +# Get working URLs #------------------ -function _urls() { - URL_1="https://git.fridu.us/heckyel/hyperterm" - URL_2="https://c.fridu.us/software/hyperterm.git" - +function _get_repo_urls() { if [ "$(_url_exists "$URL_1")" -eq 200 ]; then - URL="$URL_1" - RAW="$URL_1/raw/branch/master" + echo "$URL_1|$URL_1/raw/branch/master" elif [ "$(_url_exists "$URL_2")" -eq 200 ]; then - URL="$URL_2" - RAW="$URL_2/plain" + echo "$URL_2|$URL_2/plain" else - msg_err "El repositorio no está disponible o no hay conexión a Internet." \ - "The repository is unavailable or there's no internet connection." - exit 1 + msg_err "$REPO_ERROR_MSG_ES" "$REPO_ERROR_MSG_EN" + return 1 fi } @@ -40,44 +42,71 @@ function _urls() { function download_file() { local remote_path=$1 local local_path=$2 - mkdir -p "$(dirname "$local_path")" - curl -Ls "$RAW/$remote_path" -o "$local_path" + local repo_info + local raw_url + + repo_info=$(_get_repo_urls) || return 1 + raw_url="${repo_info#*|}" + + install -d "$(dirname "$local_path")" + curl -Ls "$raw_url/$remote_path" -o "$local_path" } -#---------------------------- +#-------------------------------- # Download _custom.sh if missing -#---------------------------- +#-------------------------------- function ifexists_custom() { - _urls - local custom_path="$HOME/.hyperterm/_custom.sh" + local custom_path="$HYPERTERM_DIR/_custom.sh" if [ ! -e "$custom_path" ]; then download_file "hyperterm/_custom.sh" "$custom_path" fi } -#---------------------------- +#------------------- # Show progress bar -#---------------------------- +#------------------- function show_progress() { - case $1 in - 1) printf '%s\r' "##### (33%)" ;; - 2) printf '%s\r' "############# (66%)" ;; - 3) printf '%s\n' "####################### (100%) done!" ;; - esac + local current=$1 + local total=$2 + local message=${3:-"Procesando"} + + if [[ $total -eq 0 ]]; then + printf '\r%s... ' "$message" + return + fi + + local percentage=$((current * 100 / total)) + local filled=$((percentage / 5)) # 20 chars max + local empty=$((20 - filled)) + + # Truncate long filenames for better display + local display_message="$message" + if [[ ${#message} -gt 30 ]]; then + display_message="...${message: -27}" + fi + + # Clear the line first, then show progress + printf '\r\033[K[%*s%*s] %d%% (%d/%d) %s' \ + "$filled" "$(printf '#%.0s' $(seq 1 $filled))" \ + "$empty" "" \ + "$percentage" "$current" "$total" "$display_message" + + [[ $current -eq $total ]] && printf '\n' } -#---------------------------- +#------------------------------ # Update HyperTerm environment -#---------------------------- +#------------------------------ function updbashrc() { - _urls + local repo_info + repo_info=$(_get_repo_urls) || return 1 # Step 1: Download checksum and _custom.sh - download_file "hyperterm/hyperterm.sha512" "$HOME/.hyperterm/hyperterm.sha512" + download_file "hyperterm/hyperterm.sha512" "$HYPERTERM_DIR/hyperterm.sha512" ifexists_custom # Step 2: Verify checksum - (cd "$HOME/.hyperterm/" && sha512sum -c hyperterm.sha512 &> /dev/null) + (cd "$HYPERTERM_DIR" && sha512sum -c hyperterm.sha512 &> /dev/null) local _integer=$? if [[ "$_integer" -eq 0 ]]; then @@ -99,21 +128,47 @@ function updbashrc() { "Get your copy at: https://c.fridu.us/software/hyperterm.git" printf '%b\n' "$RESET" else - msg "Fallo de checksum. Re-descargando archivos desde: $URL" \ - "Checksum failed. Re-downloading files from: $URL" - show_progress 1 - sleep 1 + msg "Verificando archivos modificados..." \ + "Checking for modified files..." + + # Step 3: Check each file individually and download only if needed + local files_to_update=() + local current_file=0 - # Step 3: Download all files from hyperterm.sha512 like list + # First pass: identify files that need updating while IFS= read -r line; do - file=$(echo "$line" | cut -d' ' -f2) - file=${file#./} - download_file "hyperterm/$file" "$HOME/.hyperterm/$file" - done < "$HOME/.hyperterm/hyperterm.sha512" - show_progress 2 - sleep 1 - - show_progress 3 + local expected_hash file_path + expected_hash=$(echo "$line" | cut -d' ' -f1) + file_path=$(echo "$line" | cut -d' ' -f2) + file_path=${file_path#./} + local full_path="$HYPERTERM_DIR/$file_path" + + # Check if file exists and has correct hash + if [[ ! -f "$full_path" ]] || ! echo "$expected_hash ./$file_path" | (cd "$HYPERTERM_DIR" && sha512sum -c --quiet >/dev/null 2>&1); then + files_to_update+=("$file_path") + fi + done < "$HYPERTERM_DIR/hyperterm.sha512" + + local total_updates=${#files_to_update[@]} + + if [[ $total_updates -eq 0 ]]; then + msg "✔️ Todos los archivos están actualizados" \ + "✔️ All files are up to date" + else + msg "Descargando $total_updates archivo(s) modificado(s)..." \ + "Downloading $total_updates modified file(s)..." + + # Second pass: download only the files that need updating + for file_path in "${files_to_update[@]}"; do + ((current_file++)) + if [[ "${LANG:-}" =~ ^es ]]; then + show_progress "$current_file" "$total_updates" "Actualizando $file_path" + else + show_progress "$current_file" "$total_updates" "Updating $file_path" + fi + download_file "hyperterm/$file_path" "$HYPERTERM_DIR/$file_path" + done + fi _colors_bash "$@" source "$HOME/.bashrc" fi @@ -123,25 +178,20 @@ function updbashrc() { # Overwrite _custom.sh interactively #------------------------------------ function updbashrc_custom() { - _urls - if [ "$(_url_exists "$URL")" -eq 200 ]; then - while true; do - question=$(msg "¿Estás seguro de sobre-escribir _custom.sh? [s/N]: " \ - "Are you sure to overwrite _custom.sh? [y/N]: ") - read -r -p "$question" input - case "$input" in - [yY]|[sS]) - download_file "hyperterm/_custom.sh" "$HOME/.hyperterm/_custom.sh" - source "$HOME/.bashrc" - break ;; - [nN]|"") break ;; - *) msg "Por favor responde sí o no." \ - "Please answer yes or no." ;; - esac - done - else - msg_err "El repositorio no está disponible o no hay conexión a Internet." \ - "The repository is unavailable or there's no internet connection." - return 1 - fi + _get_repo_urls > /dev/null || return 1 + + while true; do + question=$(msg "¿Estás seguro de sobre-escribir _custom.sh? [s/N]: " \ + "Are you sure to overwrite _custom.sh? [y/N]: ") + read -r -p "$question" input + case "$input" in + [yY]|[sS]) + download_file "hyperterm/_custom.sh" "$HYPERTERM_DIR/_custom.sh" + source "$HOME/.bashrc" + break ;; + [nN]|"") break ;; + *) msg "Por favor responde sí o no." \ + "Please answer yes or no." ;; + esac + done } |
