# Obarun        : 66 init/supervisor
# Maintainer    : Eric Vidal <eric@obarun.org>
# Maintainer    : Jean-Michel T.Dydak <jean-michel@obarun.org>
# Maintainer    : YianIris <yianiris At disroot Dot org>
# Obarun PkgSrc : https://git.obarun.org/pkg/obextra/mariadb
#----------------
#--------------------------------------------------------------
# DESCRIPTION ]

pkgbase=mariadb
pkgver=10.6.5
pkgrel=3
pkgdesc="Fast SQL database server, derived from MySQL"
url='https://mariadb.org/'

pkgname=(
    'mariadb-libs'
    'mariadb-clients'
    'mariadb'
    'mytop'
)

track="$pkgbase/$pkgbase-$pkgver/source"
target="$pkgbase-$pkgver"
source=(
    "https://rsync.osuosl.org/pub/${track}/${target}.tar.gz"{,.asc}
    0001-arch-specific.patch
    mariadb.sysusers.conf
    mariadb.tmpfile.conf
)

#----------------------
# BUILD CONFIGURATION ]

makedepends=(
    'boost'
    'bzip2'
    'cmake'
    'jemalloc'
    'libaio'
    'libxcrypt'
    'libxml2'
    'lz4'
    'lzo'
    'openssl'
    'zlib'
    'zstd'
    'curl'
    'krb5'
    'cracklib'
    'judy'
)

#----------------
# BUILD PREPARE ]

prepare() {
    cd $pkgbase-$pkgver

    ## Arch Linux specific patches:
    ##  * enable PrivateTmp for a little bit more security
    ##  * force preloading jemalloc for memory management
    ##  * make systemd-tmpfiles create MYSQL_DATADIR
    patch -Np1 < ../0001-arch-specific.patch
}

#--------
# BUILD ]

build() {
    local _cmake_options=(

        ## build options
        -DCOMPILATION_COMMENT="Obarun Linux"
        -DCMAKE_BUILD_TYPE=RelWithDebInfo
        -Wno-dev

        ## file paths
        ## /etc
        -DINSTALL_SYSCONFDIR=/etc
        -DINSTALL_SYSCONF2DIR=/etc/my.cnf.d

        ## /run
        -DINSTALL_UNIX_ADDRDIR=/run/mysqld/mysqld.sock

        ## /usr
        -DCMAKE_INSTALL_PREFIX=/usr

        ## /usr/bin /usr/include
        -DINSTALL_SCRIPTDIR=bin
        -DINSTALL_INCLUDEDIR=include/mysql

        ## /usr/lib
        -DINSTALL_PLUGINDIR=lib/mysql/plugin

        ## /usr/share
        -DINSTALL_SHAREDIR=share
        -DINSTALL_SUPPORTFILESDIR=share/mysql
        -DINSTALL_MYSQLSHAREDIR=share/mysql
        -DINSTALL_DOCREADMEDIR=share/doc/mariadb
        -DINSTALL_DOCDIR=share/doc/mariadb
        -DINSTALL_MANDIR=share/man

        ## /var
        -DMYSQL_DATADIR=/var/lib/mysql

        # default settings
        -DDEFAULT_CHARSET=utf8mb4
        -DDEFAULT_COLLATION=utf8mb4_unicode_ci

        ## features
        -DENABLED_LOCAL_INFILE=ON
        -DPLUGIN_EXAMPLE=NO
        -DPLUGIN_FEDERATED=NO
        -DPLUGIN_FEEDBACK=NO
        -DWITH_EMBEDDED_SERVER=ON
        -DWITH_EXTRA_CHARSETS=complex
        -DWITH_JEMALLOC=ON
        -DWITH_LIBWRAP=OFF
        -DWITH_PCRE=bundled
        -DWITH_READLINE=ON
        -DWITH_SSL=system
        -DWITH_SYSTEMD=no
        -DWITH_UNIT_TESTS=OFF
        -DWITH_ZLIB=system
        -DPLUGIN_TOKUDB=NO
    )

    mkdir build
    cd build

    cmake ../"$pkgbase-$pkgver" "${_cmake_options[@]}"
    make
}

#--------
# CHECK ]

check() {
    cd build/mysql-test
    ## Takes *really* long, so disabled by default.
    ##./mtr --parallel=5 --mem --force --max-test-fail=0
}

#----------
# PACKAGE ]

