aboutsummaryrefslogtreecommitdiffstats
path: root/claude-code-bin/claude-code-bin.SlackBuild
blob: 7d3e1b48e931a60b3fcdb8881b9aee1e4840f10f (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash

# Slackware build script for claude-code-bin

# Copyright 2026 danix <danix@danix.xyz>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=claude-code-bin
SRCNAM=claude
VERSION=${VERSION:-2.1.114}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
  echo "ERROR: claude-code-bin is not supported on 32-bit x86" >&2
  exit 1
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  echo "ERROR: claude-code-bin is not supported on 32-bit x86" >&2
  exit 1
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  ARCH_LABEL="x86_64"
  PLATFORM="linux-x64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX="64"
  ARCH_LABEL="aarch64"
  PLATFORM="linux-arm64"
else
  echo "ERROR: Unsupported architecture: $ARCH" >&2
  exit 1
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP

# Check for required downloader
DOWNLOADER=""
if command -v curl >/dev/null 2>&1; then
  DOWNLOADER="curl"
elif command -v wget >/dev/null 2>&1; then
  DOWNLOADER="wget"
else
  echo "ERROR: curl or wget is required but neither is installed" >&2
  exit 1
fi

# Check if jq is available (optional)
HAS_JQ=false
if command -v jq >/dev/null 2>&1; then
  HAS_JQ=true
fi

# Download function that works with both curl and wget
download_file() {
  local url="$1"
  local output="$2"

  if [ "$DOWNLOADER" = "curl" ]; then
    if [ -n "$output" ]; then
      curl -fsSL -o "$output" "$url"
    else
      curl -fsSL "$url"
    fi
  elif [ "$DOWNLOADER" = "wget" ]; then
    if [ -n "$output" ]; then
      wget -q -O "$output" "$url"
    else
      wget -q -O - "$url"
    fi
  else
    return 1
  fi
}

# Simple JSON parser for extracting checksum when jq is not available
get_checksum_from_manifest() {
  local json="$1"
  local platform="$2"

  # Normalize JSON to single line and extract checksum
  json=$(echo "$json" | tr -d '\n\r\t' | sed 's/ \+/ /g')

  # Extract checksum for platform using bash regex
  if [[ $json =~ \"$platform\"[^}]*\"checksum\"[[:space:]]*:[[:space:]]*\"([a-f0-9]{64})\" ]]; then
    echo "${BASH_REMATCH[1]}"
    return 0
  fi

  return 1
}

DOWNLOAD_BASE_URL="https://downloads.claude.ai/claude-code-releases"

# Resolve version if set to 'latest'
if [ "$VERSION" = "latest" ]; then
  echo "Fetching latest version tag..."
  RESOLVED_VERSION=$(download_file "$DOWNLOAD_BASE_URL/latest")
  if [ -z "$RESOLVED_VERSION" ]; then
    echo "ERROR: Could not resolve latest version" >&2
    exit 1
  fi
else
  RESOLVED_VERSION="$VERSION"
fi

echo "Building claude-code-bin version: $RESOLVED_VERSION"

# Download manifest and extract checksum
echo "Fetching manifest..."
MANIFEST_JSON=$(download_file "$DOWNLOAD_BASE_URL/$RESOLVED_VERSION/manifest.json")

# Use jq if available, otherwise fall back to pure bash parsing
if [ "$HAS_JQ" = "true" ]; then
  CHECKSUM=$(echo "$MANIFEST_JSON" | jq -r ".platforms[\"$PLATFORM\"].checksum // empty")
else
  CHECKSUM=$(get_checksum_from_manifest "$MANIFEST_JSON" "$PLATFORM")
fi

if [ -z "$CHECKSUM" ] || [[ ! "$CHECKSUM" =~ ^[a-f0-9]{64}$ ]]; then
  echo "ERROR: Could not get checksum for platform $PLATFORM from manifest" >&2
  exit 1
fi

echo "SHA-256 checksum for $PLATFORM: $CHECKSUM"

# Download and verify binary
BINARY_PATH="$TMP/claude-$RESOLVED_VERSION-$PLATFORM"

echo "Downloading claude binary..."
if ! download_file "$DOWNLOAD_BASE_URL/$RESOLVED_VERSION/$PLATFORM/claude" "$BINARY_PATH"; then
  echo "ERROR: Download failed" >&2
  rm -f "$BINARY_PATH"
  exit 1
fi

echo "Verifying SHA-256 checksum..."
ACTUAL_CHECKSUM=$(sha256sum "$BINARY_PATH" | cut -d' ' -f1)
if [ "$ACTUAL_CHECKSUM" != "$CHECKSUM" ]; then
  echo "ERROR: SHA-256 checksum mismatch" >&2
  echo "  Expected: $CHECKSUM" >&2
  echo "  Got:      $ACTUAL_CHECKSUM" >&2
  rm -f "$BINARY_PATH"
  exit 1
fi

echo "Checksum verified successfully"

chmod +x "$BINARY_PATH"

# Install binary to /usr/bin
mkdir -p $PKG/usr/bin
install -D -m 0755 "$BINARY_PATH" $PKG/usr/bin/claude

# Copy documentation
mkdir -p $PKG/usr/doc/$PRGNAM-$RESOLVED_VERSION
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$RESOLVED_VERSION/README
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$RESOLVED_VERSION/$PRGNAM.SlackBuild

# Install slack-desc (doinst.sh is not needed for this package)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Remove downloaded binary
rm -f "$BINARY_PATH"

# Create the package
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH_LABEL-$BUILD$TAG.$PKGTYPE