#!/bin/bash -e
# Sync ISO files from Julius
#
# © 2015, Multapplied Networks, Inc.
#

if [ "$(whoami)" != "bondingadmin" ] ; then
    sudo -u bondingadmin /usr/sbin/sync-base-isos
    exit $?
fi

# Read the JULIUS variable from settings, remove any quotes, trim off the
# https:// and remove any potentially trailing / in the server name
JULIUS=$(ba print_settings \
    | grep -v _explicit_settings \
    | grep JULIUS_LOCATION \
    | awk '{print $3};' \
    | sed s/\'//g \
    | sed s/\"//g \
    | cut -c 9- \
    | sed 's|\/||g' \
)

LEGACY_REPO="rsync://$JULIUS:/isos/jessie/"
OEM_REPO="rsync://$JULIUS:/oem-iso/$(ba get repository_release)/"
TARGET="/var/lib/bondingadmin/base-isos/"

mkdir -p $TARGET
rsync -a $LEGACY_REPO $TARGET
rsync -a $OEM_REPO $TARGET
