# Maintainer : Valeria Fadeeva <valeria@fadeeva.me>
# Calamares installer QT6 KF6 configured for Melawy Linux

source_name="calamares"
ver="3.3.8"

_gitname="$(basename $(pwd))"
pkgbase="melawy-calamares"
release_name="melawy-calamares"
pkgver=3.3.8
pkgrel=1
pkgdesc="Calamares installer for Melawy Linux"
arch=('x86_64')
url="https://git.melawy.ru/Installer/$_gitname"
license=('LGPL')
options=('!strip' '!emptydirs' 'debug' 'staticlibs')
sha256sums=('SKIP' 'SKIP')
#source=("git+$url.git#branch=$source_name")
source=("https://github.com/$source_name/$source_name/releases/download/v$ver/$source_name-$ver.tar.gz"
"git+https://git.melawy.ru/Installer/melawy-calamares.git"
)


depends=(
'boost-libs'
'ckbcomp'
'cryptsetup'
'dmidecode'
'doxygen'
'gptfdisk'
'hwinfo'
'kconfig'
'kcoreaddons'
'kcrash'
'kdbusaddons'
'ki18n'
'kpackage'
'kparts'
'kpmcore'
'kservice'
'kwidgetsaddons'
'libatasmart'
'libpwquality'
'melawy-calamares-config'
'networkmanager'
'parted'
'polkit-qt6'
'python'
'qt5-xmlpatterns'
'qt6-5compat'
'qt6-declarative'
'qt6-location'
'qt6-svg'
'qt6-tools'
'qt6-virtualkeyboard'
'qt6-webengine'
'rsync'
'solid'
'squashfs-tools'
'udisks2'
'upower'
'yaml-cpp'
)

makedepends=(
'boost'
'clang'
'cmake'
'extra-cmake-modules'
'gawk'
'git'
'kdbusaddons'
'kpmcore'
'ninja'
'python-jsonschema'
'python-pyaml'
'python-unidecode'
'qt6-tools'
)

pkgver() {
        #cd "${srcdir}/$source_name-$ver"
        printf "%s" "$ver"
}

prepare() {
    cp -vrf "${srcdir}/melawy-calamares/." "${srcdir}/$source_name-$ver/."

    # Update branding.desc with the proper values
    replace_command='
    {
        gsub(/\${version}/,version);
        gsub(/\${release_name}/,release);
        print
    }
    '
    awk -i inplace -v version="$(pkgver)" -v release="${release_name}" "$replace_command" "${srcdir}/$source_name-$ver/src/branding/melawy/branding.desc"
}

build() {
    _cpuCount=$(grep -c -w ^processor /proc/cpuinfo)

    cmake -B build -S "${srcdir}/$source_name-$ver" \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DWITH_APPSTREAM=OFF \
    -DWITH_PYBIND11=ON \
    -DWITH_QT6=ON \
    -DSKIP_MODULES="contextualprocess \
dracut \
dracutlukscfg \
dummycpp \
dummyprocess \
dummypython \
finishedq \
fsresizer \
hostinfo \
initcpio \
initcpiocfg \
initramfs \
initramfscfg \
interactiveterminal \
keyboardq \
license \
localeq \
luksopenswaphookcfg \
mkinitfs \
notesqml \
oemid \
openrcdmcryptcfg \
packagechooser \
plasmalnf \
plymouthcfg \
rawfs \
services-openrc \
summaryq \
tracking \
usersq \
welcomeq \
zfs \
zfshostid"

    export DESTDIR="$srcdir/build"
    cmake --build build --parallel $_cpuCount
}

_package() {
    pkgdesc="Calamares installer for Melawy Linux"

    provides=("calamares")
    conflicts=('calamares')

    DESTDIR="${pkgdir}" cmake --build "${DESTDIR}" --target install

    install -dm 750 "${pkgdir}/etc/polkit-1/rules.d"
    install -Dm644 "${srcdir}/$source_name-$ver/49-nopasswd-calamares.rules" "${pkgdir}/etc/polkit-1/rules.d/49-nopasswd-calamares.rules"

    install -dm 755 "${pkgdir}/usr/bin"
    install -Dm755 "${srcdir}/$source_name-$ver/calamares_polkit" "${pkgdir}/usr/bin/calamares_polkit"
}

_package-config() {
    pkgdesc="Calamares installer configs for Melawy Linux"
    depends=()

    provides=('calamares-config' 'melawy-calamares-config')
    conflicts=('calamares-config' 'calamares-configs')

    install -dm 755 "${pkgdir}/etc/calamares"

    install -dm 755 "${pkgdir}/etc/calamares/branding"
    cp -rf ${srcdir}/$source_name-$ver/src/branding/melawy "$pkgdir/etc/calamares/branding"

    cp -f ${srcdir}/$source_name-$ver/settings_offline.conf "$pkgdir/etc/calamares/settings_offline.conf"
    cp -f ${srcdir}/$source_name-$ver/settings_online.conf "$pkgdir/etc/calamares/settings_online.conf"
    cp -f ${srcdir}/$source_name-$ver/settings_online.conf "$pkgdir/etc/calamares/settings.conf"

    cp -rf ${srcdir}/$source_name-$ver/src/scripts "$pkgdir/etc/calamares"
#     cp -rf ${srcdir}/$source_name-$ver/src/images "$pkgdir/etc/calamares"
#     cp -rf ${srcdir}/$source_name-$ver/src/files "$pkgdir/etc/calamares"

    install -dm 755 "${pkgdir}/etc/calamares/modules"
    cp -rf ${srcdir}/$source_name-$ver/src/modules/*/*.conf "$pkgdir/etc/calamares/modules"

    cp -f "${srcdir}/$source_name-$ver/src/modules/netinstall/netinstall.yaml" "$pkgdir/etc/calamares/modules/netinstall.yaml"
}

pkgname=("${pkgbase}" "${pkgbase}-config")
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done
