#!/usr/bin/env bash
output_dir=$(pwd)
working_dir=/tmp/python_rpm_working_directory
rm -rf "$working_dir"
mkdir -p "$working_dir"/{BUILD,SOURCES,SPECS,RPMS,SRPMS}
rm -rf /tmp/py2pack_work_dir
mkdir /tmp/py2pack_work_dir
cd /tmp/py2pack_work_dir || exit
modules=(PyYAML prompt-toolkit six termcolor wcwidth SQLAlchemy pytest salt Jinja2 MarkupSafe msgpack pycryptodomex pyzmq distro requests )

echo "Будут собраны следующие модули:"
for word in "${modules[@]}"
    do
        echo "$word"
    done
echo ""

for module in "${modules[@]}"
    do
        sleep 10
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "!!! Выполняется генерация spec файла для $module !!!"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        py2pack generate "$module" -t alt.spec -f python3-module-"$module".spec
    done
for module in "${modules[@]}"
    do
        sleep 10
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "!!! Выполняется сбор архива исходных кодов для $module !!!"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        py2pack fetch "$module"
    done
cp /tmp/py2pack_work_dir/*.tar.gz "$working_dir/SOURCES/" || exit
cp /tmp/py2pack_work_dir/*.spec "$working_dir/SPECS/" || exit

cd "$working_dir/SPECS" || exit
for specfile in "$working_dir"/SPECS/*
    do
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        echo "!!!Выполняется сборка $specfile!!!"
        echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        #rpmbuild -vv --define "_topdir $working_dir" --define "__python3_libdir %_target_libdir/python3" -ba $specfile
        rpmbuild -vv --define "_topdir $working_dir" -ba "$specfile"
    done

cp "$working_dir"/{RPMS/noarch,SRPMS}/* "$output_dir" || exit
