#bin/bash

# Validate arguments
if [ "$#" -lt 2 ]; then
  echo "ERROR: Template Must Be Selected!"
  exit 1
fi

#stores path to site within variable
websitePath=$(uapi --output=jsonpretty DomainInfo single_domain_data domain=$1 | jq -r '.result.data.documentroot')

fileName=templateCopy1.xml

#going to folder where the website exists
sshpass -p 'Inception@Broadway2023' ssh -o StrictHostKeyChecking=no templates@172.16.142.161 -p 522 << EOF
    cd /home/templates/public_html
    wp export --post__in=$2 --post_type=fl-builder-template,page,post --quiet --filename_format=$fileName --allow-root > export_output.log 2>&1
    exit
EOF

cd $websitePath

#define variables to download from Templates site
fromIP="172.16.142.161"
fromDomain=templates.inception-example.com
fromDomainFull="https://${fromDomain}/$fileName"
fromDomainFull="$(echo -e "${fromDomainFull}" | tr -d '[:space:]')"

#download the file
curl --resolve "$fromDomain":443:"$fromIP" -o $fileName "$fromDomainFull" &> /dev/null;

exit 0