Ticket #21533: 21533.3.sh

File 21533.3.sh, 5.7 KB (added by taylor.smock, 23 months ago)

Add auto updates

Line 
1#!/bin/zsh
2set -x
3# Install file, curl, jq and LLVM v15 in whatever distribution you are using, e.g. `apt install file curl llvm-15 jq`
4# API documentation: https://api.azul.com/metadata/v1/docs/swagger
5JAVA_VERSION=21;
6JAVA_TYPE="jre"
7BASE_URL="https://api.azul.com/metadata/v1/zulu/packages/?java_version=${JAVA_VERSION}&os=macos&archive_type=tar.gz&java_package_type=${JAVA_TYPE}&javafx_bundled=true&crac_supported=false&crs_supported=false&support_term=lts&latest=true&release_status=ga&availability_types=CA&certifications=tck&page=1&page_size=100&arch="
8X86=$(curl ${BASE_URL}amd64)
9AARCH64=$(curl ${BASE_URL}aarch64)
10WORKDIR=workdir
11AARCH64_NAME=$(echo "${AARCH64}" | jq -r '.[].name')
12X86_NAME=$(echo "${X86}" | jq -r '.[].name')
13
14function merge() {
15 if [ "$(command -v lipo)" ]; then
16 lipo -create -output "${1}" "${2}" "${3}"
17 elif [ "$(command -v llvm-lipo-15)" ]; then
18 llvm-lipo-15 -create -output "${1}" "${2}" "${3}"
19 fi
20}
21
22function get_jres() {
23 local localPwd=$(pwd)
24 if [ ! -d "${WORKDIR}" ]; then mkdir "${WORKDIR}"; fi
25 cd "${WORKDIR}" || exit 2
26 curl --location "$(echo "${AARCH64}" | jq -r '.[].download_url')" --output "${AARCH64_NAME}"
27 curl --location "$(echo "${X86}" | jq -r '.[].download_url')" --output "${X86_NAME}"
28 echo "${AARCH64}" > ".version"
29 cd "${localPwd}" || exit 2
30}
31
32function copy() {
33 # Trim the root path
34 FILE="${1#*/}"
35 if [ ! -e "target/${FILE}" ]; then
36 # Only make directories if we aren't looking at the root files
37 if [[ "${FILE}" == *"/"* ]]; then mkdir -p "target/${FILE%/*}"; fi
38 if file "${1}" | grep -q 'Mach-O' ; then
39 merge "target/${FILE}" "x64/${FILE}" "aarch64/${FILE}"
40 if file "${1}" | grep -q 'executable'; then
41 chmod 755 "target/${FILE}"
42 fi
43 else
44 cp -a "${1}" "target/${FILE}"
45 fi
46 fi
47}
48
49function directory_iterate() {
50 while IFS= read -r -d '' file
51 do
52 copy "${file}" &
53 done < <(find "${1}" -type f,l -print0)
54 wait
55}
56
57function create_universal_target() {
58 local localPwd="$(pwd)"
59 cd "${WORKDIR}" || exit 2
60 mkdir aarch64 && tar --strip-components 1 --directory=aarch64 -xf "${AARCH64_NAME}"
61 mkdir x64 && tar --strip-components 1 --directory=x64 -xf "${X86_NAME}"
62 #if [ -d target ]; then rm -r target; fi
63 mkdir target
64 # Do both just in case there are non-shared files
65 directory_iterate aarch64
66 directory_iterate x64
67 # Remove extracted files to save disk space
68 #rm -rf x64
69 #rm -rf aarch64
70 cd "${localPwd}" || exit 2
71}
72
73function package() {
74 local localPwd="$(pwd)"
75 # Remove generic JDK information
76 cd "${1}/"*"${JAVA_TYPE}"*"/Contents" || exit 2
77 rm -r "Info.plist"
78 rm -r "MacOS"
79 rm -r "_CodeSignature"
80 # The jre directory is what we point to as "JAVA_HOME" in the script; this is the bundled jdk.
81 mv "Home" "jre"
82 cd ${localPwd} || exit 2
83 jar --create --file jre.os-x-"$(jq -r '.[].java_version | join(".")' < "${WORKDIR}/.version").jar" -C "${WORKDIR}/target/"*"${JAVA_TYPE}"*"/Contents" .
84}
85
86function generate_josm() {
87 mkdir -p "${WORKDIR}/JOSM.app/Contents/MacOS"
88 mkdir -p "${WORKDIR}/JOSM.app/Contents/jars"
89 cp -R "${WORKDIR}/target/"*"${JAVA_TYPE}"*"/Contents" "${WORKDIR}/JOSM.app/"
90 cat << EOF > "${WORKDIR}/JOSM.app/Contents/MacOS/JOSM"
91#!/bin/bash
92set -ex
93cd "\$(dirname \$0)/.."
94BRANCH="tested"
95function update_josm() {
96 curl -L -o "jars/josm-\${BRANCH}.jar.new" -z "jars/josm-\${BRANCH}.jar" https://josm.openstreetmap.de/josm-\${BRANCH}.jar
97 if [ -f "jars/josm-\${BRANCH}.jar.new" ]; then
98 # Check java compatibility
99 JAVA_HOME="./jre/" ./jre/bin/java \${JAVA_OPTS} -jar "jars/josm-\${BRANCH}.jar.new" --version || return
100 # Then move new jar file to start location
101 mv "jars/josm-\${BRANCH}.jar.new" "jars/josm-\${BRANCH}.jar"
102 fi
103}
104
105JAVA_OPTS="--add-modules java.scripting,java.sql,javafx.controls,javafx.media,javafx.swing,javafx.web \${JAVA_OPTS}"
106JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.base/sun.security.action=ALL-UNNAMED"
107JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.desktop/com.sun.imageio.plugins.jpeg=ALL-UNNAMED"
108JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.desktop/com.sun.imageio.spi=ALL-UNNAMED"
109
110function start_josm() {
111 if arch -arm64 /bin/bash -c 'echo arm64'; then
112 arch -arm64 -e JAVA_HOME="./jre/" ./jre/bin/java \${JAVA_OPTS} -jar jars/josm-\${BRANCH}.jar "\${@}"
113 else
114 JAVA_HOME="./jre/" ./jre/bin/java \${JAVA_OPTS} -jar jars/josm-\${BRANCH}.jar "\${@}"
115 fi
116}
117
118update_josm
119while true; do
120 start_josm
121 if [ "z\$?" != "z9" ]; then
122 break;
123 fi
124 echo ">> restarting josm..."
125done
126EOF
127 chmod +x "${WORKDIR}/JOSM.app/Contents/MacOS/JOSM"
128 curl -L https://josm.openstreetmap.de/josm-tested.jar --output "${WORKDIR}/JOSM.app/Contents/jars/josm-tested.jar" -z "${WORKDIR}/JOSM.app/Contents/jars/josm-tested.jar"
129 # TODO generate Info.plist (this is the only bit that is specific to Mac)
130 # Note: if we keep a static copy of it, note how it was generated here!
131 cp /Applications/JOSM.app/Contents/Info.plist "${WORKDIR}/JOSM.app/Contents"
132 cp -r /Applications/JOSM.app/Contents/Resources "${WORKDIR}/JOSM.app/Contents/"
133}
134
135if [ "$(echo "${AARCH64}" | jq '.[].distro_version')" == "$(echo "${X86}" | jq '.[].distro_version')" ]; then
136 if [ -d "${WORKDIR}" ] && [ -f "${WORKDIR}/.version" ]; then
137 if [ "$(echo "${AARCH64}" | jq '.[].distro_version')" != "$(jq '.[].distro_version' < "${WORKDIR}/.version")" ]; then
138 rm -rf "${WORKDIR}"
139 get_jres
140 fi
141 else
142 get_jres
143 fi
144 if [ ! -d "${WORKDIR}/target" ]; then create_universal_target; fi
145 if [ ! -f "jre.os-x-$(jq -r '.[].java_version | join(".")' < "${WORKDIR}/.version").jar" ]; then package "${WORKDIR}/target"; fi
146 # Everything up to this point is usable by other people
147 generate_josm
148else
149 echo "Mismatched versions"
150 echo "${X86}" | jq
151 echo "${AARCH64}" | jq
152 exit 1
153fi