#!/bin/sh


CHANNEL=channels/awp-hub-repo/docs/
MIRRORS="
updates.atomicorp.com
"



if [ -f /var/awp/etc/config ]; then
        source /var/awp/etc/config
else
        exit 1
fi

# Exit if this is a control panel environment. Not Supported
if [ -f /usr/local/psa ] || [ -f /usr/local/cpanel ]; then
  exit 0
fi

# Detect utilities
if [ -f /usr/bin/shuf ]; then
  SHUF=/usr/bin/shuf
elif [ -f /bin/shuf ]; then
  SHUF=/bin/shuf
else
  exit 0
fi

function mirror_docs() {
  MSG=$1
  URL=$2

  FILES="*.pdf"
  for host in $($SHUF -e $MIRRORS) ; do
        wget -q -t 6 --user=${USERNAME} --password=${PASSWORD} -r -N -np -nH --cut-dirs=2 -A "${FILES}" https://$host/$URL/
  done
}

if [ -d /var/www/html/ ]; then
  if  [ ! -d /var/www/html/docs/ ]; then
    mkdir -p /var/www/html/docs/
  fi
    pushd /var/www/html/
      mirror_docs documentation ${CHANNEL}
    popd

else
  exit 1
fi
