#!/bin/bash
#
# @(#) rsync_ulmdnf.sh
#
# Usage:
#   ./rsync_ulmdnf.sh -r V120
# Description
#   rsync RPM packages: from local/gitlab-runner to ulmdnf server
# Return
#   0: Success
#   1: Error(rpmspec/rpmbuild/rsync/ssh)
#   2: Usage

progname=${0##*/}
progdir=${0%/*}
export LANG=C

dryrun=0
verbose=0

# Local
readonly INSTALL_DIR="${progdir}/../../dnf_tree"
readonly INSTALL_RPMS_DIR="RPMS"
readonly INSTALL_REL_DIR="relnote"

# ulmdnf server
readonly SERV="ulmdnf"
readonly SERV_USER="ulm"
readonly SERV_RC_DIR="/pub/www/doc/RC/ulm"
readonly SERV_RC_ROCKY_DIR="/pub/www/doc/RC/ulm-rocky"
readonly SERV_V120_DIR="/pub/www/doc/release/V120/ulm"
readonly SERV_V120_ROCKY_DIR="/pub/www/doc/release/V120/ulm-rocky"
readonly SERV_V121_ROCKY_DIR="/pub/www/doc/release/V121/ulm"
readonly SERV_V130_ROCKY_DIR="/pub/www/doc/release/V130/ulm"
readonly SERV_V200_DIR="/pub/www/doc/release/V200/ulm"
readonly SERV_V200_ROCKY_DIR="/pub/www/doc/release/V200/ulm-rocky"
readonly SERV_V210_ROCKY_DIR="/pub/www/doc/release/V210/ulm"
readonly SERV_V220_ROCKY_DIR="/pub/www/doc/release/V220/ulm"
readonly SERV_V230_ROCKY_DIR="/pub/www/doc/release/V230/ulm"
readonly SERV_V240_ROCKY_DIR="/pub/www/doc/release/V240/ulm"
readonly SERV_V250_ROCKY_DIR="/pub/www/doc/release/V250/ulm"
readonly SERV_USR_DIR="/pub/home/ulm"

# updaterepo
readonly UPDATEREPO_RC="-R"
readonly UPDATEREPO_V120="-V V120"
readonly UPDATEREPO_V121="-V V121"
readonly UPDATEREPO_V130="-V V130"
readonly UPDATEREPO_V200="-V V200"
readonly UPDATEREPO_V210="-V V210"
readonly UPDATEREPO_V220="-V V220"
readonly UPDATEREPO_V230="-V V230"
readonly UPDATEREPO_V240="-V V240"
readonly UPDATEREPO_V250="-V V250"

set -o pipefail

cmd() {
    cmd="$*"
    [ ${verbose} = 0 ] || echo "${cmd}"
    [ ${dryrun} = 0 ] && eval ${cmd}
}

usage() {
    cat <<EOF
$progname [-dv] [-r repo]
    -d dry run
    -r ulmdnf repository
    -v verbose
    rsync RPM packages: from local/gitlab-runner to ulmdnf server
EOF
    exit 2
}

while getopts dr:v OPT; do
    case "${OPT}" in
    d) dryrun=1 ;;
    r) repo="${OPTARG}" ;;
    v) verbose=1 ;;
    *) usage ;;
    esac
done
shift $((OPTIND - 1))

case "${repo}" in
"V120")
    dst="${SERV_V120_DIR}"
    opt="${UPDATEREPO_V120}"
    ;;
"V120_ROCKY")
    dst="${SERV_V120_ROCKY_DIR}"
    opt="${UPDATEREPO_V120}"
    ;;
"V121")
    dst="${SERV_V121_ROCKY_DIR}"
    opt="${UPDATEREPO_V121}"
    ;;
"V130")
    dst="${SERV_V130_ROCKY_DIR}"
    opt="${UPDATEREPO_V130}"
    ;;
"V200")
    dst="${SERV_V200_DIR}"
    opt="${UPDATEREPO_V200}"
    ;;
"V200_ROCKY")
    dst="${SERV_V200_ROCKY_DIR}"
    opt="${UPDATEREPO_V200}"
    ;;
"V210")
    dst="${SERV_V210_ROCKY_DIR}"
    opt="${UPDATEREPO_V210}"
    ;;
"V220")
    dst="${SERV_V220_ROCKY_DIR}"
    opt="${UPDATEREPO_V220}"
    ;;
"V230")
    dst="${SERV_V230_ROCKY_DIR}"
    opt="${UPDATEREPO_V230}"
    ;;
"V240")
    dst="${SERV_V240_ROCKY_DIR}"
    opt="${UPDATEREPO_V240}"
    ;;
"V250")
    dst="${SERV_V250_ROCKY_DIR}"
    opt="${UPDATEREPO_V250}"
    ;;
"RC")
    dst="${SERV_RC_DIR}"
    opt="${UPDATEREPO_RC}"
    ;;
"RC_ROCKY")
    dst="${SERV_RC_ROCKY_DIR}"
    opt="${UPDATEREPO_RC}"
    ;;
"TRUE")
    dst="${SERV_RC_DIR}"
    opt="${UPDATEREPO_RC}"
    ;;
*)
    echo "Error: illegal Repo: ${DNF_REPO}" 1>&2
    exit 1
    ;;
esac

# sony_fpga_driver_py？
git remote -v | grep "sony_fpga_driver_py.git" &>/dev/null
if (($? != 0)); then
    spec_file=$(find "${progdir}/../../" -maxdepth 1 -name '*.spec' | tail -1)
    if [[ -z "${spec_file}" || ! -f "${spec_file}" ]]; then
        echo "Error: Could not find spec file." 1>&2
        exit 1
    fi

    arch=$(rpmspec -q --srpm --qf '%{arch}' "${spec_file}")
    if [[ -z "${arch}" ]]; then
        echo "Error: Could not find ARCH." 1>&2
        exit 1
    fi

    rpmdir=$(rpmbuild --eval %_rpmdir)/${arch}
    if [[ -z "${rpmdir}" || ! -d "${rpmdir}" ]]; then
        echo "Error: Could not find rpmdir." 1>&2
        exit 1
    fi

    rpms=($(rpmspec -q --qf "${rpmdir}/%{nvra}.rpm\n" "${spec_file}"))
    omit_package=("\-debuginfo\-" "\-debugsource\-" ${OMIT_PACKAGES})
    fix_rpms=($(echo ${rpms[@]} | tr ' ' '\n' | egrep -v "$(echo ${omit_package[*]} | tr ' ' '|')"))
    for rpm_file in ${fix_rpms[@]}; do
        if [[ ! -f "${rpm_file}" ]]; then
            echo "Error: Could not find RPM Package." 1>&2
            exit 1
        fi
    done
else
    spec_file="None"
    arch="noarch"
    rpmdir="$HOME/rpmbuild/RPMS/noarh"
    sony_fpga_driver_py_ver=$(grep "version=" setup.py | awk -F"'" '{print $2}')
    fix_rpms="$HOME/rpmbuild/RPMS/noarch/ulm-python3-sony_fpga_driver-${sony_fpga_driver_py_ver}-1.el8.noarch.rpm"
fi

if ((${verbose} == 1)); then
    echo
    echo "[RPM Package Information]"
    echo "spec_file: ${spec_file}"
    echo "arch: ${arch}"
    echo "rpmdir: ${rpmdir}"
    echo "fix_rpms[@]: ${fix_rpms[@]}"
    echo
fi

# create RPM package tree
mkdir -p "${INSTALL_DIR}/${INSTALL_RPMS_DIR}/${arch}"
mkdir -p "${INSTALL_DIR}/${INSTALL_REL_DIR}"
ln -sf "${fix_rpms[@]}" "${INSTALL_DIR}/${INSTALL_RPMS_DIR}/${arch}"

# rsync RPM packages
cmd "rsync -vaL ${INSTALL_DIR}/${INSTALL_RPMS_DIR} ${SERV_USER}@${SERV}:${dst}"
if (($? != 0)); then
    echo "Error: rsync RPM packages" 1>&2
    exit 1
fi

# updaterepo
cmd "ssh ${SERV_USER}@${SERV} /usr/bin/updaterepo ${opt}"
if (($? != 0)); then
    echo "Error: updaterepo" 1>&2
    exit 1
fi

# rsync relnote
relnote=$(find "${progdir}/../../" -maxdepth 1 -name '*relnote.txt' | tail -1)
if [[ -f "${relnote}" ]]; then
    ln -sf "../../${relnote##*/}" "${INSTALL_DIR}/${INSTALL_REL_DIR}"
    cmd "rsync -vaL ${INSTALL_DIR}/${INSTALL_REL_DIR} ${SERV_USER}@${SERV}:${SERV_USR_DIR}"
    if (($? != 0)); then
        echo "Error: rsync relnote" 1>&2
        exit 1
    fi
fi

echo
echo "Success: rsync ulmdnf"
echo

exit 0
