|
| 1 | +#!/usr/bin/env bash |
| 2 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 3 | +# title Install Script + |
| 4 | +# project nord-terminix + |
| 5 | +# repository https://github.com/arcticicestudio/nord-terminix + |
| 6 | +# author Arctic Ice Studio + |
| 7 | + |
| 8 | +# copyright Copyright (C) 2016 + |
| 9 | +# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 10 | +set -e |
| 11 | + |
| 12 | +_ct_error="\e[0;31m" |
| 13 | +_ct_success="\e[0;32m" |
| 14 | +_ct_warning="\e[0;33m" |
| 15 | +_ct_highlight="\e[0;34m" |
| 16 | +_ct_primary="\e[0;36m" |
| 17 | +_ct="\e[0;37m" |
| 18 | +_ctb_subtle="\e[1;30m" |
| 19 | +_ctb_error="\e[1;31m" |
| 20 | +_ctb_success="\e[1;32m" |
| 21 | +_ctb_warning="\e[1;33m" |
| 22 | +_ctb_highlight="\e[1;34m" |
| 23 | +_ctb_primary="\e[1;36m" |
| 24 | +_ctb="\e[1;37m" |
| 25 | +_c_reset="\e[0m" |
| 26 | + |
| 27 | +__help() { |
| 28 | + printf "${_ctb}Usage: ${_ct_primary}install.sh ${_ctb_subtle}[OPTIONS]\n" |
| 29 | + printf " ${_ctb_highlight}-h${_ct},${_ctb_highlight} --help ${_ct}Help\n" |
| 30 | + printf " ${_ctb_highlight}-v${_ct},${_ctb_highlight} --verbose ${_ct}Verbose output\n${_ctb_reset}" |
| 31 | + printf " ${_ctb_highlight}-g${_ct},${_ctb_highlight} --global \ |
| 32 | +${_ct}Install global\n${_ctb_reset}" |
| 33 | + printf " ${_ctb_highlight}-s${_ct},${_ctb_highlight} --schemefile <SCHEME_FILE> \ |
| 34 | +${_ct}Use the specified scheme file\n${_ctb_reset}" |
| 35 | +} |
| 36 | + |
| 37 | +__cleanup() { |
| 38 | + trap '' SIGINT SIGTERM |
| 39 | + unset -v _ct_error _ct_success _ct_warning _ct_highlight _ct_primary _ct |
| 40 | + unset -v _ctb_error _ctb_success _ctb_warning _ctb_highlight _ctb_primary _ctb _c_reset |
| 41 | + unset -v NORD_TERMINIX_SCRIPT_OPTS COLOR_SCHEME_FILE VERBOSE GLOBAL_INSTALL LOCAL_INSTALL |
| 42 | + unset -f __help __cleanup __log_error __log_success __log_warning __log_info |
| 43 | + unset -f __validate_file __local_install __global_install |
| 44 | +} |
| 45 | + |
| 46 | +__log_error() { |
| 47 | + printf "${_ctb_error}[ERROR] ${_ct}$1${_c_reset}\n" |
| 48 | +} |
| 49 | + |
| 50 | +__log_success() { |
| 51 | + printf "${_ctb_success}[OK] ${_ct}$1${_c_reset}\n" |
| 52 | +} |
| 53 | + |
| 54 | +__log_warning() { |
| 55 | + printf "${_ctb_warning}[WARN] ${_ct}$1${_c_reset}\n" |
| 56 | +} |
| 57 | + |
| 58 | +__log_info() { |
| 59 | + printf "${_ctb}[INFO] ${_ct}$1${_c_reset}\n" |
| 60 | +} |
| 61 | + |
| 62 | +__summary_success() { |
| 63 | + if [ $GLOBAL_INSTALL = true ]; then |
| 64 | + __log_success "Global installation completed" |
| 65 | + else |
| 66 | + __log_success "Local installation completed" |
| 67 | + fi |
| 68 | + __cleanup |
| 69 | + exit 0 |
| 70 | +} |
| 71 | + |
| 72 | +__summary_error() { |
| 73 | + __log_error "An error occurred during the installation!" |
| 74 | + __log_error "Exit code: $1" |
| 75 | + __cleanup |
| 76 | + exit 1 |
| 77 | +} |
| 78 | + |
| 79 | +__global_install() { |
| 80 | + __validate_file |
| 81 | + if [ ! -d $GLOBAL_INSTALL_DIR ]; then |
| 82 | + sudo mkdir -p $GLOBAL_INSTALL_DIR |
| 83 | + if [ $? -eq 0 ]; then |
| 84 | + if [ $VERBOSE = true ]; then __log_info "Created global directory $GLOBAL_INSTALL_DIR"; fi |
| 85 | + else |
| 86 | + __log_error "Could not create global directory $GLOBAL_INSTALL_DIR" |
| 87 | + __summary_error 1 |
| 88 | + fi |
| 89 | + fi |
| 90 | + sudo cp -f $COLOR_SCHEME_FILE $GLOBAL_INSTALL_DIR |
| 91 | + if [ $? -eq 0 ]; then |
| 92 | + if [ $VERBOSE = true ]; then __log_success "Copied color scheme file to $GLOBAL_INSTALL_DIR"; fi |
| 93 | + __summary_success |
| 94 | + else |
| 95 | + __log_error "Could not copy color scheme file to $GLOBAL_INSTALL_DIR" |
| 96 | + __summary_error 1 |
| 97 | + fi |
| 98 | +} |
| 99 | + |
| 100 | +__local_install() { |
| 101 | + __validate_file |
| 102 | + if [ ! -d $LOCAL_INSTALL_DIR ]; then |
| 103 | + mkdir -p $LOCAL_INSTALL_DIR |
| 104 | + if [ $? -eq 0 ]; then |
| 105 | + if [ $VERBOSE = true ]; then __log_info "Created local directory $LOCAL_INSTALL_DIR"; fi |
| 106 | + else |
| 107 | + __log_error "Could not create local directory $LOCAL_INSTALL_DIR" |
| 108 | + __summary_error 1 |
| 109 | + fi |
| 110 | + fi |
| 111 | + cp -f $COLOR_SCHEME_FILE $LOCAL_INSTALL_DIR |
| 112 | + if [ $? -eq 0 ]; then |
| 113 | + if [ $VERBOSE = true ]; then __log_success "Copied color scheme file to $LOCAL_INSTALL_DIR"; fi |
| 114 | + __summary_success |
| 115 | + else |
| 116 | + __log_error "Could not copy color scheme file to $LOCAL_INSTALL_DIR" |
| 117 | + __summary_error 1 |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +__validate_file() { |
| 122 | + if [ ! -f $COLOR_SCHEME_FILE ]; then |
| 123 | + __log_error "Color scheme file not found: $COLOR_SCHEME_FILE" |
| 124 | + __summary_error 1 |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +trap "printf '${_ctb_error}User aborted.${_ctb_reset}\n' && exit 1" SIGINT SIGTERM |
| 129 | + |
| 130 | +NORD_TERMINIX_SCRIPT_OPTS=`getopt -o vghs: --long verbose,global,help,schemefile: -n 'install.sh' -- "$@"` |
| 131 | +COLOR_SCHEME_FILE=src/json/nord.json |
| 132 | +VERBOSE=false |
| 133 | +GLOBAL_INSTALL=false |
| 134 | +LOCAL_INSTALL_DIR=~/.config/terminix/schemes |
| 135 | +GLOBAL_INSTALL_DIR=/usr/share/terminix/schemes |
| 136 | + |
| 137 | +eval set -- "$NORD_TERMINIX_SCRIPT_OPTS" |
| 138 | +while true; do |
| 139 | + case "$1" in |
| 140 | + -v | --verbose ) VERBOSE=true; shift ;; |
| 141 | + -g | --global ) GLOBAL_INSTALL=true; shift ;; |
| 142 | + -h | --help ) __help; exit 0; break ;; |
| 143 | + -s | --schemefile ) |
| 144 | + COLOR_SCHEME_FILE="$2"; shift 2 ;; |
| 145 | + -- ) shift; break ;; |
| 146 | + * ) break ;; |
| 147 | + esac |
| 148 | +done |
| 149 | + |
| 150 | +if [ $GLOBAL_INSTALL = true ]; then |
| 151 | + __global_install |
| 152 | +else |
| 153 | + __local_install |
| 154 | +fi |
| 155 | + |
| 156 | +__cleanup |
| 157 | +exit 0 |
0 commit comments