summaryrefslogtreecommitdiff
path: root/install.sh
blob: 3a9cdd993fd470b2ca6e96ce072d38980cf720be (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
#!/usr/bin/env bash
# Module: install
# Description: Instalador de ShFlow en modo local o global
# License: GPLv3
# Author: Luis GuLo
# Version: 1.3.0

set -e

# 🌐 Detectar idioma del sistema
LANGUAGE="es"
[[ "${LANG,,}" != *es* ]] && LANGUAGE="en"

# 🗣️ Mensajes traducidos
declare -A tr

if [[ "$LANGUAGE" == "es" ]]; then
  tr[logo]="🐙 SHFLOW"
  tr[mode]="🔧 Instalando ShFlow en modo: %s"
  tr[folder]="📁 Carpeta de instalación: %s"
  tr[prev_detected]="⚠️ Instalación previa detectada en %s"
  tr[preserve_vault]="📦 Preservando vault existente..."
  tr[preserve_inventory]="📦 Preservando inventory existente..."
  tr[preserve_modules]="📦 Preservando user_modules existente..."
  tr[removing_old]="🧹 Eliminando instalación previa..."
  tr[copying]="📦 Copiando archivos..."
  tr[restore_vault]="🔁 Restaurando vault..."
  tr[restore_inventory]="🔁 Restaurando inventory..."
  tr[restore_modules]="🔁 Restaurando user_modules..."
  tr[env_added]="✅ Variables añadidas a %s"
  tr[env_exists]="ℹ️ SHFLOW_HOME ya está definido en %s"
  tr[done]="🎉 Instalación completada correctamente."
  tr[installed]="📦 Proyecto instalado en: %s"
  tr[symlinks]="🔗 Symlinks creados en: %s"
  tr[restart]="🧠 Recuerda reiniciar tu terminal o ejecutar: source %s"
  tr[run]="👉 Puedes ejecutar 'shflow' desde cualquier ruta del terminal."
else
  tr[logo]="🐙 SHFLOW"
  tr[mode]="🔧 Installing ShFlow in mode: %s"
  tr[folder]="📁 Installation folder: %s"
  tr[prev_detected]="⚠️ Previous installation detected at %s"
  tr[preserve_vault]="📦 Preserving existing vault..."
  tr[preserve_inventory]="📦 Preserving existing inventory..."
  tr[preserve_modules]="📦 Preserving existing user_modules..."
  tr[removing_old]="🧹 Removing previous installation..."
  tr[copying]="📦 Copying files..."
  tr[restore_vault]="🔁 Restoring vault..."
  tr[restore_inventory]="🔁 Restoring inventory..."
  tr[restore_modules]="🔁 Restoring user_modules..."
  tr[env_added]="✅ Variables added to %s"
  tr[env_exists]="ℹ️ SHFLOW_HOME already defined in %s"
  tr[done]="🎉 Installation completed successfully."
  tr[installed]="📦 Project installed at: %s"
  tr[symlinks]="🔗 Symlinks created at: %s"
  tr[restart]="🧠 Remember to restart your terminal or run: source %s"
  tr[run]="👉 You can run 'shflow' from any terminal path."
fi

# 🖼️ Logo
[[ -f "shflow-logo.ascii" ]] && cat shflow-logo.ascii || echo "${tr[logo]}"

# 🧭 Detectar modo de instalación
if [[ "$EUID" -eq 0 ]]; then
  INSTALL_DIR="/opt/shflow"
  BIN_DIR="/usr/local/bin"
  MODE="global"
else
  INSTALL_DIR="$HOME/shflow"
  BIN_DIR="$HOME/.local/bin"
  MODE="local"
fi

printf "${tr[mode]}\n" "$MODE"
printf "${tr[folder]}\n" "$INSTALL_DIR"

mkdir -p "$INSTALL_DIR" "$BIN_DIR"

# 🧹 Limpiar instalación previa
if [[ -d "$INSTALL_DIR" ]]; then
  printf "${tr[prev_detected]}\n" "$INSTALL_DIR"

  [[ -d "$INSTALL_DIR/core/vault" ]] && echo "${tr[preserve_vault]}" && mv "$INSTALL_DIR/core/vault" /tmp/shflow_vault_backup
  [[ -d "$INSTALL_DIR/core/inventory" ]] && echo "${tr[preserve_inventory]}" && mv "$INSTALL_DIR/core/inventory" /tmp/shflow_inventory_backup
  [[ -d "$INSTALL_DIR/user_modules" ]] && echo "${tr[preserve_modules]}" && mv "$INSTALL_DIR/user_modules" /tmp/shflow_user_modules_backup

  echo "${tr[removing_old]}"
  rm -rf "$INSTALL_DIR"
  mkdir -p "$INSTALL_DIR"
fi

# 📥 Copiar archivos
echo "${tr[copying]}"
for file in shflow.sh shflow.tr.es shflow.tr.en vault.sh vault.tr.es vault.tr.en LICENSE README.md; do cp "$file" "$INSTALL_DIR/"; done
for dir in core community_modules user_modules examples; do cp -r "$dir" "$INSTALL_DIR/"; done

# 🔁 Restaurar backups
[[ -d "/tmp/shflow_vault_backup" ]] && echo "${tr[restore_vault]}" && rm -rf "$INSTALL_DIR/core/vault" && mv /tmp/shflow_vault_backup "$INSTALL_DIR/core/vault"
[[ -d "/tmp/shflow_inventory_backup" ]] && echo "${tr[restore_inventory]}" && rm -rf "$INSTALL_DIR/core/inventory" && mv /tmp/shflow_inventory_backup "$INSTALL_DIR/core/inventory"
[[ -d "/tmp/shflow_user_modules_backup" ]] && echo "${tr[restore_modules]}" && rm -rf "$INSTALL_DIR/user_modules" && mv /tmp/shflow_user_modules_backup "$INSTALL_DIR/user_modules"

# 🔗 Symlinks
ln -sf "$INSTALL_DIR/shflow.sh" "$BIN_DIR/shflow"
ln -sf "$INSTALL_DIR/vault.sh" "$BIN_DIR/shflow-vault"
ln -sf "$INSTALL_DIR/core/utils/shflow-doc.sh" "$BIN_DIR/shflow-doc"
ln -sf "$INSTALL_DIR/core/utils/module-docgen.sh" "$BIN_DIR/module-docgen"
ln -sf "$INSTALL_DIR/core/utils/shflow-check.sh" "$BIN_DIR/shflow-check"
ln -sf "$INSTALL_DIR/core/utils/shflow-trust.sh" "$BIN_DIR/shflow-trust"
ln -sf "$INSTALL_DIR/core/utils/shflow-ssh-init.sh" "$BIN_DIR/shflow-ssh-init"
ln -sf "$INSTALL_DIR/core/utils/vault-init.sh" "$BIN_DIR/vault-init"

# 🧠 Variables de entorno
PROFILE_FILE="$HOME/.bashrc"
[[ "$SHELL" == *zsh ]] && PROFILE_FILE="$HOME/.zshrc"

if ! grep -q "SHFLOW_HOME" "$PROFILE_FILE"; then
  echo "export SHFLOW_HOME=\"$INSTALL_DIR\"" >> "$PROFILE_FILE"
  echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$PROFILE_FILE"
  printf "${tr[env_added]}\n" "$PROFILE_FILE"
else
  printf "${tr[env_exists]}\n" "$PROFILE_FILE"
fi

# ✅ Finalización
echo ""
echo "${tr[done]}"
printf "${tr[installed]}\n" "$INSTALL_DIR"
printf "${tr[symlinks]}\n" "$BIN_DIR"
printf "${tr[restart]}\n" "$PROFILE_FILE"
echo "${tr[run]}"