#!/bin/ksh

# Variables.
datevar=`date +"%Y-%m-%d_%H-%M-%S"`
pub_path="/mnt/storage1/www/isabelladean.com/html/pub"
loop_counter="0"

# Functions.
mirror_func() {
  #rsync -Prv --delete-delay --delay-updates --fuzzy --exclude={'alpha','arm64','armv7','hppa','i386','landisk','loongson','luna88k','macppc','octeon','powerpc64','riscv64','sparc64','packages/aarch64','packages/arm','packages/i386','packages/mips64','packages/powerpc','packages/powerpc64','packages/riscv64','packages/sparc64','packages-stable/aarch64','packages-stable/i386'} "rsync://mirror.exonetric.net/OpenBSD/7.5" "/var/www/isabelladean.com/html/pub/OpenBSD"

  # Files.
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/ftplist" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/timestamp" "$pub_path/OpenBSD"
  # Folders.
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/Changelogs" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/patches" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/LibreSSL" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/OpenBGPD" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/OpenIKED" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/OpenNTPD" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/OpenSSH" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log --exclude={'syspatch/6.1','syspatch/6.2','syspatch/6.3','syspatch/6.4','syspatch/6.5','syspatch/6.6','syspatch/6.7','syspatch/6.8','syspatch/6.9','syspatch/7.0','syspatch/7.1','syspatch/7.2','syspatch/7.3','syspatch/7.4','syspatch/7.5/arm64','syspatch/7.5/i386','syspatch/7.6/arm64','syspatch/7.6/i386'} "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/syspatch" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/rpki-client" "$pub_path/OpenBSD"
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/songs" "$pub_path/OpenBSD"
  # 7.5
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log --exclude={'alpha','arm64','armv7','hppa','i386','landisk','loongson','luna88k','macppc','octeon','powerpc64','riscv64','sparc64','packages/aarch64','packages/arm','packages/i386','packages/mips64','packages/powerpc','packages/powerpc64','packages/riscv64','packages/sparc64','packages-stable/aarch64','packages-stable/i386'} "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/7.5" "$pub_path/OpenBSD"
  # 7.6
  rsync -turvP --delete --fuzzy --log-file=$pub_path/rsync-logs/$datevar.log --exclude={'alpha','arm64','armv7','hppa','i386','landisk','loongson','luna88k','macppc','octeon','powerpc64','riscv64','sparc64','packages/aarch64','packages/arm','packages/i386','packages/mips64','packages/powerpc','packages/powerpc64','packages/riscv64','packages/sparc64','packages/stable/aarch64','packages-stable/i386'} "rsync://rsync.mirrorservice.org/ftp.openbsd.org/pub/OpenBSD/7.6" "$pub_path/OpenBSD"
}

# Main.
mkdir -p $pub_path/OpenBSD $pub_path/rsync-logs

# Prevent overcharge.
while [ $loop_counter -le 2 ]
do
  ((loop_counter=loop_counter + 1))
  if [[ `ls -l $pub_path/rsync-logs | wc -l` -ge 7 ]]; then
    rm -f `ls -t $pub_path/rsync-logs/*.log | tail -1`
    rm -f `ls -t $pub_path/rsync-logs/*.log | tail -1`
    else
      # Continue.
      echo
    fi
done

if [[ `ls -l $pub_path/rsync-logs | wc -l` -le 6 ]]; then
  mirror_func
else
  exit 1
fi