package_mariadb-libs() {
    pkgdesc="MariaDB libraries"
    depends=(
        'bzip2'
        'libaio'
        'libxcrypt'
        'libcrypt.so'
        'lz4'
        'lzo'
        'openssl'
        'xz'
        'zlib'
    )
    optdepends=(
        'krb5: for gssapi authentication'
    )
    conflicts=(
        'libmysqlclient'
        'libmariadbclient'
        'mariadb-connector-c'
    )
    provides=(
        'libmariadbclient'
        'mariadb-connector-c'
        'libmariadb.so'
        'libmariadbd.so'
    )
    replaces=(
        'libmariadbclient'
    )

    cd build

    for dir in libmariadb libmysqld libservices include; do
     make -C "$dir" DESTDIR="$pkgdir" install
    done

    ln -s mariadb_config "$pkgdir"/usr/bin/mysql_config
    install -D -m0644 "$srcdir"/"$pkgbase-$pkgver"/man/mysql_config.1 "$pkgdir"/usr/share/man/man1/mysql_config.1

    install -D -m0644 support-files/mariadb.pc "$pkgdir"/usr/share/pkgconfig/mariadb.pc
    install -D -m0644 "$srcdir"/"$pkgbase-$pkgver"/support-files/mysql.m4 "$pkgdir"/usr/share/aclocal/mysql.m4

    cd "$pkgdir"

    ## remove static libraries
    rm usr/lib/*.a

    ## remove man pages
    rm -r usr/share/man
}

package_mariadb-clients() {
    pkgdesc="MariaDB client tools"
    depends=(
        "mariadb-libs=$pkgver"
        'jemalloc'
    )
    conflicts=(
        'mysql-clients'
    )
    provides=(
        "mysql-clients=$pkgver"
    )

    cd build

    make -C client DESTDIR="$pkgdir" install

    ## install man pages
    for man in mysql mysql_plugin mysql_upgrade mysqladmin mysqlbinlog mysqlcheck mysqldump mysqlimport mysqlshow mysqlslap mysqltest; do
     install -D -m0644 "$srcdir"/"$pkgbase-$pkgver"/man/"$man.1" "$pkgdir"/usr/share/man/man1/"$man.1"
    done
}

package_mariadb() {
    pkgdesc='Fast SQL database server, derived from MySQL'
    backup=(
        'etc/my.cnf'
        'etc/my.cnf.d/client.cnf'
        'etc/my.cnf.d/enable_encryption.preset'
        'etc/my.cnf.d/mysql-clients.cnf'
        'etc/my.cnf.d/server.cnf'
        'etc/my.cnf.d/s3.cnf'
        'etc/my.cnf.d/spider.cnf'
        'etc/security/user_map.conf'
    )
    depends=(
        "mariadb-clients=$pkgver"
        'libxml2'
        'zstd'
    )
    optdepends=(
        'cracklib: for cracklib plugin'
        'curl: for ha_s3 plugin'
        'galera: for MariaDB cluster with Galera WSREP'
        'judy: for Open Query GRAPH (OQGraph) computation engine'
        'mysql-python: for myrocks_hotbackup'
        'perl-dbd-mariadb: for mariadb-hotcopy, mariadb-convert-table-format and mariadb-setpermission'
    )
    conflicts=(
        'mysql'
    )
    provides=(
        "mysql=$pkgver"
    )
    options=(
        'emptydirs'
    )
    install=mariadb.install

    cd build

    make DESTDIR="$pkgdir" install

    cd "$pkgdir"

    ## no SysV init, please!
    rm -r etc/logrotate.d
    rm usr/bin/rcmysql
    rm usr/share/mysql/{binary-configure,mysql{,d_multi}.server}

    install -Dm0644 "$srcdir"/mariadb.tmpfile.conf "$pkgdir"/usr/lib/tmpfiles.d/mariadb.conf
    install -Dm0644 "$srcdir"/mariadb.sysusers.conf "$pkgdir"/usr/lib/sysusers.d/mariadb.conf

    ## move to proper licenses directories
    install -d usr/share/licenses/mariadb
    mv usr/share/doc/mariadb/COPYING* usr/share/licenses/mariadb/

    ## move it where one might look for it
    mv usr/share/{groonga{,-normalizer-mysql},doc/mariadb/}

    # move to pam directories
    install -d {etc,usr/lib}/security
    mv usr/share/user_map.conf etc/security/
    mv usr/share/pam_user_map.so usr/lib/security/

    # provided by mariadb-libs
    rm usr/bin/mariadb_config
    rm usr/bin/mysql_config
    rm -r usr/include/
    rm usr/share/man/man1/mysql_config.1
    rm -r usr/share/aclocal
    rm usr/lib/lib*
    rm -r usr/lib/pkgconfig
    rm usr/lib/mysql/plugin/{auth_gssapi_client,caching_sha2_password,client_ed25519,dialog,mysql_clear_password,sha256_password}.so

    # provided by mariadb-clients
    rm usr/bin/mysql{,_plugin,_upgrade,admin,binlog,check,dump,import,show,slap,test}
    rm usr/bin/mariadb{,-{admin,binlog,check,conv,dump,import,plugin,show,slap,test,upgrade}}
    rm usr/share/man/man1/mysql{,_plugin,_upgrade,admin,binlog,check,dump,import,show,slap,test}.1

    # provided by mytop
    rm usr/bin/mytop

    # not needed
    rm -r usr/{mysql-test,sql-bench}
    rm usr/share/man/man1/mysql-test-run.pl.1
}

package_mytop() {
    pkgdesc="Top clone for MariaDB"
    depends=(
        'perl'
        'perl-dbd-mariadb'
        'perl-term-readkey'
    )

    cd build

    install -D -m0755 scripts/mytop "$pkgdir"/usr/bin/mytop
}

#--------------------
# ARCH LICENSE AUTH ]

arch=(x86_64)
license=(GPL)

validpgpkeys=(
    '199369E5404BD5FC7D2FE43BCBCB082A1BB943DB' # MariaDB Pkg Signing Key <package-signing-key@mariadb.org>
)

sha512sums=('')
