1+ #! /bin/sh
2+ # AIScript Installer
3+ # This script detects the user's OS and architecture and downloads the appropriate AIScript binary
4+
5+ set -e
6+
7+ # Print error message and exit
8+ die () {
9+ echo " Error: $1 " >&2
10+ exit 1
11+ }
12+
13+ # Get latest version from GitHub API
14+ get_latest_version () {
15+ if command -v curl > /dev/null 2>&1 ; then
16+ VERSION=$( curl -s https://api.github.com/repos/aiscript-dev/aiscript/releases/latest | grep ' "tag_name":' | sed -E ' s/.*"([^"]+)".*/\1/' )
17+ elif command -v wget > /dev/null 2>&1 ; then
18+ VERSION=$( wget -q -O- https://api.github.com/repos/aiscript-dev/aiscript/releases/latest | grep ' "tag_name":' | sed -E ' s/.*"([^"]+)".*/\1/' )
19+ else
20+ die " Neither curl nor wget found, please install one of them"
21+ fi
22+
23+ if [ -z " $VERSION " ]; then
24+ die " Could not determine the latest version"
25+ fi
26+
27+ echo " $VERSION "
28+ }
29+
30+ # Detect OS and architecture
31+ detect_platform () {
32+ OS=$( uname -s | tr ' [:upper:]' ' [:lower:]' )
33+ ARCH=$( uname -m)
34+
35+ case " $OS " in
36+ linux* )
37+ OS=" linux"
38+ ;;
39+ darwin* )
40+ OS=" macos"
41+ ;;
42+ msys* | mingw* | cygwin* | win* )
43+ OS=" windows"
44+ ;;
45+ * )
46+ die " Unsupported operating system: $OS "
47+ ;;
48+ esac
49+
50+ case " $ARCH " in
51+ x86_64 | amd64)
52+ ARCH=" x86_64"
53+ ;;
54+ arm64 | aarch64)
55+ if [ " $OS " = " macos" ]; then
56+ ARCH=" arm64"
57+ else
58+ die " Unsupported architecture: $ARCH for $OS "
59+ fi
60+ ;;
61+ * )
62+ die " Unsupported architecture: $ARCH "
63+ ;;
64+ esac
65+
66+ # Return platform identifier
67+ if [ " $OS " = " windows" ]; then
68+ echo " aiscript-$OS -$ARCH .exe"
69+ else
70+ echo " aiscript-$OS -$ARCH "
71+ fi
72+ }
73+
74+ # Download a file
75+ download () {
76+ URL=" $1 "
77+ OUTPUT=" $2 "
78+
79+ if command -v curl > /dev/null 2>&1 ; then
80+ HTTP_CODE=$( curl -L -s -w " %{http_code}" " $URL " -o " $OUTPUT " )
81+ if [ " $HTTP_CODE " != " 200" ]; then
82+ die " Failed to download $URL (HTTP Status: $HTTP_CODE )"
83+ fi
84+ elif command -v wget > /dev/null 2>&1 ; then
85+ if ! wget --server-response -q " $URL " -O " $OUTPUT " 2>&1 | grep -q ' 200 OK' ; then
86+ die " Failed to download $URL "
87+ fi
88+ else
89+ die " Neither curl nor wget found, please install one of them"
90+ fi
91+ }
92+
93+ # Verify the checksum of the downloaded file
94+ verify_checksum () {
95+ FILE=" $1 "
96+ EXPECTED_CHECKSUM=" $2 "
97+
98+ if command -v shasum > /dev/null 2>&1 ; then
99+ ACTUAL_CHECKSUM=$( shasum -a 256 " $FILE " | cut -d ' ' -f 1)
100+ elif command -v sha256sum > /dev/null 2>&1 ; then
101+ ACTUAL_CHECKSUM=$( sha256sum " $FILE " | cut -d ' ' -f 1)
102+ else
103+ echo " Warning: Neither shasum nor sha256sum found, skipping checksum verification"
104+ return 0
105+ fi
106+
107+ if [ " $ACTUAL_CHECKSUM " != " $EXPECTED_CHECKSUM " ]; then
108+ die " Checksum verification failed (expected: $EXPECTED_CHECKSUM , got: $ACTUAL_CHECKSUM )"
109+ fi
110+
111+ echo " Checksum verification successful"
112+ }
113+
114+ # Main function
115+ main () {
116+ echo " AIScript Installer"
117+ echo " ----------------"
118+
119+ # Detect platform
120+ PLATFORM=$( detect_platform)
121+ echo " Detected platform: $PLATFORM "
122+
123+ # Get latest version
124+ VERSION=$( get_latest_version)
125+ echo " Latest version: $VERSION "
126+
127+ # Download binary
128+ BINARY_URL=" https://github.com/aiscript-dev/aiscript/releases/download/${VERSION} /${PLATFORM} "
129+ CHECKSUM_URL=" https://github.com/aiscript-dev/aiscript/releases/download/${VERSION} /checksums.txt"
130+
131+ echo " Downloading AIScript binary..."
132+ TMP_DIR=$( mktemp -d)
133+ BINARY_PATH=" $TMP_DIR /aiscript"
134+ CHECKSUM_PATH=" $TMP_DIR /checksums.txt"
135+
136+ download " $BINARY_URL " " $BINARY_PATH "
137+ download " $CHECKSUM_URL " " $CHECKSUM_PATH "
138+
139+ # Verify checksum
140+ echo " Verifying checksum..."
141+ EXPECTED_CHECKSUM=$( grep " $PLATFORM " " $CHECKSUM_PATH " | cut -d ' ' -f 1)
142+ verify_checksum " $BINARY_PATH " " $EXPECTED_CHECKSUM "
143+
144+ # Install binary
145+ INSTALL_DIR=" $HOME /.aiscript/bin"
146+ INSTALL_PATH=" $INSTALL_DIR /aiscript"
147+
148+ mkdir -p " $INSTALL_DIR "
149+
150+ echo " Installing AIScript to $INSTALL_PATH ..."
151+ cp " $BINARY_PATH " " $INSTALL_PATH "
152+ chmod +x " $INSTALL_PATH "
153+
154+ # Clean up
155+ rm -rf " $TMP_DIR "
156+
157+ # Add to PATH if needed
158+ if [ -n " $SHELL " ]; then
159+ SHELL_NAME=$( basename " $SHELL " )
160+ if [ " $SHELL_NAME " = " bash" ]; then
161+ PROFILE=" $HOME /.bashrc"
162+ elif [ " $SHELL_NAME " = " zsh" ]; then
163+ PROFILE=" $HOME /.zshrc"
164+ elif [ " $SHELL_NAME " = " fish" ]; then
165+ PROFILE=" $HOME /.config/fish/config.fish"
166+ else
167+ PROFILE=" $HOME /.profile"
168+ fi
169+
170+ if ! echo " $PATH " | grep -q " $INSTALL_DIR " ; then
171+ echo " Adding AIScript to PATH in $PROFILE ..."
172+ if [ " $SHELL_NAME " = " fish" ]; then
173+ echo ' set -gx PATH $PATH ' " $INSTALL_DIR " >> " $PROFILE "
174+ else
175+ echo ' export PATH="$PATH:' " $INSTALL_DIR " ' "' >> " $PROFILE "
176+ fi
177+ echo " Please restart your shell or run 'source $PROFILE ' to update your PATH."
178+ fi
179+ else
180+ echo " To use AIScript, add $INSTALL_DIR to your PATH."
181+ fi
182+
183+ echo " AIScript $VERSION has been installed successfully!"
184+ echo " Run 'aiscript --help' to get started."
185+ }
186+
187+ main
0 commit comments