Bash · 2221 bytes Raw Blame History
1 # FORTRESS shell integration
2 # Add this to your .bashrc or .zshrc:
3 # source /usr/share/fortress/fortress.sh
4 #
5 # Then use: fortress
6 #
7 # This provides the cd-on-exit feature. Without sourcing this file,
8 # you can still run fortress but 'c' won't change your shell's directory.
9
10 fortress() {
11 # Determine which fortress binary to use
12 local fortress_exe
13
14 # Check standard install locations (lib path first, then legacy bin path)
15 if [ -x "/usr/lib/fortress/fortress" ]; then
16 fortress_exe="/usr/lib/fortress/fortress"
17 elif [ -x "/usr/lib64/fortress/fortress" ]; then
18 fortress_exe="/usr/lib64/fortress/fortress"
19 elif command -v fortress-bin &> /dev/null; then
20 # Legacy: fortress-bin in PATH (Homebrew, older packages)
21 fortress_exe="fortress-bin"
22 elif [ -n "$FORTRESS_BIN" ]; then
23 # Allow override via environment variable
24 fortress_exe="$FORTRESS_BIN"
25 elif [ -n "$FORTRESS_DIR" ]; then
26 # Development: FORTRESS_DIR points to repo root
27 fortress_exe="$FORTRESS_DIR/build/gfortran_"*"/app/fortress"
28 else
29 # Fallback: look for any fortress executable (NixOS, custom installs)
30 # Use command -v to find it, but filter out this function
31 local found
32 found=$(type -P fortress 2>/dev/null)
33 if [ -n "$found" ] && [ -x "$found" ]; then
34 fortress_exe="$found"
35 else
36 echo "fortress: binary not found. Set FORTRESS_BIN or FORTRESS_DIR." >&2
37 return 1
38 fi
39 fi
40
41 # Run fortress
42 "$fortress_exe" "$@"
43 local exit_code=$?
44
45 # Check if fortress wants us to cd somewhere
46 if [ -f "$HOME/.fortress_cd" ]; then
47 local target_dir
48 target_dir=$(cat "$HOME/.fortress_cd")
49 rm -f "$HOME/.fortress_cd"
50 if [ -n "$target_dir" ] && [ -d "$target_dir" ]; then
51 cd "$target_dir" || return 1
52 echo "fortress: changed directory to $(pwd)"
53 fi
54 fi
55
56 return $exit_code
57 }
58
59 # For zsh compatibility
60 if [ -n "$ZSH_VERSION" ]; then
61 # Nothing special needed for zsh
62 :
63 fi
64
65 # Export for subshells if needed (bash only)
66 if [ -n "$BASH_VERSION" ]; then
67 export -f fortress 2>/dev/null || true
68 fi