#!/bin/bash

############################################################################################################
DST=~/www/os
PKG="dnf dosfstools e2fsprogs grub2-efi-x64 grub2-pc kernel microdnf NetworkManager openssh-server shim-x64"
############################################################################################################
ZMB_BASIC=file:///opt/zmb/www/basic
ZMB_EXTRA=file:///opt/zmb/www/extra
############################################################################################################
   ZMB_OS=file:///opt/zmb/rpm/OS/x86_64
############################################################################################################
APPSTREAM=http://rpm.aliennet.ru/oracle/ol8/appstream/x86_64
   BASEOS=http://rpm.aliennet.ru/oracle/ol8/baseos/x86_64
     EPEL=http://rpm.aliennet.ru/epel/8/Everything/x86_64
RPMFUSION=http://rpm.aliennet.ru/rpmfusion/free/el/updates/8/x86_64
############################################################################################################
#APPSTREAM=https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64
#   BASEOS=https://yum.oracle.com/repo/OracleLinux/OL8/baseos/latest/x86_64
#     EPEL=https://mirror.yandex.ru/epel/8/Everything/x86_64
#RPMFUSION=https://mirror.yandex.ru/fedora/rpmfusion/free/el/updates/8/x86_64
############################################################################################################

set -e

#
# NEW packages folder
#
rm -rf $DST/x86_64.*.latest
NEW=$DST/x86_64.$(date +%Y-%m-%d).latest/packages
mkdir -pv $NEW

#
# Packages download
#
dnf --refresh \
    --downloaddir $NEW \
    --resolve --alldeps \
    --assumeyes \
    --nodocs \
    --nogpgcheck \
    --setopt plugins=true \
    --disableplugin \* \
    --enableplugin download \
    --setopt reposdir=/dev/null \
    --repofrompath zmb-basic,$ZMB_BASIC \
    --repofrompath zmb-extra,$ZMB_EXTRA \
    --repofrompath    zmb-os,$ZMB_OS \
    --repofrompath appstream,$APPSTREAM \
    --repofrompath    baseos,$BASEOS \
    --repofrompath      epel,$EPEL \
    --repofrompath rpmfusion,$RPMFUSION \
    download $PKG zmb-\*

#
# modules.yaml download
#
curl --silent $APPSTREAM/$(\
curl --silent $APPSTREAM/repodata/repomd.xml | grep "modules.yaml.gz" | cut -d \" -f 2) | gunzip \
> $NEW/../modules.yaml

#
# Cleaning
#
dnf clean all
rm -rfv /var/tmp/dnf-$USER-* $NEW/zmb-*

echo "OK"
