1+ #! /bin/sh
2+ set -e
3+
4+ # Colors
5+ RED=' \033[0;31m'
6+ GREEN=' \033[0;32m'
7+ YELLOW=' \033[1;33m'
8+ NC=' \033[0m' # No Color
9+
10+ log_info () {
11+ printf " ${GREEN} [INFO]${NC} %s\n" " $1 "
12+ }
13+
14+ log_warn () {
15+ printf " ${YELLOW} [WARN]${NC} %s\n" " $1 "
16+ }
17+
18+ log_error () {
19+ printf " ${RED} [ERROR]${NC} %s\n" " $1 "
20+ }
21+
22+ # Detect OS
23+ detect_os () {
24+ case " $( uname -s) " in
25+ Linux* ) echo " linux" ;;
26+ Darwin* ) echo " darwin" ;;
27+ CYGWIN* ) echo " windows" ;;
28+ MINGW* ) echo " windows" ;;
29+ * ) echo " unsupported" ;;
30+ esac
31+ }
32+
33+ # Detect architecture
34+ detect_arch () {
35+ case " $( uname -m) " in
36+ x86_64) echo " amd64" ;;
37+ arm64|aarch64) echo " arm64" ;;
38+ * ) echo " amd64" ;;
39+ esac
40+ }
41+
42+ # Detect config directory (matches Go's os.UserConfigDir)
43+ detect_config_dir () {
44+ case " $( uname -s) " in
45+ Darwin* ) echo " $HOME /Library/Application Support" ;;
46+ Linux* ) echo " $HOME /.config" ;;
47+ CYGWIN* |MINGW* ) echo " $APPDATA " ;;
48+ * ) echo " $HOME /.config" ;;
49+ esac
50+ }
51+
52+ # Get latest version from GitHub
53+ get_latest_version () {
54+ curl -sL " https://api.github.com/repos/menloresearch/cli/releases/latest" | grep -o ' "tag_name": "v[^"]*"' | cut -d' "' -f4 | cut -d' v' -f2
55+ }
56+
57+ # Install menlo
58+ install () {
59+ OS=$( detect_os)
60+ ARCH=$( detect_arch)
61+
62+ log_info " Detected OS: $OS , Arch: $ARCH "
63+
64+ # Get version (default to latest if not set)
65+ VERSION=" ${VERSION:- $(get_latest_version)} "
66+
67+ if [ -z " $VERSION " ]; then
68+ log_warn " Could not fetch latest version, using v0.0.4"
69+ VERSION=" v0.0.4"
70+ fi
71+
72+ # Remove 'v' prefix if present
73+ VERSION=" ${VERSION# v} "
74+
75+ # Binary name
76+ BINARY_NAME=" menlo"
77+ if [ " $OS " = " windows" ]; then
78+ BINARY_NAME=" menlo.exe"
79+ fi
80+
81+ # Download URL
82+ DOWNLOAD_URL=" https://github.com/menloresearch/cli/releases/download/v${VERSION} /${BINARY_NAME} _${VERSION} _${OS} _${ARCH} .tar.gz"
83+
84+ log_info " Downloading menlo v${VERSION} ..."
85+ log_info " URL: $DOWNLOAD_URL "
86+
87+ # Create temp directory
88+ TEMP_DIR=$( mktemp -d)
89+ cd " $TEMP_DIR "
90+
91+ # Download and extract
92+ if curl -sL " $DOWNLOAD_URL " -o " menlo.tar.gz" ; then
93+ tar -xzf " menlo.tar.gz" || log_error " Failed to extract archive"
94+ else
95+ log_error " Failed to download menlo"
96+ cd /
97+ rm -rf " $TEMP_DIR "
98+ exit 1
99+ fi
100+
101+ # Check if binary exists
102+ if [ ! -f " $BINARY_NAME " ]; then
103+ # Try alternative naming pattern
104+ DOWNLOAD_URL=" https://github.com/menloresearch/cli/releases/download/v${VERSION} /menlo_${VERSION} _${OS} _${ARCH} .tar.gz"
105+ log_info " Trying alternate URL: $DOWNLOAD_URL "
106+
107+ curl -sL " $DOWNLOAD_URL " -o " menlo.tar.gz" || {
108+ log_error " Failed to download. Please check if version $VERSION exists."
109+ cd /
110+ rm -rf " $TEMP_DIR "
111+ exit 1
112+ }
113+ tar -xzf " menlo.tar.gz"
114+ fi
115+
116+ # Install to /usr/local/bin (may need sudo)
117+ log_info " Installing to /usr/local/bin..."
118+ if [ -w /usr/local/bin ]; then
119+ cp " $BINARY_NAME " /usr/local/bin/menlo
120+ chmod +x /usr/local/bin/menlo
121+ else
122+ log_warn " Need sudo to install to /usr/local/bin"
123+ sudo cp " $BINARY_NAME " /usr/local/bin/menlo
124+ sudo chmod +x /usr/local/bin/menlo
125+ fi
126+
127+ # Write version to config (preserve existing config)
128+ CONFIG_BASE_DIR=$( detect_config_dir)
129+ CONFIG_DIR=" $CONFIG_BASE_DIR /menlo"
130+ mkdir -p " $CONFIG_DIR "
131+ CONFIG_FILE=" $CONFIG_DIR /config.yaml"
132+ if [ -f " $CONFIG_FILE " ]; then
133+ # Use awk to update version in YAML - replace existing or add new
134+ awk -v ver=" $VERSION " '
135+ /^version:/ { print "version: \"" ver "\""; found=1; next }
136+ { print }
137+ END { if (!found) print "version: \"" ver "\"" }
138+ ' " $CONFIG_FILE " > " $CONFIG_FILE .tmp" && mv " $CONFIG_FILE .tmp" " $CONFIG_FILE "
139+ else
140+ printf ' version: "%s"\n' " $VERSION " > " $CONFIG_FILE "
141+ fi
142+ log_info " Version $VERSION written to config"
143+
144+ # Cleanup
145+ cd /
146+ rm -rf " $TEMP_DIR "
147+
148+ log_info " menlo v${VERSION} installed successfully!"
149+ log_info " Run 'menlo init' to get started"
150+ }
151+
152+ # Check if curl is installed
153+ if ! command -v curl > /dev/null 2>&1 ; then
154+ log_error " curl is required but not installed. Please install curl first."
155+ exit 1
156+ fi
157+
158+ # Parse arguments
159+ while [ $# -gt 0 ]; do
160+ case " $1 " in
161+ -h|--help)
162+ echo " Usage: sh install.sh"
163+ echo " "
164+ echo " Options:"
165+ echo " -h, --help Show this help message"
166+ exit 0
167+ ;;
168+ * )
169+ log_error " Unknown option: $1 "
170+ echo " Use -h or --help for usage information"
171+ exit 1
172+ ;;
173+ esac
174+ done
175+
176+ # Run install
177+ install
0 commit comments