diff options
Diffstat (limited to 'install.sh')
-rw-r--r-- | install.sh | 321 |
1 files changed, 163 insertions, 158 deletions
@@ -27,53 +27,87 @@ function msg_err() { esac } - # Check Requirements # ------------------- -programs=("wget" "curl" "git" "less") +function install_package() { + local pkg="$1" + local sudo_cmd="" + local distro="" + local os_data + + case "$(id -u)" in + 0) sudo_cmd="" ;; + *) + case "$(command -v doas 2>/dev/null)" in + "") sudo_cmd="sudo" ;; + *) sudo_cmd="doas" ;; + esac + ;; + esac + + os_data="$(cat /etc/*release 2>/dev/null | tr '[:upper:]' '[:lower:]')" + + case "$os_data" in + *id=artix*|*id=arch*|*id=hyperbola*) distro="arch" ;; + *id_like=arch*) distro="arch" ;; + *id=debian*|*id=ubuntu*|*id_like=debian*) distro="debian" ;; + *) + msg_err "Sistema operativo no soportado automáticamente." \ + "Operating system not automatically supported." + exit 1 + ;; + esac + + case "$distro" in + arch) + $sudo_cmd pacman -Sy --noconfirm "$pkg" + ;; + debian) + $sudo_cmd apt-get update + $sudo_cmd apt-get install -y "$pkg" + ;; + esac +} + +programs=("curl" "less" "ls" "netstat" "unzip") for program in "${programs[@]}"; do if ! command -v "$program" &>/dev/null; then - msg_err "$program no está instalado." \ - "$program is not installed" && exit 1 + case "$program" in + netstat) pkg="net-tools" ;; + ls) pkg="coreutils" ;; + *) pkg="$program" ;; + esac + msg "Instalando dependencia: $pkg" "Installing dependency: $pkg" + install_package "$pkg" fi done -# Check URL's -# ----------- -function _which() { - command -v "$1" &> /dev/null -} - +# Check URLs availability +# ----------------------- function _url_exists() { - if _which wget; then - if wget --spider "$1" 2>/dev/null; then - return 0 # URL 'ok' - else - return 1 # URL 'fail' - fi - elif _which curl; then - if curl --output /dev/null --silent --head --fail "$1"; then - return 0 # URL 'ok' - else - return 1 # URL 'fail' - fi - fi + curl --output /dev/null --silent --head --write-out "%{http_code}" "$1" } function _urls() { - URL_1="https://git.fridu.us/heckyel/hyperterm" - URL_2="https://c.fridu.us/heckyel/hyperterm" - - if [[ $(_url_exists "$URL_1") -eq 0 ]]; then - URL="$URL_1" - elif [[ $(_url_exists "$URL_2") -eq 0 ]]; then - URL="$URL_2" - fi + URL_1="https://git.fridu.us/heckyel/hyperterm/archive/master.zip" + URL_2="https://c.fridu.us/software/hyperterm.git/snapshot/hyperterm-master.zip" + case "$(_url_exists "$URL_1")" in + 200) URL="$URL_1" ;; + *) + case "$(_url_exists "$URL_2")" in + 200) URL="$URL_2" ;; + *) + msg_err "No se pudo acceder a las URLs de HyperTerm." \ + "Could not access HyperTerm URLs." + exit 1 + ;; + esac + ;; + esac } -# ----------- -# Show how to use this installer -# ------------------------------ +# Show usage +# ----------- function show_usage() { msg "\n$0: Instalar HyperTerm" \ "\n$0: Install HyperTerm" @@ -83,178 +117,148 @@ function show_usage() { "Arguments:" msg "--help (-h): Muestra mensaje de ayuda" \ "--help (-h): Display this help message" - msg "--silent (-s): Instala la configuración predeterminada sin solicitar entrada" \ - "--silent (-s): Install default settings without prompting for input" - msg "--no-modify-config (-n): No modifica el archivo de configuración existente" \ - "--no-modify-config (-n): Do not modify existing config file" - exit 0; + msg "--silent (-s): Instala sin pedir interacción" \ + "--silent (-s): Install silently" + msg "--no-modify-config (-n): No modifica archivo config" \ + "--no-modify-config (-n): Do not modify config file" + exit 0 } -# Clone -#------ -function clone_new() { - _urls "$@" - - # clone - msg "\e[1;32m==>\e[0m\033[1m Clonando hyperterm... \e[m" \ - "\e[1;32m==>\e[0m\033[1m Cloning hyperterm... \e[m" - git clone "$URL" "/tmp/hyperterm/" --depth=1 - - # copy - msg "\e[1;32m==>\e[0m\033[1m Copiando hyperterm... \e[m" \ - "\e[1;32m==>\e[0m\033[1m Copying hyperterm... \e[m" - if [[ "$silent" ]]; then - install -d -m755 "$HOME/.hyperterm/" - cp -r /tmp/hyperterm/hyperterm/* "$HOME/.hyperterm/" - install -m644 /tmp/hyperterm/.bash_profile "$HOME/" - install -d -m755 "$HOME/.hyperterm/template/" - install -m644 /tmp/hyperterm/template/bash_profile.template.bash "$HOME/.hyperterm/template/" - else - install -d -m755 -v "$HOME/.hyperterm/" - cp -rv /tmp/hyperterm/hyperterm/* "$HOME/.hyperterm/" - install -m644 -v /tmp/hyperterm/.bash_profile "$HOME/" - install -d -m755 -v "$HOME/.hyperterm/template/" - install -m644 -v /tmp/hyperterm/template/bash_profile.template.bash "$HOME/.hyperterm/template/" +# Download and unzip archive +# -------------------------- +function download_and_unzip() { + _urls + + msg "\e[1;32m==>\e[0m\033[1m Descargando HyperTerm... \e[m" \ + "\e[1;32m==>\e[0m\033[1m Downloading HyperTerm... \e[m" + + TMP_DIR=$(mktemp -d /tmp/hyperterm.XXXXXX) + ZIP_FILE="$TMP_DIR/hyperterm.zip" + + curl -L -o "$ZIP_FILE" "$URL" + + msg "\e[1;32m==>\e[0m\033[1m Descomprimiendo HyperTerm... \e[m" \ + "\e[1;32m==>\e[0m\033[1m Unzipping HyperTerm... \e[m" + + unzip -q "$ZIP_FILE" -d "$TMP_DIR" + # The unzip folder will be something like hyperterm-master or hyperterm-master.zip contents + + # Move extracted files to ~/.hyperterm + mkdir -p "$HOME/.hyperterm" + # Find extracted dir + EXTRACTED_DIR=$(find "$TMP_DIR" -mindepth 1 -maxdepth 1 -type d | head -n 1) + + cp -r "$EXTRACTED_DIR/hyperterm"/* "$HOME/.hyperterm/" + + # Copy .bash_profile and template files if exist + if [[ -f "$EXTRACTED_DIR/.bash_profile" ]]; then + cp "$EXTRACTED_DIR/.bash_profile" "$HOME/" fi -} -function clean_temp() { - # clean up temp files - msg "\e[1;32m==>\e[0m\033[1m Limpiando archivos temporales... \e[m" \ - "\e[1;32m==>\e[0m\033[1m Clean up temp files... \e[m" - if [[ "$silent" ]]; then - rm -rf /tmp/hyperterm/ - if [[ -f "$HOME/.hyperterm/template/bash_profile.template.bash" ]]; then - rm -fr "$HOME/.hyperterm/template/" - fi - else - rm -rfv /tmp/hyperterm/ - if [[ -f "$HOME/.hyperterm/template/bash_profile.template.bash" ]]; then - rm -frv "$HOME/.hyperterm/template/" - fi + mkdir -p "$HOME/.hyperterm/template" + if [[ -f "$EXTRACTED_DIR/template/bash_profile.template.bash" ]]; then + cp "$EXTRACTED_DIR/template/bash_profile.template.bash" "$HOME/.hyperterm/template/" fi + + # Clean temp + rm -rf "$TMP_DIR" } -# Back up existing profile and create new one for hyperterm -# --------------------------------------------------------- -function backup_new() { - clone_new "$@" - test -w "$HOME/$CONFIG_FILE" && - cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && +# Backup and install +# ------------------ +function backup_and_install() { + download_and_unzip + + test -w "$HOME/$CONFIG_FILE" && cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && msg "\033[0;36mTu archivo original $CONFIG_FILE ha sido respaldado a $CONFIG_FILE.bak \033[0m" \ "\033[0;36mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak \033[0m" - sed "s|{{HYPER_BASH}}|$HYPER_BASH|" "$HYPER_BASH/.hyperterm/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" - msg "\033[0;36mPlantilla copiada de $CONFIG_FILE dentro de ~/$CONFIG_FILE \033[0m" \ - "\033[0;36mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE \033[0m" - clean_temp "$@" + sed "s|{{HYPER_BASH}}|$HYPER_BASH|" "$HOME/.hyperterm/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" + + msg "\033[0;36mPlantilla copiada a ~/$CONFIG_FILE \033[0m" \ + "\033[0;36mTemplate copied to ~/$CONFIG_FILE \033[0m" } +# Parse args +# ---------- for param in "$@"; do shift case "$param" in "--help") set -- "$@" "-h" ;; "--silent") set -- "$@" "-s" ;; "--no-modify-config") set -- "$@" "-n" ;; - *) set -- "$@" "$param" + *) set -- "$@" "$param" ;; esac done OPTIND=1 -while getopts "hsn" opt -do +while getopts "hsn" opt; do case "$opt" in - "h") show_usage;; - "s") silent=true ;; - "n") no_modify_config=true ;; - "?") show_usage >&2;; + h) show_usage ;; + s) silent=true ;; + n) no_modify_config=true ;; + ?) show_usage >&2 ;; esac done shift $((OPTIND - 1)) +# Setup config file based on OS +case "$OSTYPE" in + darwin*) CONFIG_FILE=".bash_profile" ;; + *) CONFIG_FILE=".bashrc" ;; +esac + HYPER_BASH="$(cd "$(dirname "$0")" && pwd)" -case $OSTYPE in - darwin*) - CONFIG_FILE=.bash_profile - ;; - *) - CONFIG_FILE=.bashrc - ;; -esac +msg "Instalando HyperTerm" "Installing HyperTerm" -BACKUP_FILE=$CONFIG_FILE.bak -msg "Instalando HyperTerm" \ - "Installing HyperTerm" if ! [[ "$silent" ]] && ! [[ "$no_modify_config" ]]; then - if [ -e "$HOME/$BACKUP_FILE" ]; then - msg_err "\033[0;36mEl archivo de respaldo ya existe. Asegúrese de hacer una copia de seguridad de su .bashrc antes de ejecutar esta instalación. \033[0m" \ - "\033[0;36mBackup file already exists. Make sure to backup your .bashrc before running this installation. \033[0m" - while ! [ "$silent" ]; do - question=$(msg "¿Desea sobrescribir la copia de seguridad existente? Esto eliminará su archivo de copia de seguridad existente ($HOME/$BACKUP_FILE) [s/N] " \ - "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] ") - - read -e -n 1 -r -p "$question" RESP - case $RESP in - [yY]|[sS]) - break - ;; - [nN]|"") - msg "\033[91mInstalación interrumpida. Por favor vuelve pronto!\033[m" \ - "\033[91mInstallation aborted. Please come back soon!\033[m" - exit 1 - ;; - *) - msg "\033[91mPor favor elija sí o no.\033[m" \ - "\033[91mPlease choose y or n.\033[m" - ;; + if [[ -e "$HOME/$CONFIG_FILE.bak" ]]; then + msg_err "\033[0;36mArchivo de respaldo ya existe. Haz backup antes de instalar.\033[0m" \ + "\033[0;36mBackup file already exists. Please backup before installing.\033[0m" + while true; do + read -e -n 1 -r -p "$(msg "¿Sobrescribir backup? [s/N] " "Overwrite backup? [y/N] ") " RESP + case "$RESP" in + [yYsS]) break ;; + [nN]|"") msg "Instalación abortada." "Installation aborted."; exit 1 ;; + *) msg "Elige sí o no." "Choose y or n." ;; esac done fi - while ! [ "$silent" ]; do - question=$(msg "¿Le gustaría conservar su configuración de $CONFIG_FILE y agregar plantillas de HyperTerm al final? [s/N] " \ - "Would you like to keep your config $CONFIG_FILE and append HyperTerm templates at the end? [y/N] ") - read -e -n 1 -r -p "$question" choice - case $choice in - [yY]|[sS]) - clone_new "$@" - test -w "$HOME/$CONFIG_FILE" && - cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && - msg "\033[0;36mTu archivo original $CONFIG_FILE ha sido respaldado a $CONFIG_FILE.bak \033[0m" \ - "\033[0;36mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak \033[0m" - - (sed "s|{{HYPER_BASH}}|$HYPER_BASH|" "$HYPER_BASH/.hyperterm/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" - msg "\033[0;36mla plantilla HyperTerm ha sido agregada a $CONFIG_FILE\033[0m" \ - "\033[0;36mHyperTerm template has been added to your $CONFIG_FILE\033[0m" - clean_temp "$@" + while true; do + read -e -n 1 -r -p "$(msg "¿Conservar $CONFIG_FILE y añadir plantilla HyperTerm al final? [s/N] " "Keep $CONFIG_FILE and append HyperTerm template? [y/N] ") " choice + case "$choice" in + [yYsS]) + download_and_unzip "$@" + test -w "$HOME/$CONFIG_FILE" && cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && + msg "\033[0;36mRespaldo creado en $CONFIG_FILE.bak \033[0m" "\033[0;36mBackup created at $CONFIG_FILE.bak \033[0m" + (sed "s|{{HYPER_BASH}}|$HYPER_BASH|" "$HOME/.hyperterm/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" + msg "\033[0;36mPlantilla HyperTerm añadida a $CONFIG_FILE\033[0m" "\033[0;36mHyperTerm template added to $CONFIG_FILE\033[0m" break ;; [nN]|"") - backup_new "$@" + backup_and_install "$@" break ;; *) - msg "\033[91mPor favor elija sí o no.\033[m" \ - "\033[91mPlease choose y or n.\033[m" + msg "Elige sí o no." "Choose y or n." ;; esac done elif [[ "$silent" ]] && ! [[ "$no_modify_config" ]]; then - # backup/new by default - backup_new "$@" + backup_and_install "$@" fi echo "" -msg "\e[1;32m==>\e[0m\033[1m Instalación finalizada con éxito! Disfrute HyperTerm! \e[m" \ +msg "\e[1;32m==>\e[0m\033[1m Instalación finalizada con éxito! Disfruta HyperTerm! \e[m" \ "\e[1;32m==>\e[0m\033[1m Installation finished successfully! Enjoy HyperTerm! \e[m" -msg "\033[0;36mPara comenzar a usarlo, abra una nueva pestaña o haga 'source $HOME/$CONFIG_FILE'.\033[0m" \ - "\033[0;36mTo start using it, open a new tab or 'source $HOME/$CONFIG_FILE'.\033[0m" +msg "\033[0;36mPara empezar, abre una nueva pestaña o haz 'source $HOME/$CONFIG_FILE'.\033[0m" \ + "\033[0;36mTo start, open a new tab or 'source $HOME/$CONFIG_FILE'.\033[0m" echo "" -msg "¡Muchas gracias! por instalar" \ - "Thank you! for install" +msg "¡Gracias por instalar!" "Thank you for installing!" echo -e '\033[0;36m __ __ ______ ' echo -e '\033[0;36m / / / /_ ______ ___ ____/_ __/__ _________ ___ ' echo -e '\033[0;36m / /_/ / / / / __ \/ _ \/ ___// / / _ \/ ___/ __ `__ \ ' @@ -262,7 +266,8 @@ echo -e '\033[0;36m / __ / /_/ / /_/ / __/ / / / / __/ / / / / / / / ' echo -e '\033[0;36m /_/ /_/\__, / .___/\___/_/ /_/ \___/_/ /_/ /_/ /_/ ' echo -e '\033[0;36m /____/_/ ' echo -e '\033[m' -msg "Para evitar problemas y mantener su shell, habilite solo las funciones que realmente desea utilizar desde $HOME/.hyperterm/_custom.sh" \ - "To avoid issues and to keep your shell lean, please enable only features you really want to use from $HOME/.hyperterm/_custom.sh" -msg "Puede reportarnos errores en \033[0;36mhttps://todo.sr.ht/~heckyel/hyperterm \033[0m" \ - "You can report errors issues in \033[0;36mhttps://todo.sr.ht/~heckyel/hyperterm \033[0m" + +msg "Para evitar problemas, activa solo las funciones que uses en $HOME/.hyperterm/_custom.sh" \ + "To avoid issues, enable only the features you want from $HOME/.hyperterm/_custom.sh" +msg "Puedes reportar errores en \033[0;36mhttps://todo.sr.ht/~heckyel/hyperterm \033[0m" \ + "You can report issues at \033[0;36mhttps://todo.sr.ht/~heckyel/hyperterm \033[0m" |