# Maintainer: BGME <i@bgme.me>
# Contributor: everyx <lunt.luo@gmail.com>

pkgname=sing-box-git
_pkgname=sing-box
pkgver=r1217.cca9c067
pkgrel=2

pkgdesc='The universal proxy platform (git version).'
arch=('x86_64' 'i686')
url='https://sing-box.sagernet.org/'
license=('GPL3')

provides=("${_pkgname}")
conflicts=("sing-box" "sing-box-beta")

makedepends=('go' 'git' 'patch')
optdepends=(
    'sing-geosite:  sing-geosite database'
    'sing-geoip:    sing-geoip database'
)

backup=("etc/${_pkgname}/config.json")

source=(
    "${_pkgname}::git+https://github.com/SagerNet/sing-box.git#branch=dev-next"
    "sing-box.sysusers"
    "sing-box.tmpfiles"
    "0001-sing-box-service.patch"
)
sha256sums=(
    'SKIP'
    '35b9966604f3e5cf2acf9b86a7a1826b73829d59e04f283acaf89e84d9f4f4f7'
    '83f27690c2b37823b4de56d890462ac32118c6984554e32b4f6d25b93426a94f'
    'b7a21a59ce2ec82fdded6b8a9e337c4f43e85a0688ad4a26f0f601aefa44dc54'
)

pkgver() {
    cd "${srcdir}/${_pkgname}"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

_tags=with_gvisor,with_quic,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api
build() {
    cd "${srcdir}/${_pkgname}"

    export COMMIT=$(git rev-parse --short HEAD)
    export VERSION=$(go run ./cmd/internal/read_tag)

    export CGO_CPPFLAGS="$CPPFLAGS"
    export CGO_CFLAGS="$CFLAGS"
    export CGO_CXXFLAGS="$CXXFLAGS"
    export CGO_LDFLAGS="$LDFLAGS"

    go build \
        -v \
        -trimpath \
        -buildmode=pie \
        -mod=readonly \
        -modcacherw \
        -tags "$_tags" \
        -ldflags "
            -X \"github.com/sagernet/sing-box/constant.Version=$VERSION\"
            -s -w -buildid= 
            -linkmode=external" \
        ./cmd/sing-box

    mkdir completions
    go run ./cmd/sing-box completion bash   > completions/bash
    go run ./cmd/sing-box completion fish   > completions/fish
    go run ./cmd/sing-box completion zsh    > completions/zsh

    cd release/config/
    patch -p1 <"${srcdir}/0001-sing-box-service.patch"
}

package() {
    cd "${srcdir}/${_pkgname}"

    install -Dm644 LICENSE -t "${pkgdir}/usr/share/licenses/${_pkgname}"
    install -Dm755 "${_pkgname}" -t "${pkgdir}/usr/bin"
    install -Dm644 "release/config/config.json" -t "${pkgdir}/etc/${_pkgname}"
    install -Dm644 "release/config/${_pkgname}.service" -t "${pkgdir}/usr/lib/systemd/system"
    install -Dm644 "release/config/${_pkgname}@.service" -t "${pkgdir}/usr/lib/systemd/system"

    install -Dm644 completions/bash "${pkgdir}/usr/share/bash-completion/completions/${_pkgname}"
    install -Dm644 completions/fish "${pkgdir}/usr/share/fish/vendor_completions.d/${_pkgname}.fish"
    install -Dm644 completions/zsh  "${pkgdir}/usr/share/zsh/site-functions/_${_pkgname}"

    install -Dm644 "${srcdir}/sing-box.sysusers" "${pkgdir}/usr/lib/sysusers.d/${_pkgname}.conf"
    install -Dm644 "${srcdir}/sing-box.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/${_pkgname}.conf"
}