name='codex'
version=26.721.81911
release=1
summary='Unofficial Linux repack of the Codex desktop app'
summary_ru='Неофициальная Linux-перепаковка десктопного приложения Codex'
group='Development/Tools'
desc='codex-app-linux repacks the Codex desktop application into Linux Electron artifacts and bundles a Linux codex CLI binary for local workflows.'
desc_ru='codex-app-linux перепаковывает десктопное приложение Codex в Linux-артефакты Electron и включает Linux-бинарник codex CLI для локальных сценариев.'
homepage='https://github.com/Boria138/codex-app-linux'
maintainer='Boria138 <https://github.com/Boria138>'

architectures=('amd64')

license=('Custom')
nonfree=1
nonfree_msg='codex-app устанавливает неофициальную Linux-перепаковку проприетарного настольного приложения. Использование приложения и сетевых функций регулируется условиями исходного сервиса.'
nonfree_url='https://github.com/Boria138/codex-app-linux'

provides=()
replaces=('codex')
conflicts=()

auto_reqprov_method="dirty"
auto_req=0
auto_prov=0

deps=()
deps_debian=('ca-certificates' 'libc6' 'libasound2' 'libatk-bridge2.0-0' 'libatk1.0-0' 'libatspi2.0-0' 'libcairo2' 'libcups2' 'libcurl3-gnutls | libcurl4' 'libdbus-1-3' 'libexpat1' 'libgbm1' 'libglib2.0-0' 'libgtk-3-0' 'libnspr4' 'libnss3' 'libpango-1.0-0' 'libsecret-1-0' 'libudev1' 'libx11-6' 'libxcb1' 'libxcomposite1' 'libxdamage1' 'libxext6' 'libxfixes3' 'libxkbcommon0' 'libxkbfile1' 'libxrandr2' 'libxss1' 'libxtst6' 'xdg-desktop-portal' 'xdg-utils')
deps_ubuntu=("${deps_debian[@]}")
deps_fedora=('ca-certificates' 'glibc' 'alsa-lib' 'atk' 'at-spi2-atk' 'at-spi2-core' 'cairo' 'cups-libs' 'libcurl' 'dbus-libs' 'expat' 'mesa-libgbm' 'glib2' 'gtk3' 'nspr' 'nss' 'pango' 'libsecret' 'systemd-libs' 'libX11' 'libxcb' 'libXcomposite' 'libXdamage' 'libXext' 'libXfixes' 'libxkbcommon' 'libxkbfile' 'libXrandr' 'libXScrnSaver' 'libXtst' 'xdg-desktop-portal' 'xdg-utils')
deps_arch=('ca-certificates' 'glibc' 'alsa-lib' 'atk' 'at-spi2-atk' 'at-spi2-core' 'cairo' 'cups' 'curl' 'dbus' 'expat' 'libgbm' 'glib2' 'gtk3' 'nspr' 'nss' 'pango' 'libsecret' 'systemd-libs' 'libx11' 'libxcb' 'libxcomposite' 'libxdamage' 'libxext' 'libxfixes' 'libxkbcommon' 'libxkbfile' 'libxrandr' 'libxss' 'libxtst' 'xdg-desktop-portal' 'xdg-utils')
deps_opensuse=('ca-certificates' 'glibc' 'alsa' 'libatk-1_0-0' 'libatk-bridge-2_0-0' 'libatspi0' 'libcairo2' 'cups-libs' 'libcurl4' 'libdbus-1-3' 'libexpat1' 'libgbm1' 'glib2' 'gtk3' 'mozilla-nspr' 'mozilla-nss' 'pango' 'libsecret-1-0' 'libudev1' 'libX11-6' 'libxcb1' 'libXcomposite1' 'libXdamage1' 'libXext6' 'libXfixes3' 'libxkbcommon0' 'libxkbfile1' 'libXrandr2' 'libXss1' 'libXtst6' 'xdg-desktop-portal' 'xdg-utils')
deps_altlinux=('ca-certificates' 'glibc' 'libalsa' 'at-spi2-atk' 'libat-spi2-core' 'libatk' 'libcairo' 'libcups' 'libcurl' 'libdbus' 'libexpat' 'libgbm' 'glib2' 'libgtk+3' 'libnspr' 'libnss' 'libpango' 'libsecret' 'libudev0' 'libX11' 'libxcb' 'libXcomposite' 'libXdamage' 'libXext' 'libXfixes' 'libxkbcommon' 'libxkbfile' 'libXrandr' 'libXScrnSaver' 'libXtst' 'xdg-desktop-portal' 'xdg-utils')

