#!/bin/bash

domain="$1"
id="$2"

# Ensure at least two arguments are passed
if [ "$#" -lt 2 ]; then
  echo "ERROR: Missing information!"
  exit 2
fi

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

if [ -z "$websitePath" ]; then
  echo "ERROR: Unable to retrieve website path."
  exit 3
fi

cd "$websitePath"

fileName="plugin_copy.zip"

# Shift the arguments to skip the domain
shift 1

# Iterate over the remaining arguments in pairs
while (( "$#" >= 1 ))
do

    id="$1"
    echo $id
    
    if [[ $id == *\|* ]]; then
        echo $id
        id=$(echo "$id" | cut -d'|' -f1)
        echo $id
    fi
    
    #check if plugin is already installed, removes it if yes
    /usr/local/bin/php /usr/local/bin/wp plugin is-installed $id
    
    if [ $? -eq 0 ]; then
        /usr/local/bin/php /usr/local/bin/wp plugin uninstall $id --deactivate
    fi
    
    #Double Check if the directory exists, remove it if yes
    if [ -d "$websitePath/wp-content/plugins/$id" ]; then
        rm -r -f $websitePath/wp-content/plugins/$id
    fi

    #going to folder where the website exists
    sshpass -p 'Inception@Broadway2023' ssh -o StrictHostKeyChecking=no templates@54.160.76.56 -p 22 << EOF
    cd /home/templates/public_html/wp-content/plugins

    # Check if the directory exists
    if [ -d "/home/templates/public_html/wp-content/plugins/\$id" ]; then
        zip -r /home/templates/public_html/plugin_copy.zip "$id" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "Successfully created ZIP file"
        else
            echo "Failed to create ZIP file"
        fi
    else
        echo "Directory not found: /home/templates/public_html/wp-content/plugins/\$id"
    fi

    exit
EOF

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

    if [ -e "$fileName" ]; then
        echo "File Copied Successfully"
    else
        echo
        echo "The file $fileName could not be copied. Please contact the Tech Team."
        exit 1
    fi
    
    #move the file to the plugin directory
    mv $fileName $websitePath/wp-content/plugins/$fileName
    
    #unzip the file
    unzip "$websitePath/wp-content/plugins/$fileName" -d "$websitePath/wp-content/plugins/"
    
    # Check if the directory exists
    if [ -d "$websitePath/wp-content/plugins/$id" ]; then
        echo "Plugin Unzipped Successfully"
    else
        echo
        echo "Issue Unzipping File - Please contact the Tech Team."
    fi
    
    #going to back to Templates website and removing temp file
    sshpass -p 'Inception@Broadway2023' ssh -o StrictHostKeyChecking=no templates@54.160.76.56 -p 22 << EOF
    cd /home/templates/public_html
    rm -f $fileName
    exit
EOF

    #remove the temp zip file
    rm -f $websitePath/wp-content/plugins/$fileName
   
    #activate the plugin on the website
    /usr/local/bin/php /usr/local/bin/wp plugin activate $id
    
    # Shift the processed arguments
    shift 1

done

exit 0