#!/usr/bin/env bash

# SPDX-License-Identifier: MIT

set -euo pipefail

repo_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
tool_dir="$repo_root/plasma-task-group-shortcuts"
topdir="$repo_root/rpmbuild"
source_dir="$topdir/SOURCES/plasma-task-group-shortcuts-0.1.0"

mkdir -p "$topdir"/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
mkdir -p "$source_dir/tests"
install -m 0644 "$tool_dir/CMakeLists.txt" "$source_dir/CMakeLists.txt"
install -m 0644 "$tool_dir/keyboardlayout.cpp" "$source_dir/keyboardlayout.cpp"
install -m 0644 "$tool_dir/keyboardlayout.h" "$source_dir/keyboardlayout.h"
install -m 0644 "$tool_dir/main.cpp" "$source_dir/main.cpp"
install -m 0644 "$tool_dir/taskselection.cpp" "$source_dir/taskselection.cpp"
install -m 0644 "$tool_dir/taskselection.h" "$source_dir/taskselection.h"
install -m 0644 "$tool_dir/tests/test-taskselection.cpp" \
    "$source_dir/tests/test-taskselection.cpp"
install -m 0644 "$tool_dir/tests/test-keyboardlayout.cpp" \
    "$source_dir/tests/test-keyboardlayout.cpp"
install -m 0644 "$tool_dir/se.ajpanton.plasma-task-group-shortcuts.desktop" \
    "$source_dir/se.ajpanton.plasma-task-group-shortcuts.desktop"
install -m 0644 "$tool_dir/se.ajpanton.plasma-task-group-shortcuts-autostart.desktop" \
    "$source_dir/se.ajpanton.plasma-task-group-shortcuts-autostart.desktop"
tar -C "$topdir/SOURCES" -czf \
    "$topdir/SOURCES/plasma-task-group-shortcuts-0.1.0.tar.gz" \
    plasma-task-group-shortcuts-0.1.0
install -m 0644 "$repo_root/LICENSE" "$topdir/SOURCES/LICENSE"
install -m 0644 "$repo_root/README.md" "$topdir/SOURCES/README.md"

rpmbuild \
    --define "_topdir $topdir" \
    -bb "$tool_dir/plasma-task-group-shortcuts.spec"
