| 1 | #!/bin/zsh
|
|---|
| 2 | set -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
|
|---|
| 5 | JAVA_VERSION=21;
|
|---|
| 6 | JAVA_TYPE="jre"
|
|---|
| 7 | BASE_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="
|
|---|
| 8 | X86=$(curl ${BASE_URL}amd64)
|
|---|
| 9 | AARCH64=$(curl ${BASE_URL}aarch64)
|
|---|
| 10 | WORKDIR=workdir
|
|---|
| 11 | AARCH64_NAME=$(echo "${AARCH64}" | jq -r '.[].name')
|
|---|
| 12 | X86_NAME=$(echo "${X86}" | jq -r '.[].name')
|
|---|
| 13 |
|
|---|
| 14 | function 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 |
|
|---|
| 22 | function 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 |
|
|---|
| 32 | function 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 |
|
|---|
| 49 | function 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 |
|
|---|
| 57 | function 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 |
|
|---|
| 73 | function 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 |
|
|---|
| 86 | function 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
|
|---|
| 92 | set -ex
|
|---|
| 93 | cd "\$(dirname \$0)/.."
|
|---|
| 94 | BRANCH="tested"
|
|---|
| 95 | function 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 |
|
|---|
| 105 | JAVA_OPTS="--add-modules java.scripting,java.sql,javafx.controls,javafx.media,javafx.swing,javafx.web \${JAVA_OPTS}"
|
|---|
| 106 | JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.base/sun.security.action=ALL-UNNAMED"
|
|---|
| 107 | JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.desktop/com.sun.imageio.plugins.jpeg=ALL-UNNAMED"
|
|---|
| 108 | JAVA_OPTS="\${JAVA_OPTS} --add-exports=java.desktop/com.sun.imageio.spi=ALL-UNNAMED"
|
|---|
| 109 |
|
|---|
| 110 | function 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 |
|
|---|
| 118 | update_josm
|
|---|
| 119 | while true; do
|
|---|
| 120 | start_josm
|
|---|
| 121 | if [ "z\$?" != "z9" ]; then
|
|---|
| 122 | break;
|
|---|
| 123 | fi
|
|---|
| 124 | echo ">> restarting josm..."
|
|---|
| 125 | done
|
|---|
| 126 | EOF
|
|---|
| 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 |
|
|---|
| 135 | if [ "$(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
|
|---|
| 148 | else
|
|---|
| 149 | echo "Mismatched versions"
|
|---|
| 150 | echo "${X86}" | jq
|
|---|
| 151 | echo "${AARCH64}" | jq
|
|---|
| 152 | exit 1
|
|---|
| 153 | fi
|
|---|