summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 74951517a9dd9e3f5bdc71e9c9ab3896ec150d7c (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
# Maintainer: Victor Bonnelle <victor.bonnelle@proton.me>
# Credits: Cedric Roijakkers <cedric [the at sign goes here] roijakkers [the dot sign goes here] be>.
# Inspired from the PKGBUILD for ferdi-git.

_pkgname='ferdium'
pkgname="ferdium-beta"
_electron='electron25'
pkgver=6.4.1.beta.2
pkgrel=1
pkgdesc='A messaging browser that allows you to combine your favorite messaging services into one application (git build from latest beta release).'
arch=('x86_64' 'i686' 'armv7h' 'aarch64')
url="https://ferdium.org/"
license=('Apache')
depends=('nss' 'atk' 'at-spi2-atk' 'libcups' 'libdrm' 'gdk-pixbuf2' 'gtk3' 'alsa-lib' 'c-ares' 'ffmpeg' 'libevent' 'libxkbfile' 'libxslt' 'minizip' 're2' 'snappy' "$_electron")
apptag='v6.4.1-beta.2'
makedepends=('nvm' 'git' 'python' 'libxcrypt-compat' 'asar')
provides=(
    'ferdium'
)
conflicts=(
    'ferdium'
    'ferdium-bin'
    'ferdium-nightly-bin'
    'ferdium-nightly'
    'ferdium-beta-bin'
    'ferdium-beta'
    'ferdium-git'
    'ferdium-electron'
)
source=(
	"$pkgname::git+https://github.com/ferdium/ferdium-app#tag=${apptag}"
)
sha512sums=('SKIP')

_sourcedirectory="$pkgname"

case "$CARCH" in
	i686)
		_electronbuilderarch='ia32'
	;;
	armv7h)
		_electronbuilderarch='armv7l'
	;;
	aarch64)
		_electronbuilderarch='arm64'
	;;
	*)
		_electronbuilderarch='x64'
	;;
esac

prepare() {
	# Due to patches and sed's, reset the code to upstream before starting
	cd "$srcdir/$_sourcedirectory/"
	git reset --hard "refs/tags/${apptag}"

	# Initialise the recipes submodule
	git submodule init
	cd "$srcdir/$_sourcedirectory/recipes"
	git reset --hard
	cd "$srcdir/$_sourcedirectory/"
	git submodule update --init --recursive

	# Specify path for autostart file
	if [ -f src/stores/AppStore.ts ]
	then
		sed -i -e "s#path: executablePath,#path: '/usr/bin/ferdium',#g" src/stores/AppStore.ts
	fi
	if [ -f src/stores/AppStore.js ]
	then
		sed -i -e "s#path: executablePath,#path: '/usr/bin/ferdium',#g" src/stores/AppStore.js
	fi
	# Set noUnusedLocals to false to avoid compilation error in AppStore.ts
	sed -i -e 's#"noUnusedLocals": true#"noUnusedLocals": false#g' tsconfig.json
}

pkgver() {
	cd "$srcdir/$_sourcedirectory/"
	printf "%s" "$(git describe --tags | sed 's/\([^-]*-\)g/r\1/;s/-/./g' | sed 's/^v//')"
}

