aboutsummaryrefslogtreecommitdiffstats
path: root/hyperterm/core/update.sh
blob: ade74e3b49791b682c6d388127a73cd2b1605554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
# shellcheck source=/dev/null
#----------------------------
# Check if a command exists
#----------------------------
function _which() {
    command -v "$1" &> /dev/null
}

#----------------------------
# Check if a URL is reachable
#----------------------------
function _url_exists() {
    curl --output /dev/null --silent --head --write-out "%{http_code}" "$1"
}

#------------------
# Set working URLs
#------------------
function _urls() {
    URL_1="https://git.fridu.us/heckyel/hyperterm"
    URL_2="https://c.fridu.us/software/hyperterm.git"

    if [ "$(_url_exists "$URL_1")" -eq 200 ]; then
        URL="$URL_1"
        RAW="$URL_1/raw/branch/master"
    elif [ "$(_url_exists "$URL_2")" -eq 200 ]; then
        URL="$URL_2"
        RAW="$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
    fi
}

#----------------------------
# Download file with curl
#----------------------------
function download_file() {
    local remote_path=$1
    local local_path=$2
    mkdir -p "$(dirname "$local_path")"
    curl -Ls "$RAW/$remote_path" -o "$local_path"
}

#----------------------------
# Download _custom.sh if missing
#----------------------------
function ifexists_custom() {
    _urls
    local custom_path="$HOME/.hyperterm/_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
}

#----------------------------
# Update HyperTerm environment
#----------------------------
function updbashrc() {
    _urls

    # Step 1: Download checksum and _custom.sh
    download_file "hyperterm/hyperterm.sha512" "$HOME/.hyperterm/hyperterm.sha512"
    ifexists_custom

    # Step 2: Verify checksum
    (cd "$HOME/.hyperterm/" && sha512sum -c hyperterm.sha512 &> /dev/null)
    local _interger=$?

    if [[ "$_interger" -eq 0 ]]; then
        # Load colors and print banner
        _colors_bash "$@"
        printf '%b' "${BOLD}${CYAN}"
        printf '%s\n' '     __  __                     ______                   '
        printf '%s\n' '    / / / /_  ______  ___  ____/_  __/__  _________ ___  '
        printf '%s\n' '   / /_/ / / / / __ \/ _ \/ ___// / / _ \/ ___/ __ `__ \ '
        printf '%s\n' '  / __  / /_/ / /_/ /  __/ /   / / /  __/ /  / / / / / / '
        printf '%s\n' ' /_/ /_/\__, / .___/\___/_/   /_/  \___/_/  /_/ /_/ /_/  '
        printf '%s\n' '       /____/_/                                          '
        printf '%s\n' '                                                         '
        printf '%b' "${BOLD}${GREY}"
        msg "✔️ HyperTerm se ha actualizado y/o está en la versión actual." \
            "✔️ HyperTerm has been updated and/or is at the current version."
        msg "Puede reportar errores en https://todo.sr.ht/~heckyel/hyperterm" \
            "You can report issues at https://todo.sr.ht/~heckyel/hyperterm"
        msg "Obtén tu copia en: https://c.fridu.us/software/hyperterm.git" \
            "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

        # Step 3: Core files
        for f in autocomplete.sh colors.sh git.sh languages.sh status.sh update.sh; do
            download_file "hyperterm/core/$f" "$HOME/.hyperterm/core/$f"
        done

        # Step 4: Themes
        for f in default.sh joy.sh light_theme.sh minterm.sh pure.sh simple.sh special.sh; do
            download_file "hyperterm/themes/$f" "$HOME/.hyperterm/themes/$f"
        done

        show_progress 2
        sleep 1

        # Step 5: Tools
        for f in aliases.sh compress.sh export.sh listuser.sh network.sh proxy.sh rar2zip.sh ruby.sh ssh-agent.sh sysinfo.sh vconverter.sh virtualenv.sh; do
            download_file "hyperterm/tools/$f" "$HOME/.hyperterm/tools/$f"
        done

        # Step 6: Main files
        download_file "hyperterm/hyperterm.sh" "$HOME/.hyperterm/hyperterm.sh"
        download_file "hyperterm/hyperterm.sha512" "$HOME/.hyperterm/hyperterm.sha512"
        download_file ".bash_profile" "$HOME/.bash_profile"

        ifexists_custom
        show_progress 3
        _colors_bash "$@"
        source "$HOME/.bashrc"
    fi
}

#------------------------------------
# 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
}