# Maintainer (aur): Maxime Poulin <maxpoulin64@gmail.com>
# Maintainer (aur): Felix Golatofski <contact@xdfr.de>
# Contributor: Damian Nowak <damian.nowak@atlashost.eu>
# Contributor: Kyle Fuller <inbox@kylefuller.co.uk>
# Contributor: jibi <jibi@paranoici.org>
# Maintainer: Wael Karram <wael@waelk.tech>


pkgname='inspircd'
pkgver='3.17.0'
pkgrel='1'
pkgdesc='InspIRCd is a modular Internet Relay Chat (IRC) server written in C++ for Linux, BSD, Windows and macOS systems.'
arch=('x86_64' 'aarch64')
arch+=('armv7h' 'i686')
url='https://www.inspircd.org/'
license=('GPL2')
conflicts=('inspircd')
provides=('inspircd')
depends=('perl')
makedepends=('libmariadbclient' 'sqlite3' 'libldap' 'geoip' 'libmaxminddb'
    'gnutls' 'openssl' 'libgcrypt' 'postgresql-client'
    're2' 'tre' 'mbedtls' 'pkg-config')
optdepends=('gnutls: m_ssl_gnutls'
    'libgcrypt: m_ssl_gnutls'
    'openssl: m_ssl_openssl'
    'libmariadbclient: m_mysql'
    'postgresql-client: m_pgsql'
    'libmaxminddb: m_geo_maxmind'
    'pcre: m_regex_pcre'
    'sqlite3: m_sqlite3'
    'libldap: m_ldap'
    'geoip: m_geoip'
    're2: m_regex_re2'
    'tre: m_regex_tre'
    'mbedtls: m_ssl_mbedtls')
install='inspircd.install'
source=("https://github.com/inspircd/inspircd/archive/v$pkgver.tar.gz"
    "$pkgname.service"
    "$pkgname.sysusers"
    )
sha256sums=('181de90130e11a26ec107fcb6b74005cbce3051b89b500347e416054e29c3166'
            'b59bbb46ed411ad4f39f04539ea3957f3d029c4a2f8994769eb775b17a804351'
            'cab41d386b4207e967b8fc5ab6859e20b342fd91c7e55a20eac856b8ab6cdf8c')

prepare() {
    cd "${srcdir}/inspircd-${pkgver}"
    sed -i -e s/-lldap_r/-lldap/g src/modules/extra/m_ldap.cpp
}

build() {
    cd "${srcdir}/inspircd-${pkgver}"

    ./configure \
    --enable-extras=m_geo_maxmind.cpp \
    --enable-extras=m_ldap.cpp \
    --enable-extras=m_mysql.cpp \
    --enable-extras=m_pgsql.cpp \
    --enable-extras=m_regex_posix.cpp \
    --enable-extras=m_regex_stdlib.cpp \
    --enable-extras=m_regex_re2.cpp \
    --enable-extras=m_regex_tre.cpp \
    --enable-extras=m_sqlite3.cpp \
    --enable-extras=m_ssl_gnutls.cpp \
    --enable-extras=m_ssl_openssl.cpp \
    --enable-extras=m_ssl_mbedtls.cpp \
    --enable-extras=m_sslrehashsignal.cpp

    ./configure \
    --uid=0 \
    --gid=0 \
    --prefix=/usr/lib/inspircd \
    --binary-dir=/usr/bin \
    --module-dir=/usr/lib/inspircd/modules \
    --config-dir=/etc/inspircd \
    --data-dir=/var/lib/inspircd \
    --log-dir=/var/log/inspircd \
    --distribution-label=archlinux
    make
}

package() {
    install -Dm644 "${srcdir}/$pkgname.service" "${pkgdir}"/usr/lib/systemd/system/inspircd.service
    install -Dm644 "${srcdir}/$pkgname.sysusers" "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
    install -dm750 "${pkgdir}/var/log/inspircd" "${pkgdir}/var/lib/inspircd"

    cd "${srcdir}/inspircd-${pkgver}"
    make DESTDIR="$pkgdir" install

    mkdir -p "${pkgdir}"/usr/share/inspircd
    mv "${pkgdir}"/etc/inspircd/examples "${pkgdir}"/usr/share/inspircd/examples

    rm -rf "${pkgdir}"/usr/lib/inspircd/logs
    rm -rf "${pkgdir}"/usr/lib/inspircd/data
}
