#!/bin/bash -xe
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

source /usr/share/mirror-update/functions.sh

MIRROR_VOLUME=$1

if [[ ${NO_TIMEOUT:-0} -eq 1 ]]; then
    echo "Running interactively"
    TIMEOUT=""
else
    TIMEOUT="timeout -k 2m 30m"
fi

_DRY_RUN=''
if [[ ${DRY_RUN:-0} -eq 1 ]]; then
    echo "Dry run only"
    _DRY_RUN='--dry-run'
fi

RSYNC="rsync ${_DRY_RUN} -rltvz"

BASE="/afs/.openstack.org/mirror/centos"
K5START="k5start -t -f /etc/centos.keytab service/centos-mirror -- $TIMEOUT"

#
# -- Centos 8-stream --
#

MIRROR=rsync://mirror.dal10.us.leaseweb.net/centos/8-stream/ # somewhere in TX

if ! [ -f $BASE/8-stream ]; then
    $K5START mkdir -p $BASE/8-stream
fi
date --iso-8601=ns
echo "Running Centos 8-stream rsync..."
$K5START ${RSYNC} \
    --delete \
    --delete-excluded \
    --exclude="isos" \
    --exclude="ppc64le*" \
    --exclude=".*" \
    $MIRROR $BASE/8-stream

#
# -- Centos 8 --
#

MIRROR=rsync://mirror.dal10.us.leaseweb.net/centos/8/ # somewhere in TX

if ! [ -f $BASE/8 ]; then
    $K5START mkdir -p $BASE/8
fi
date --iso-8601=ns
echo "Running Centos 8 rsync..."
$K5START ${RSYNC} \
    --delete \
    --delete-excluded \
    --exclude="isos" \
    --exclude="ppc64le*" \
    --exclude=".*" \
    $MIRROR $BASE/8

#
# -- Centos 7 --
#

MIRROR="rsync://mirrors.kernel.org/centos"
MIRROR_ALTARCH="rsync://mirror.dal.nexril.net/centos-altarch/"
REPOS="7 altarch/7"
ALTARCHS="aarch64"
ALTARCHS_IGNORED="armhfp i386 power9 ppc64 ppc64le x86_64" # altarch/kernel/ has x86_64 dir we do not want

for REPO in $REPOS; do
    if ! [ -f $BASE/$REPO ]; then
        $K5START mkdir -p $BASE/$REPO
    fi

    MIRROR_PATH=$MIRROR/$REPO/
    EXTRA_OPTS="--links" # -l
    if [[ $REPO == "altarch"* ]]; then
        MIRROR_PATH=$MIRROR_ALTARCH/7/
        EXTRA_OPTS="--copy-links" # copy because altarch has symlinks to ../7.6.1810/

        for arch in $ALTARCHS_IGNORED; do
            EXTRA_OPTS="$EXTRA_OPTS --exclude=$arch"
        done
        for arch in $ALTARCHS; do
            EXTRA_OPTS="$EXTRA_OPTS --include *.${arch}.rpm"
        done
    fi

    date --iso-8601=ns
    echo "Running rsync..."
    $K5START ${RSYNC} \
    --delete \
    --delete-excluded \
    --exclude="atomic" \
    --exclude="centosplus" \
    --exclude="experimental" \
    --exclude="fasttrack" \
    --exclude="isos" \
    --exclude="paas" \
    --exclude="*/drpms" \
    $EXTRA_OPTS \
    $MIRROR_PATH $BASE/$REPO/

    # TODO(pabelanger): Validate rsync process
done

if [[ ${DRY_RUN:-0} -ne 1 ]]; then
    date --iso-8601=ns | $K5START tee $BASE/timestamp.txt
    echo "rsync completed successfully, running vos release."
    vos_release $MIRROR_VOLUME
fi

date --iso-8601=ns
echo "Done."