build_deps=('binutils')
build_deps_debian=('binutils' 'build-essential' 'cargo' 'libwayland-dev' 'nodejs' 'pkg-config' 'python3' 'rustc')
build_deps_ubuntu=("${build_deps_debian[@]}")
build_deps_fedora=('binutils' 'cargo' 'gcc' 'gcc-c++' 'make' 'nodejs' 'pkgconf-pkg-config' 'python3' 'rust' 'wayland-devel')
build_deps_arch=('base-devel' 'binutils' 'cargo' 'nodejs' 'pkgconf' 'python' 'rust' 'wayland')
build_deps_opensuse=('binutils' 'cargo' 'gcc' 'gcc-c++' 'make' 'nodejs' 'pkg-config' 'python3' 'rust' 'wayland-devel')
build_deps_altlinux=('binutils' 'gcc-c++' 'libwayland-client-devel' 'make' 'node' 'pkg-config' 'python3' 'rust' 'rust-cargo')

sources=(
	"https://github.com/Boria138/codex-app-linux/releases/download/${version}/codex-app-${version}-x86_64.tar.gz?~archive=false&~name=codex-app.tar.gz"
	'https://github.com/ilysenko/codex-desktop-linux/archive/15f3bd4fabc44bc0afba85b16d0121f0b4abd052.tar.gz?~archive=false&~name=codex-linux-port.tar.gz'
	'https://github.com/agent-sh/computer-use-linux/archive/refs/tags/v0.4.1.tar.gz?~archive=false&~name=computer-use-linux.tar.gz'
	'local:///asar_tree.py'
	'local:///patch-computer-use.js'
	"local:///codex-app.png"
	'local:///LICENSE'
)

checksums=(
	'sha256:87798af5884c55a1faab57c8fef818a827a9f0243241c94d19a467d8216aef31'
	'sha256:8cde2dac642947cdfab23d302056ef81b574cc7f5684f25f7116f80411ec551c'
	'sha256:fab36489be4f1e92591d2ebcd28a96a95f788b5d1ba87824985623ab7df5e3f4'
	'sha256:9259dc4b9ae217855d55414b2266db08d9b40c5b0cdbca20a183234ff55ba006'
	'sha256:91b3cc2a6b660e3962f24976a29a261dae9a38f879758e6e6463e1295477e172'
	'sha256:5c4fe03d5777a9684e37ec9a2331302abf525f53db83762e6adaafd2fc5eb510'
	'sha256:755f407295f07facd26c968b73a204db5a6f25deac890d230062b09c33eb395e'
)

scripts=(
	['postinstall']='postinstall.sh'
	['postremove']='postremove.sh'
)

