# Maintainer: AlphaJack <alphajack at tuta dot io>

pkgname="librespeed-go"
pkgver=1.1.6
_commit=0f5f67cd4b174cc667ebf18710da2629a24f5dbd
pkgrel=13
pkgdesc="Go backend for LibreSpeed"
url="https://github.com/librespeed/speedtest-go"
license=("LGPL3")
arch=("i386" "x86_64" "aarch64" "armv5h" "armv6h" "armv7h")
conflicts=("librespeed-rs")
makedepends=("go>=1.16")
optdepends=("mariadb: alternative database to bolt"
            "postgresql: alternative database to bolt")
source=("git+https://gitea.balki.me/balki/speedtest-go.git#commit=${_commit}"
        "librespeed.ico"
        "librespeed.service"
        "librespeed.sysusers"
        "librespeed.tmpfiles")
b2sums=('727c92c6219cba373dd3daecea979c6e2a3bcb7c955323780617879156dd21713e750619a90cdd7115718df08933832c8d181ef75a9cfa8f03f100f6b14dda64'
        'd561280edc2fe3f7228ce3366132bd22e345e7574f21d92e6f188f701642fe98ffab8c8b12e807ad72fea15a6016605d6febd08c3389395828c47767779540bc'
        '6aa0ca421c7ee5ac2dd58df13c1a42a350186eb5a72bad7f83776b7a291b3032ae84e0789d0fafb8116f4371b73317b6bda476ea3f44f72337b236e7e2514761'
        'a7d3d6d4e7adb5b5d45e254fafad7c7180a54c1eec898657cbd8d457e218c746742067d7a024b81e2bf1fefb05a6ce3f7abebbdf233f70c99a34406edf047b6c'
        'f89c7cdee011d281dca7726a2b196e768423e8b3c52a1335b04b6740d83d2700acfd1ad248a3e0549a7e0ca8f996db635448be35fdf96889a3d714902ec95aae')
backup=("etc/librespeed/settings.toml" "usr/share/librespeed/assets/index.html")

prepare(){
 cd "speedtest-go"
 # use arch linux paths
 sed -i "settings.toml" \
     -e 's|^assets_path=.*|assets_path="/usr/share/librespeed/assets"|' \
     -e 's|database_file=.*|database_file="/var/lib/librespeed/speedtest.db"|'
 # remove "Example" from html pages
 sed -i "web/assets/"*.html \
     -e "s|LibreSpeed Example|LibreSpeed Speedtest|g"
}

build(){
 cd "speedtest-go"
 go build -ldflags "-w -s" -trimpath -o "speedtest" "main.go"
}

package(){
 cd "speedtest-go"
 # binary file
 install -D "speedtest" "$pkgdir/usr/bin/librespeed"
 # configuration
 install -D -m 640 "settings.toml" "$pkgdir/etc/librespeed/settings.toml"
 # database
 install -d -m 750 "$pkgdir/var/lib/librespeed"
 #touch "$pkgdir/var/lib/librespeed/speedtest.db"
 # webapp files
 install -d "$pkgdir/usr/share/librespeed"
 install -m 644 "database/mysql/telemetry_mysql.sql" "$pkgdir/usr/share/librespeed"
 install -m 644 "database/postgresql/telemetry_postgresql.sql" "$pkgdir/usr/share/librespeed"
 # html pages
 install -D -m 644 "$srcdir/librespeed.ico" "web/assets/favicon.ico"
 cp -r "web/assets" "$pkgdir/usr/share/librespeed"
 # systemd files
 install -D -m 644 "$srcdir/librespeed.service" "$pkgdir/usr/lib/systemd/system/librespeed.service"
 install -D -m 644 "$srcdir/librespeed.sysusers" "$pkgdir/usr/lib/sysusers.d/librespeed.conf"
 install -D -m 644 "$srcdir/librespeed.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/librespeed.conf"
}
