#!/usr/bin/env bash set -euo pipefail SCRIPT_NAME="lsf" VERSION="1" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" usage() { cat </dev/null 2>&1; then echo "Rust binary not found. Attempting to build with cargo..." if cargo build --release; then USE_RUST=true else echo "Warning: cargo build failed. Falling back to Bash version." >&2 fi fi if [ "$USE_RUST" = true ]; then echo "Using Rust version (v5.0.x)" LSF_SOURCE="$RUST_BINARY" else echo "Using legacy Bash version (v4.x)" if [ ! -f "$SCRIPT_DIR/lsf" ]; then echo "Error: unable to locate 'lsf' next to install.sh. Please run this installer from an ls-f checkout." >&2 exit 1 fi LSF_SOURCE="$SCRIPT_DIR/lsf" fi echo "Installing to: $BIN_DIR" if [ "$FONT_CHECK" = true ]; then if ! command -v fc-list >/dev/null 2>&1; then echo "Warning: 'fc-list' not available; skipping Nerd Font check. Use --no-font-check to suppress this check." >&2 else if ! fc-list | grep -i "Nerd Font" >/dev/null 2>&1; then echo "Warning: Nerd Fonts not detected. Icons may not render correctly." >&2 echo "Visit https://www.nerdfonts.com/ for installation instructions." >&2 fi fi fi # Create config dir and default config mkdir -p "$CONFIG_DIR" if [ ! -f "$CONFIG_FILE" ]; then cat > "$CONFIG_FILE" <<'EOL' # Config file for lsf # You can override the default icons and colors here. # Example: # ICONS["dir"]="" # COLORS["dir"]="\e[31m" EOL echo "Created default config at $CONFIG_FILE" fi # Ensure bin dir exists mkdir -p "$BIN_DIR" install_file() { local src="$1" local dest_name="$(basename "$src")" local dest="$BIN_DIR/$dest_name" if [ -e "$dest" ] && [ "$FORCE" = false ]; then # If running in non-interactive mode (CI), don't ask if [ -t 0 ]; then read -r -p "$dest exists. Overwrite? [y/N] " ans || ans=n if [[ ! "$ans" =~ ^[Yy]$ ]]; then echo "Skipping $dest" return fi else echo "Overwriting $dest (non-interactive mode)" fi fi cp "$src" "$dest" chmod +x "$dest" echo "Installed $dest" } # Install main script/binary install_file "$LSF_SOURCE" # Install helper (column.py) only for legacy version if [ "$USE_RUST" = false ] && [ -f "$SCRIPT_DIR/column.py" ]; then install_file "$SCRIPT_DIR/column.py" fi if [ "$REPLACE_LS" = true ]; then # Only allow replace-ls when installing system-wide if [ "$BIN_DIR" != "/usr/local/bin" ]; then echo "--replace-ls requires system installation (run with --system or sudo)." >&2 else LS_WRAPPER="/usr/local/bin/ls" if [ -e "$LS_WRAPPER" ] && [ "$FORCE" = false ]; then read -r -p "$LS_WRAPPER exists. Backup and replace it? [y/N] " ans || ans=n if [[ ! "$ans" =~ ^[Yy]$ ]]; then echo "Skipping replacement of ls." else mv "$LS_WRAPPER" "$LS_WRAPPER.bak.$(date +%s)" fi fi cat > "$LS_WRAPPER" <<'EOF' #!/usr/bin/env bash # Wrapper to prefer lsf; falls back to original ls if needed if command -v lsf >/dev/null 2>&1; then lsf "$@" else /bin/ls "$@" fi EOF chmod +x "$LS_WRAPPER" echo "Installed ls wrapper at $LS_WRAPPER (original backed up if present)." fi fi echo "Installation complete." if [[ ":$PATH:" != *":$BIN_DIR:"* ]]; then echo "Note: $BIN_DIR is not in your PATH. Add it to be able to run 'lsf' without a full path." >&2 fi echo "To use lsf as your default 'ls', add the following to your shell rc (e.g. ~/.bashrc or ~/.zshrc):" echo " alias ls='lsf'" if [ -f "$SCRIPT_DIR/config/lsf_alias_setup.sh" ]; then echo "If you want me to set the alias automatically, run:" echo " source $SCRIPT_DIR/config/lsf_alias_setup.sh" fi echo "Verify the installation with:" echo " lsf --version" echo " lsf -x" exit 0