package() {
	cd "${srcdir}"

	tar -xzf codex-app.tar.gz
	tar -xzf codex-linux-port.tar.gz
	tar -xzf computer-use-linux.tar.gz
	src="$(find "${srcdir}" -maxdepth 1 -type d -name "codex-app-${version}-x64" | head -1)"
	linux_port_src="$(find "${srcdir}" -maxdepth 1 -type d -name 'codex-desktop-linux-*' | head -1)"
	computer_use_src="$(find "${srcdir}" -maxdepth 1 -type d -name 'computer-use-linux-*' | head -1)"

	cargo build \
		--manifest-path "${computer_use_src}/Cargo.toml" \
		--target-dir "${srcdir}/computer-use-target" \
		--release \
		--locked

	install -dm755 "${pkgdir}/opt/codex-app"
	cp -a "${src}/." "${pkgdir}/opt/codex-app/"

	codex_asar="${pkgdir}/opt/codex-app/resources/codex.asar"
	extracted_asar="${srcdir}/codex-asar"
	python3 "${srcdir}/asar_tree.py" extract "${codex_asar}" "${extracted_asar}"
	node "${srcdir}/patch-computer-use.js" "${linux_port_src}" "${extracted_asar}"
	python3 "${srcdir}/asar_tree.py" rebuild "${codex_asar}" "${extracted_asar}"

	bundled_root="${pkgdir}/opt/codex-app/resources/plugins/openai-bundled"
	plugin_dir="${bundled_root}/plugins/computer-use"
	install -dm755 "${plugin_dir}"
	cp -a \
		"${linux_port_src}/plugins/openai-bundled/plugins/computer-use/." \
		"${plugin_dir}/"
	find "${plugin_dir}" -name '.gitkeep' -delete
	install -Dm755 \
		"${srcdir}/computer-use-target/release/computer-use-linux" \
		"${plugin_dir}/bin/codex-computer-use-linux"
	install -Dm755 \
		"${srcdir}/computer-use-target/release/computer-use-linux-cosmic" \
		"${plugin_dir}/bin/codex-computer-use-cosmic"
	install -Dm755 \
		"${srcdir}/computer-use-target/release/computer-use-linux-cosmic" \
		"${plugin_dir}/bin/computer-use-linux-cosmic"
	install -Dm644 codex-app.png "${plugin_dir}/assets/app-icon.png"

	install -Dm644 /dev/stdin \
		"${bundled_root}/.agents/plugins/marketplace.json" <<'EOF'
{
  "plugins": [
    {
      "name": "computer-use",
      "source": {
        "source": "local",
        "path": "./plugins/computer-use"
      },
      "policy": {
        "installation": "AVAILABLE",
        "authentication": "ON_INSTALL"
      },
      "category": "Productivity"
    }
  ]
}
EOF

	chmod 755 "${pkgdir}/opt/codex-app/codex-app"
	chmod 755 "${pkgdir}/opt/codex-app/resources/codex"
	if [[ -f "${pkgdir}/opt/codex-app/chrome-sandbox" ]]; then
		chmod 0755 "${pkgdir}/opt/codex-app/chrome-sandbox"
	fi

	install -dm755 "${pkgdir}/usr/bin"
	ln -sfn /opt/codex-app/codex-app "${pkgdir}/usr/bin/codex-app"
	ln -sfn \
		/opt/codex-app/resources/plugins/openai-bundled/plugins/computer-use/bin/codex-computer-use-linux \
		"${pkgdir}/usr/bin/codex-computer-use-linux"

	install -Dm644 codex-app.png \
		"${pkgdir}/usr/share/icons/hicolor/512x512/apps/codex-app.png"

	install -Dm644 "${src}/LICENSE.electron.txt" \
		"${pkgdir}/usr/share/licenses/codex/LICENSE.electron.txt"
	install -Dm644 "${src}/LICENSES.chromium.html" \
		"${pkgdir}/usr/share/licenses/codex/LICENSES.chromium.html"
	install -Dm644 "${linux_port_src}/LICENSE" \
		"${pkgdir}/usr/share/licenses/codex/LICENSE.codex-desktop-linux"
	install -Dm644 "${computer_use_src}/LICENSE" \
		"${pkgdir}/usr/share/licenses/codex/LICENSE.computer-use-linux"
	install-license "${srcdir}/LICENSE" "codex/LICENSE.nivora"

	install -Dm644 /dev/stdin "${pkgdir}/usr/share/applications/codex-app.desktop" <<'EOF'
[Desktop Entry]
Name=Codex
Comment=Desktop coding agent client
Comment[ru]=Десктопный клиент coding agent
Exec=/usr/bin/codex-app %U
Terminal=false
Type=Application
Icon=codex-app
Categories=Development;IDE;
StartupWMClass=codex-app
EOF
}

files() {
	files-find "/opt/codex-app/**/*"
	files-find-binary
	files-find-desktop
	files-find-license
	files-find "/usr/share/icons/hicolor/*/apps/codex-app.png"
}
