#!/bin/bash

## Specify all local repositories in a single variable
LOCAL_REPOS="base centosplus extras updates"

## A loop to update repos one at a time
for REPO in ${LOCAL_REPOS}; do
  reposync -g -l -d -m --repoid=$REPO --newest-only --download-metadata --download_path=/opt/repos/

  # Run createrepo -g comps.xml only for the 'base' repository
  if [ "$REPO" == "base" ]; then
    createrepo -g comps.xml /opt/repos/$REPO/
  else
    createrepo /opt/repos/$REPO/
  fi
done

### Get the current date and time
#DATE_UPDATED=$(date "+%Y-%m-%d %H:%M:%S")
#
### Send completion email using relay server
#mail -a "Content-Type: text/plain; charset=UTF-8" -S smtp=192.168.58.7 -S from=noreply@naaptol.com -s "Repository Update Completed" amol.arote@naaptol.com <<EOF
#Repository update completed on ${DATE_UPDATED}.
#
#Details:
#- Local repositories: ${LOCAL_REPOS}
#- Update script: /opt/anm/scripts/reposync/update-localrepos.sh
#
#Thank you.
#EOF
DATE_UPDATED=$(date "+%Y-%m-%d %H:%M:%S")

## Send completion email using mailx
emailFrom="noreply@naaptol.com"
emailTo="monitoring.alert@naaptol.com"
emailSubject="Repository Update Completed"
emailBody="Repository update completed on ${DATE_UPDATED}.\n\nDetails:\n- Local repositories: ${LOCAL_REPOS}\n- Update script: /opt/anm/scripts/reposync/update-localrepos.sh\n\nThank you."

echo -e "$emailBody" | mailx -s "$emailSubject" -r "$emailFrom" -S smtp=192.168.58.7 "$emailTo"

