#!/usr/bin/env bash

# Script to generate fake Arch Linux packages files and fake http response.

set -euo pipefail

# Please note that you need to install Zstandard compression tool (zstd) to compress
# to .zst archive and Xz utils (xz) to compress to .xz archive.
command -v zstd || echo "you should install 'zstd' to run this script"
command -v xz || echo "you should install 'xz' to run this script"

# files and directories
mkdir https_archive.archlinux.org
mkdir https_uk.mirror.archlinuxarm.org
mkdir -p tmp_dir/arch/

cd tmp_dir/arch/

mkdir 'packages_d_dialog_dialog-1:1.3_20190211-1-x86_64'
mkdir 'packages_d_dialog_dialog-1:1.3_20220414-1-x86_64'

echo -e '''pkgname = dialog
pkgbase = dialog
pkgver = 1:1.3_20190211-1
pkgdesc = A tool to display dialog boxes from shell scripts
url = https://invisible-island.net/dialog/
builddate = 1550046926
packager = Evangelos Foutras <evangelos@foutrelis.com>
size = 455680
arch = x86_64
license = LGPL2.1
provides = libdialog.so=15-64
depend = sh
''' > packages_d_dialog_dialog-1:1.3_20190211-1-x86_64/.PKGINFO

echo -e '''pkgname = dialog
pkgbase = dialog
pkgver = 1:1.3_20220414-1
pkgdesc = A tool to display dialog boxes from shell scripts
url = https://invisible-island.net/dialog/
builddate = 1650081535
packager = Evangelos Foutras <foutrelis@archlinux.org>
size = 483988
arch = x86_64
license = LGPL2.1
provides = libdialog.so=15-64
depend = sh
depend = ncurses
''' > packages_d_dialog_dialog-1:1.3_20220414-1-x86_64/.PKGINFO

# Compress packages folders to .tar.gz archives

tar --force-local -acf 'packages_d_dialog_dialog-1:1.3_20190211-1-x86_64.pkg.tar.xz' -C 'packages_d_dialog_dialog-1:1.3_20190211-1-x86_64' .
tar --force-local -acf 'packages_d_dialog_dialog-1:1.3_20220414-1-x86_64.pkg.tar.zst' -C 'packages_d_dialog_dialog-1:1.3_20220414-1-x86_64' .

mv *.xz ../../https_archive.archlinux.org
mv *.zst ../../https_archive.archlinux.org

# uk.mirror.archlinuxarm.org
mkdir 'aarch64_core_gzip-1.12-1-aarch64'

echo -e '''# Generated by makepkg 6.0.1
# using fakeroot version 1.28
pkgname = gzip
pkgbase = gzip
pkgver = 1.12-1
pkgdesc = GNU compression utility
url = https://www.gnu.org/software/gzip/
builddate = 1649365694
packager = Arch Linux ARM Build System <builder+seattle@archlinuxarm.org>
size = 162688
arch = aarch64
license = GPL3
group = base-devel
depend = glibc
depend = bash
depend = less
''' > aarch64_core_gzip-1.12-1-aarch64/.PKGINFO

tar --force-local -acf 'aarch64_core_gzip-1.12-1-aarch64.pkg.tar.xz' -C 'aarch64_core_gzip-1.12-1-aarch64' .

mv *.xz ../../https_uk.mirror.archlinuxarm.org

# Clean up removing tmp_dir
cd ../../
rm -r tmp_dir/