build() {
	# Prepare recipes
	cd "$srcdir/$_sourcedirectory/"

	# Set environment variables needed for NodeJS/Electron
    export CI=true

    # Deactivate any pre-loaded nvm, and make sure we use our own in the current source directory
    which nvm >/dev/null 2>&1 && nvm deactivate && nvm unload
    export NVM_DIR="${srcdir}/.nvm"
    source /usr/share/nvm/init-nvm.sh || [[ $? != 1 ]]

	# Install the correct version of NodeJS (read from .nvmrc)
	nvm install $(cat .nvmrc)
    nvm use

    # Extract the correct versions of tools from the package.json file
    expected_node_version=$(node -p 'require("./package.json").engines.node')
    expected_npm_version=$(node -p 'require("./package.json").engines.npm')
    expected_pnpm_version=$(node -p 'require("./package.json").engines.pnpm')

    # If pnpm is not found in the package.json, get it from recipes/package.json (old style)
    if [[ "${expected_pnpm_version}" == "undefined" ]]
    then
    	expected_pnpm_version=$(node -p 'require("./recipes/package.json").engines.pnpm')
	fi

    # Cross-check if all versions of node match
    if [[ "$(node --version)" != "v${expected_node_version}" || "$(node --version)" != "v$(cat .nvmrc)" || "v${expected_node_version}" != "v$(cat .nvmrc)" ]]
    then
    	echo "Version mismatch in NodeJS! Version in .nvmrc is [v$(cat .nvmrc)], version in package.json is [v${expected_node_version}], and used version is ["$(node --version)"]."
    	exit 1
    fi

    # Empty the cache before building to avoid issues with packages not being found
    npm cache clean --force

	# Install the correct version of npm and/or pnpm, depending on the contents of package.json
	if [[ "${expected_npm_version}" != "undefined" ]]
	then
		npm i -gf npm@${expected_npm_version}
	fi
	if [[ "${expected_pnpm_version}" != "undefined" ]]
	then
		npm i -gf pnpm@${expected_pnpm_version}
	fi

	# This is useful if we move from 'npm' to 'pnpm' for the main repo as well
    if [[ -s 'pnpm-lock.yaml' ]]; then
		BASE_CMD=pnpm
    else
		BASE_CMD=npm
    fi

	# Build recipe archives, from here on switch to pnpm
	$BASE_CMD i
	$BASE_CMD run prepare-code || true # Ignore errors, because there is a false error causing the build to break

	# Run the package script
	pushd recipes
    pnpm i
    pnpm run package
    popd

	# Make sure the internal version of electron-builder is being used by prefixing it to the $PATH
	export PATH=./node_modules/.bin/:$PATH

	# When passing arguments downstream, npm needs an extra "--" while pnpm does not
	extra_cli_args=""
	if [[ "${BASE_CMD}" == "npm" ]]
	then
		extra_cli_args="--"
	fi

	# Run the electron build script,
	# passing parameters "-l dir" to only build for Linux and the dir target (which we will use later in function package() to make the Arch package),
	# and "--${_electronbuilderarch}" to only build for the current architecture, saving build time
	NODE_ENV='production' $BASE_CMD run build ${extra_cli_args} -l dir --${_electronbuilderarch} -c.electronDist="/usr/lib/$_electron" -c.electronVersion="$(cat "/usr/lib/$_electron/version" | sed -e 's/^v//')"
}

package() {
	cd "$srcdir/$_sourcedirectory/"

	# Determine where the unpacked output directory is located
	local _outpath='out/linux'
	if [ "$_electronbuilderarch" != 'x64' ]; then
		_outpath="$_outpath-$_electronbuilderarch"
	fi
	_outpath="$_outpath-unpacked"

	# Create a shell script to start Ferdium with Electron
	install -d -m755 "${pkgdir}/usr/bin/"
	cat > "$pkgdir/usr/bin/$_pkgname" <<EOF
    #!/bin/sh
ELECTRON_IS_DEV=0 exec /usr/bin/$_electron /opt/$pkgname/ "\$@"
EOF
	chmod +x "$pkgdir/usr/bin/ferdium"

	# Extract the asar package to the system
	install -d -m755 "${pkgdir}/opt/$pkgname/"
	asar e "$_outpath/resources/app.asar" "${pkgdir}/opt/$pkgname/"

    # Create a .desktop file
	install -dm755 "$pkgdir/usr/share/applications/"
	cat << EOF > "$pkgdir/usr/share/applications/$_pkgname.desktop"
[Desktop Entry]
Name=${_pkgname^}
Exec=/usr/bin/$_pkgname %U
Terminal=falseparu
Type=Application
Icon=ferdium
StartupWMClass=Ferdium
Comment=Ferdium is your messaging app / former heir to the throne of Austria-Hungary and combines chat & messaging services into one application. Ferdium currently supports Slack, WhatsApp, Gmail, Facebook Messenger, Telegram, Google Hangouts, GroupMe, Skype and many more. You can download Ferdium for free for Mac, Windows, and Linux.
MimeType=x-scheme-handler/ferdium;
Categories=Network;InstantMessaging;
EOF

	# Install the icons
	for _size in 16 24 32 48 64 96 128 256 512 1024; do
		install -Dm644 "build-helpers/images/icons/${_size}x${_size}.png" "$pkgdir/usr/share/icons/hicolor/${_size}x${_size}/apps/$_pkgname.png"
	done
}