/ lsf / install.sh
swadhin biswas feat: major v4.5.0 release with GNU ls compatibility and comprehensive documentation · 10/15/2025
20c1470
177 lines · 4.8 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
#!/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"

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

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="$BIN_DIR/$(basename "$src")"
    if [ -e "$dest" ] && [ "$FORCE" = false ]; then
        read -r -p "$dest exists. Overwrite? [y/N] " ans || ans=n
        if [[ ! "$ans" =~ ^[Yy]$ ]]; then
            echo "Skipping $dest"
            return
        fi
    fi
    cp "$src" "$dest"
    chmod +x "$dest"
    echo "Installed $dest"
}

# Install main script and helper (column.py) if present
install_file "$SCRIPT_DIR/lsf"
if [ -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