|
| 1 | +# Shell Archive Pattern for SEFACA |
| 2 | + |
| 3 | +## What is a Shell Archive? |
| 4 | + |
| 5 | +A shell archive (shar) is a self-extracting script that contains embedded files. When executed, it creates the necessary files and directories on the target system. SEFACA uses this pattern for reliable, single-file distribution. |
| 6 | + |
| 7 | +## How SEFACA Uses This Pattern |
| 8 | + |
| 9 | +### 1. Structure of `install-standalone.sh` |
| 10 | + |
| 11 | +```bash |
| 12 | +#!/bin/sh |
| 13 | +# Installation script header |
| 14 | +# - Sets up variables |
| 15 | +# - Creates directories |
| 16 | +# - Shows banner |
| 17 | + |
| 18 | +# Embedded file using heredoc |
| 19 | +cat > "${INSTALL_DIR}/sefaca.sh" << 'SEFACA_EOF' |
| 20 | +#!/bin/bash |
| 21 | +# This is the entire sefaca.sh script |
| 22 | +# ... hundreds of lines of code ... |
| 23 | +SEFACA_EOF |
| 24 | + |
| 25 | +# Post-installation |
| 26 | +# - Set permissions |
| 27 | +# - Create loader script |
| 28 | +# - Show instructions |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Key Benefits |
| 32 | + |
| 33 | +- **Single Download**: One `curl` command gets everything |
| 34 | +- **No Network Dependencies**: After initial download, no more network calls |
| 35 | +- **No 404 Errors**: Can't fail to find secondary files |
| 36 | +- **Self-Contained**: Everything needed is in one file |
| 37 | +- **Version Locked**: The embedded content matches the installer version |
| 38 | + |
| 39 | +### 3. The Installation Flow |
| 40 | + |
| 41 | +``` |
| 42 | +User runs curl |
| 43 | + ↓ |
| 44 | +Downloads install-standalone.sh (350+ lines) |
| 45 | + ↓ |
| 46 | +Shell executes the script |
| 47 | + ↓ |
| 48 | +Script creates ~/.sefaca/bin/ |
| 49 | + ↓ |
| 50 | +Script writes sefaca.sh (embedded content) |
| 51 | + ↓ |
| 52 | +Script creates load-sefaca wrapper |
| 53 | + ↓ |
| 54 | +Installation complete! |
| 55 | +``` |
| 56 | + |
| 57 | +### 4. Why Not Download sefaca.sh Separately? |
| 58 | + |
| 59 | +Our failed attempts taught us: |
| 60 | +- GitHub raw URLs can 404 |
| 61 | +- Gist URLs need exact formatting |
| 62 | +- Multiple downloads = multiple failure points |
| 63 | +- Caching delays cause version mismatches |
| 64 | + |
| 65 | +### 5. The Heredoc Technique |
| 66 | + |
| 67 | +```bash |
| 68 | +cat > "output_file" << 'END_MARKER' |
| 69 | +This content is written exactly as-is. |
| 70 | +No variable expansion because marker is quoted. |
| 71 | +Perfect for embedding shell scripts. |
| 72 | +END_MARKER |
| 73 | +``` |
| 74 | + |
| 75 | +Using `'SEFACA_EOF'` (quoted) prevents the shell from interpreting variables in the embedded script. |
| 76 | + |
| 77 | +## Creating Your Own Shell Archive |
| 78 | + |
| 79 | +1. **Start with your installer template**: |
| 80 | +```bash |
| 81 | +#!/bin/sh |
| 82 | +set -e |
| 83 | +echo "Installing..." |
| 84 | +mkdir -p "$HOME/.myapp" |
| 85 | +``` |
| 86 | + |
| 87 | +2. **Embed your application**: |
| 88 | +```bash |
| 89 | +cat > "$HOME/.myapp/app.sh" << 'APP_EOF' |
| 90 | +#!/bin/bash |
| 91 | +# Your entire application here |
| 92 | +echo "Hello from embedded app!" |
| 93 | +APP_EOF |
| 94 | +chmod +x "$HOME/.myapp/app.sh" |
| 95 | +``` |
| 96 | + |
| 97 | +3. **Test the archive**: |
| 98 | +```bash |
| 99 | +sh your-installer.sh |
| 100 | +``` |
| 101 | + |
| 102 | +## Real-World Examples |
| 103 | + |
| 104 | +- **Rustup**: Rust's installer uses this pattern |
| 105 | +- **nvm**: Node Version Manager installation |
| 106 | +- **rbenv-installer**: Ruby version management |
| 107 | +- **SEFACA**: Our implementation for AI agent safety |
| 108 | + |
| 109 | +## Best Practices |
| 110 | + |
| 111 | +1. **Use quoted heredocs** to prevent variable expansion |
| 112 | +2. **Include version info** in both installer and embedded content |
| 113 | +3. **Make scripts executable** with `chmod +x` |
| 114 | +4. **Provide clear post-install instructions** |
| 115 | +5. **Test on minimal shells** (sh, not just bash) |
| 116 | + |
| 117 | +## The SEFACA Success |
| 118 | + |
| 119 | +From our deployment experiment: |
| 120 | +- ❌ Multi-file download approach: Failed with 404s |
| 121 | +- ❌ GitHub raw URLs: Caching and availability issues |
| 122 | +- ✅ Shell archive pattern: Worked first time on pi.lan! |
| 123 | + |
| 124 | +This pattern enabled our "curl|sh" installation to work reliably across platforms. |
0 commit comments