scripts/install-deps.sh
1#!/bin/bash
2# =============================================================================
3# Neocities Modernization - Dependency Installation Script
4# Installs all external dependencies locally to this project
5#
6# This script orchestrates installation of:
7# - Effil (Lua multithreading library)
8# - Other dependencies as needed
9#
10# Usage: ./scripts/install-deps.sh [--clean] [--check]
11# =============================================================================
12
13set -euo pipefail
14
15# {{{ Configuration
16SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
18LIBS_DIR="${PROJECT_DIR}/libs"
19NATIVE_LIBS_DIR="${LIBS_DIR}/native"
20
21# Effil configuration
22EFFIL_VERSION="1.2.0"
23EFFIL_REPO="https://github.com/effil/effil.git"
24EFFIL_TAG="v${EFFIL_VERSION}"
25EFFIL_DIR="${NATIVE_LIBS_DIR}/effil"
26
27# Use LuaJIT for this project (parallel processing)
28LUA_VERSION="luajit"
29
30# Colors
31RED='\033[0;31m'
32GREEN='\033[0;32m'
33YELLOW='\033[1;33m'
34CYAN='\033[0;36m'
35NC='\033[0m'
36
37log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
38log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
39log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
40# }}}
41
42# {{{ check_dependencies
43check_dependencies() {
44 log_info "Checking system dependencies..."
45
46 local missing=()
47
48 command -v cmake &>/dev/null || missing+=("cmake")
49 command -v git &>/dev/null || missing+=("git")
50 command -v g++ &>/dev/null || command -v clang++ &>/dev/null || missing+=("g++ or clang++")
51 command -v luajit &>/dev/null || missing+=("luajit")
52
53 if [[ ${#missing[@]} -gt 0 ]]; then
54 log_error "Missing: ${missing[*]}"
55 echo "Install with: sudo apt-get install cmake git g++ luajit libluajit-5.1-dev"
56 return 1
57 fi
58
59 log_info "All system dependencies found"
60 return 0
61}
62# }}}
63
64# {{{ install_effil
65install_effil() {
66 log_info "Installing Effil (Lua multithreading)..."
67
68 # Check if already built
69 if [[ -f "${EFFIL_DIR}/build/effil.so" ]]; then
70 log_info "Effil already installed"
71 return 0
72 fi
73
74 mkdir -p "$NATIVE_LIBS_DIR"
75
76 # Clone if needed
77 if [[ ! -d "${EFFIL_DIR}/.git" ]]; then
78 log_info "Cloning Effil..."
79 git clone --depth 1 --branch "$EFFIL_TAG" "$EFFIL_REPO" "$EFFIL_DIR"
80 cd "$EFFIL_DIR"
81 git submodule update --init --recursive
82 fi
83
84 # Build
85 log_info "Building Effil..."
86 mkdir -p "${EFFIL_DIR}/build"
87 cd "${EFFIL_DIR}/build"
88
89 # Find LuaJIT paths
90 local lua_include="/usr/include/luajit-2.1"
91 [[ ! -d "$lua_include" ]] && lua_include="/usr/include/luajit-2.0"
92
93 cmake .. \
94 -DCMAKE_BUILD_TYPE=Release \
95 -DLUA_INCLUDE_DIR="$lua_include"
96
97 make -j"$(nproc)"
98
99 log_info "Effil built successfully"
100 cd "$PROJECT_DIR"
101}
102# }}}
103
104# {{{ create_lua_paths
105create_lua_paths() {
106 local paths_file="${LIBS_DIR}/paths.lua"
107
108 log_info "Creating Lua paths configuration..."
109
110 cat > "$paths_file" << 'EOF'
111-- Auto-generated by install-deps.sh
112-- Add this at the start of your Lua scripts:
113-- dofile('libs/paths.lua')
114
115local project_root = debug.getinfo(1, "S").source:match("@(.*/)")
116if project_root then
117 project_root = project_root:gsub("/libs/$", "")
118else
119 project_root = "."
120end
121
122-- Add native library paths (effil.so)
123package.cpath = package.cpath .. ";" .. project_root .. "/libs/native/effil/build/?.so"
124
125-- Add Lua library paths
126package.path = package.path .. ";" .. project_root .. "/libs/?.lua"
127package.path = package.path .. ";" .. project_root .. "/src/?.lua"
128
129return project_root
130EOF
131
132 log_info "Created: $paths_file"
133}
134# }}}
135
136# {{{ verify_installation
137verify_installation() {
138 log_info "Verifying installation..."
139
140 cd "$PROJECT_DIR"
141
142 # Test effil
143 if luajit -e "package.cpath = './libs/native/effil/build/?.so;' .. package.cpath; local e = require('effil'); print('Effil OK, threads:', e.hardware_threads())" 2>/dev/null; then
144 log_info "Effil: OK"
145 else
146 log_error "Effil: FAILED"
147 return 1
148 fi
149
150 return 0
151}
152# }}}
153
154# {{{ clean
155clean() {
156 log_info "Cleaning native libraries..."
157 rm -rf "$NATIVE_LIBS_DIR"
158 log_info "Cleaned"
159}
160# }}}
161
162# {{{ show_usage
163show_usage() {
164 echo "Usage: $0 [OPTIONS]"
165 echo ""
166 echo "Options:"
167 echo " --check Check if dependencies are installed"
168 echo " --clean Remove all installed native libraries"
169 echo " --help Show this help"
170}
171# }}}
172
173# {{{ main
174main() {
175 cd "$PROJECT_DIR"
176
177 case "${1:-}" in
178 --check)
179 check_dependencies && verify_installation
180 exit $?
181 ;;
182 --clean)
183 clean
184 exit 0
185 ;;
186 --help|-h)
187 show_usage
188 exit 0
189 ;;
190 esac
191
192 echo ""
193 echo -e "${CYAN}=== Neocities Modernization Dependency Installer ===${NC}"
194 echo ""
195
196 check_dependencies || exit 1
197
198 install_effil
199 create_lua_paths
200 verify_installation
201
202 echo ""
203 log_info "Installation complete!"
204 echo ""
205 echo "To use in your Lua scripts, add at the top:"
206 echo " dofile('libs/paths.lua')"
207 echo ""
208 echo "Or manually add to package.cpath:"
209 echo " package.cpath = './libs/native/effil/build/?.so;' .. package.cpath"
210 echo ""
211}
212
213main "$@"
214# }}}
215