/ lsf / install.sh
swadhinbiswas FIX:tested · 2/15/2026
16dca6a
208 lines · 5.7 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/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 <<EOF
Usage: $0 [--user | --system] [--replace-ls] [--force] [--no-font-check]

Options:
  --user         Install to user local bin (~/.local/bin). Default when not root.
  --system       Install system-wide to /usr/local/bin. Requires sudo or root.
  --replace-ls   Replace system 'ls' command by creating a wrapper at /usr/local/bin/ls (backups created).
  --force        Overwrite existing files without prompt.
  --no-font-check  Skip checking for Nerd Fonts.
  -h, --help     Show this help message.

This installer will copy the bundled 'lsf' script and 'column.py' helper into the chosen bin directory
and create a config directory at ~/.config/lsf (if not present).
EOF
}

INSTALL_SYSTEM=false
REPLACE_LS=false
FORCE=false
FONT_CHECK=true

while [ "$#" -gt 0 ]; do
    case "$1" in
        --user)
            INSTALL_SYSTEM=false
            shift
            ;;
        --system)
            INSTALL_SYSTEM=true
            shift
            ;;
        --replace-ls)
            REPLACE_LS=true
            shift
            ;;
        --force)
            FORCE=true
            shift
            ;;
        --no-font-check)
            FONT_CHECK=false
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            echo "Unknown arg: $1"
            usage
            exit 2
            ;;
    esac
done

# Decide default install dir
if [ "$INSTALL_SYSTEM" = true ] || [ "$(id -u)" -eq 0 ]; then
    BIN_DIR="/usr/local/bin"
else
    BIN_DIR="$HOME/.local/bin"
fi

CONFIG_DIR="$HOME/.config/lsf"
CONFIG_FILE="$CONFIG_DIR/lsf.conf"

# Check for Rust version first
RUST_BINARY="$SCRIPT_DIR/target/release/lsf"
USE_RUST=false

if [ -f "$RUST_BINARY" ]; then
    USE_RUST=true
elif command -v cargo >/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