#!/bin/bash

rootDomain="$1"
lpType="$2"
clinicName="$3"
address="$4"
city="$5"
state="$6"
zip="$7"
doctorName="$8"
phone="$9"
hlId="${10}"
special="${11}"
price="${12}"
listItemsHeadline="${13}"
listItem1="${14}"
listItem2="${15}"
listItem3="${16}"
listItem4="${17}"
listItem5="${18}"
mainHeadlineBlack="${19}"
mainHeadlineBlue="${20}"
p1="${21}"
firstH3="${22}"
p2="${23}"
secondH3="${24}"
p3="${25}"
testimonialName1="${26}"
testimonial1="${27}"
testimonialName2="${28}"
testimonial2="${29}"
testimonialName3="${30}"
testimonial3="${31}"
googleMap="${32}"
uploadedDoctorImage="${33}"
doctorAltText="${34}"
uploadedClinicLogo="${35}"
clinicAltText="${36}"
uploadedMobileBackground="${37}"
mobileAltText="${38}"
slug="${39}"
postmetadata="${40}"
lpOptimization="${41}"
typeOfLP="${42}"
lphc="${43}"
gMapOrig="${44}"

# Remove single quotes at the start and end of each variable
rootDomain=$(echo "$rootDomain" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
lpType=$(echo "$lpType" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
clinicName=$(echo "$clinicName" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
address=$(echo "$address" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
city=$(echo "$city" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
state=$(echo "$state" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
zip=$(echo "$zip" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
doctorName=$(echo "$doctorName" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
phone=$(echo "$phone" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
hlId=$(echo "$hlId" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
special=$(echo "$special" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
price=$(echo "$price" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItemsHeadline=$(echo "$listItemsHeadline" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItem1=$(echo "$listItem1" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItem2=$(echo "$listItem2" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItem3=$(echo "$listItem3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItem4=$(echo "$listItem4" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
listItem5=$(echo "$listItem5" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
mainHeadlineBlack=$(echo "$mainHeadlineBlack" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
mainHeadlineBlue=$(echo "$mainHeadlineBlue" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
p1=$(echo "$p1" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
firstH3=$(echo "$firstH3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
p2=$(echo "$p2" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
secondH3=$(echo "$secondH3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
p3=$(echo "$p3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonialName1=$(echo "$testimonialName1" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonial1=$(echo "$testimonial1" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonialName2=$(echo "$testimonialName2" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonial2=$(echo "$testimonial2" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonialName3=$(echo "$testimonialName3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
testimonial3=$(echo "$testimonial3" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
googleMap=$(echo "$googleMap" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
uploadedDoctorImage=$(echo "$uploadedDoctorImage" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
doctorAltText=$(echo "$doctorAltText" | sed -e "s/^'//" -e "s/'$//")
uploadedClinicLogo=$(echo "$uploadedClinicLogo" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
clinicAltText=$(echo "$clinicAltText" | sed -e "s/^'//" -e "s/'$//")
uploadedMobileBackground=$(echo "$uploadedMobileBackground" | sed -e "s/^'//" -e "s/'$//" | sed 's/\\//g')
mobileAltText=$(echo "$mobileAltText" | sed -e "s/^'//" -e "s/'$//")
slug=$(echo "$slug" | sed -e "s/^'//" -e "s/'$//")
postmetadata=$(echo "$postmetadata" | sed -e "s/^'//" -e "s/'$//")
lpOptimization=$(echo "$lpOptimization" | sed -e "s/^'//" -e "s/'$//")
typeOfLP=$(echo "$typeOfLP" | sed -e "s/^'//" -e "s/'$//")
lphc=$(echo "$lphc" | sed -e "s/^'//" -e "s/'$//")
gMapOrig=$(echo "$gMapOrig" | sed -e "s/^'//" -e "s/'$//")

phonenospace=$(echo "$phone" | tr -cd [:alnum:])
googleFinal=$(echo "$googleMap" | cut -d'"' -f 2)

#define the landing page type
if [[ "$typeOfLP" == "google" ]]; then
    typeOfLP="GLP"
elif [[ "$typeOfLP" == "facebook" ]]; then
    typeOfLP="FBLP"
else
    typeOfLP="LP"
fi

# For testing and troubleshooting purposes
#echo "Root Domain: $rootDomain"
#echo "LP Type: $lpType"
#echo "Clinic Name: $clinicName"
#echo "ADDRESS: $address"
#echo "CITY: $city"
#echo "STATE: $state"
#echo "ZIP: $zip"
#echo "Doctor Name: $doctorName"
#echo "Phone Number: $phone"
#echo "HL ID: $hlId"
#echo "Special: $special"
#echo "Price: $price"
#echo "List Item Headline: $listItemsHeadline"
#echo "List Item 1: $listItem1"
#echo "List Item 2: $listItem2"
#echo "List Item 3: $listItem3"
#echo "List Item 4: $listItem4"
#echo "List Item 5: $listItem5"
#echo "Black Headline: $mainHeadlineBlack"
#echo "Blue Headline: $mainHeadlineBlue"
#echo "Paragraph 1: $p1"
#echo "Title 2: $firstH3"
#echo "Paragraph 2: $p2"
#echo "Title 3: $secondH3"
#echo "Paragraph 3: $p3"
#echo "Name 1: $testimonialName1"
#echo "Testimonial 1: $testimonial1"
#echo "Name 2: $testimonialName2"
#echo "Testimonial 2: $testimonial2"
#echo "Name 3: $testimonialName3"
#echo "Testimonial 3: $testimonial3"
#echo "Google Map to Use: $googleFinal"
#echo "$uploadedDoctorImage"
#echo "Doctor Alt Text: $doctorAltText"
#echo "$uploadedClinicLogo"
#echo "Clinc Alt Text: $clinicAltText"
#echo "$uploadedMobileBackground"
#echo "Background Alt Text: $mobileAltText"
#echo "Slug: $slug"
#echo "Metadata: $postmetadata"
#echo "Optimization: $lpOptimization"
#echo "Type of LP: $typeOfLP"
#echo "Hero Content: $lphc"
#echo "Google Map Original: $gMapOrig"
#echo " "
#echo

#slug checkers
if [[ "$slug" == *" "* ]]; then
    echo "Slug '$slug' contains spaces! Please remove those & try again!"
    exit 1
fi

if [[ "$slug" == *"/"* ]]; then
    echo "Slug '$slug' contains forward slashes! Please remove those & try again!"
    exit 1
fi

#if photo is not correct file type, give an error
if [[ ! $uploadedDoctorImage =~ \.(jpeg|jpg|gif|png|webp)$ ]]; then
    echo "Invalid file format for the doctor photo! Only .jpeg, .jpg, .gif, .png, and .webp are allowed."
    exit 1
fi

if [[ ! $uploadedClinicLogo =~ \.(jpeg|jpg|gif|png|webp)$ ]]; then
    echo "Invalid file format for the clinic logo photo! Only .jpeg, .jpg, .gif, .png, and .webp are allowed."
    exit 1
fi

if [[ ! -z "$uploadedMobileBackground" ]]; then
    if [[ ! $uploadedMobileBackground =~ \.(jpeg|jpg|gif|png|webp)$ ]]; then
        echo "Invalid file format for the background! Only .jpeg, .jpg, .gif, .png, and .webp are allowed."
        exit 1
    fi
    
    #check if alt text field was filled out
    if [[ -z "$mobileAltText" ]]; then
        echo "Mobile Background Alternate Text field was empty! Please try again and fill out all fields."
        exit 1
    fi
fi

#get today's date
today=$(date +'%m-%d-%Y-%H-%M')
postdate=$(date +'%F %T')

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

#define a file name that is easy to find and remove with cronjobs
fileName=inceptionlpcopy1.xml

#get the information about the landing page needed from the lp-type-list.json file
jsonPath="$websitePath/wp-content/plugins/inception-techboost/lp-maker/lp-type-list.json"
lpTopic="${lpType}"
lpTitle=$(cat $jsonPath | jq -r ".[] | select(.lpTopic==\"$lpTopic\") | .lpTitle")
pageId=$(cat $jsonPath | jq -r ".[] | select(.lpTopic==\"$lpTopic\") | .pageId")
originalSlug=$(cat $jsonPath | jq -r ".[] | select(.lpTopic==\"$lpTopic\") | .originalSlug")
originalHlId=$(cat $jsonPath | jq -r ".[] | select(.lpTopic==\"$lpTopic\") | .originalHlId")

#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
    /usr/local/bin/php /usr/local/bin/wp post update $pageId --post_status=pending --allow-root --quiet
    /usr/local/bin/php /usr/local/bin/wp export --post__in=$pageId --post_type=page --post_status=pending --filename_format=$fileName --allow-root > export_output.log 2>&1
    /usr/local/bin/php /usr/local/bin/wp post update $pageId --post_status=publish --allow-root --quiet
    exit
EOF

cd $websitePath

#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:]')"

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

#import it
import=$(/usr/local/bin/php /usr/local/bin/wp import $fileName --authors=skip --allow-root)

#remove the unnecessary file
rm -f $fileName

#if images are chiro images, download it
if [[ $uploadedMobileBackground == *"inceptionimages.com"* ]]; then
    mobile_image_file_name=$(echo "$uploadedMobileBackground" | sed 's:.*/::')
    extension="${mobile_image_file_name##*.}"
    
    #remove special characters
    mobile_alt_text_no_special=$(echo $mobileAltText | sed 's/[^- a-z A-Z 0-9]//g')
    
    #alt text replace spaces with dashes for filename
    mobileImageFileName=$(echo $mobile_alt_text_no_special | tr " " "-")
    mobileImageFileName="$mobileImageFileName.$extension"
    
    #define variables to download from Templates site
    imagefromIP="18.214.60.67"
    imagefromDomain=hero.inceptionimages.com
    
    #download the image and define the path to it
    curl --resolve "$imagefromDomain":443:"$imagefromIP" -o $mobileImageFileName "$uploadedMobileBackground" &> /dev/null;

    # Check if the download was successful
    if [[ $? -ne 0 ]]; then
        # If download failed, try to download from the old IP
        imagefromOldIP="67.225.142.253"
        curl --resolve "$imagefromDomain":443:"$imagefromOldIP" -o $mobileImageFileName "$uploadedMobileBackground" &> /dev/null;
    fi
    mobile_image_file_name=$websitePath/$mobileImageFileName
    else
    mobile_image_file_name=$uploadedMobileBackground
fi

#get ID of the post for later
ID=$(/usr/local/bin/php /usr/local/bin/wp post list --post_type=page --post_status=pending --field=ID --allow-root)

#import images to the media library if they aren't in there already
if [[ "$uploadedDoctorImage" == *"/home/"* ]]; then
    doctor_file_id=$(/usr/local/bin/php /usr/local/bin/wp media import "$uploadedDoctorImage" --alt="$doctorAltText" --title="$doctorAltText" --allow-root)
    #get the id number for the uploaded image
    doctor_media_id=$(echo "$doctor_file_id" | grep -oP 'attachment ID\s+\K[^ ]+.*' | awk -F"." '{print $1}')
    #remove the image from public_html
    rm -f "$uploadedDoctorImage"
    #get the url of the image in the media library
    doctor_media_url=$(/usr/local/bin/php /usr/local/bin/wp post get "$doctor_media_id" --field='guid' --allow-root)
    else
    doctor_media_url="${uploadedDoctorImage}"
fi

if [[ "$uploadedClinicLogo" == *"/home/"* ]]; then
    clinic_file_id=$(/usr/local/bin/php /usr/local/bin/wp media import "$uploadedClinicLogo" --alt="$clinicAltText" --title="$clinicAltText" --allow-root)
    #get the id number for the uploaded image
    clinic_media_id=$(echo "$clinic_file_id" | grep -oP 'attachment ID\s+\K[^ ]+.*' | awk -F"." '{print $1}')
    #remove the image from public_html
    rm -f "$uploadedClinicLogo"
    #get the url of the image in the media library
    clinic_media_url=$(/usr/local/bin/php /usr/local/bin/wp post get "$clinic_media_id" --field='guid' --allow-root)
    else
    clinic_media_url="${uploadedClinicLogo}"
fi

if [[ ! -z "$mobile_image_file_name" ]]; then
    mobile_file_id=$(/usr/local/bin/php /usr/local/bin/wp media import "$mobile_image_file_name" --alt="$mobileAltText" --title="$mobileAltText" --allow-root)
    #get the id number for the uploaded image
    mobile_media_id=$(echo "$mobile_file_id" | grep -oP 'attachment ID\s+\K[^ ]+.*' | awk -F"." '{print $1}')
    #remove the image from public_html
    rm -f "$mobile_image_file_name"
    #get the url of the image in the media library
    mobile_media_url=$(/usr/local/bin/php /usr/local/bin/wp post get "$mobile_media_id" --field='guid' --allow-root)
fi

#check if the slug exists already
slugCheck=$(/usr/local/bin/php /usr/local/bin/wp post list --post_type=page --name="$slug" --field=ID --allow-root)

#if slug exists, add a -2 at the end of it
if [[ ! -z "$slugCheck" ]]; then
    slug="$slug-2"
fi

#get the user who will be the page author
user=$(/usr/local/bin/php /usr/local/bin/wp user list --role=administrator --field=ID --allow-root)

#publish the page
publish=$(/usr/local/bin/php /usr/local/bin/wp post update $ID --post_status=publish --post_name="$slug" --post_date="$postdate" --allow-root --quiet)

#search and replace for custom landing page elements only
if [[ "$lpType" == *"custom"* ]]; then
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPOP*" "${lpOptimization}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPHC*" "${lphc}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPLISTHEADLINE*" "${listItemsHeadline}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LIST1*" "${listItem1}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LIST2*" "${listItem2}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LIST3*" "${listItem3}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LIST4*" "${listItem4}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LIST5*" "${listItem5}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPCT1*" "${mainHeadlineBlack}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPCT2*" "${mainHeadlineBlue}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPC1*" "${p1}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPCT3*" "${firstH3}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPC2*" "${p2}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPCT4*" "${secondH3}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPC3*" "${p3}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "METAD*" "$postmetadata" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "LPFULLNAME*" "${doctorName}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "https://www.hero.inceptionimages.com/wp-content/uploads/Banner-Sliders-LP-4-Placeholder.webp" "${mobile_media_url}" --all-tables --allow-root --quiet
    
    /usr/local/bin/php /usr/local/bin/wp search-replace "http://FIMAGEURL*" "${mobile_media_url}" --all-tables --allow-root --quiet
fi

#search and replace ALL THE THINGS
/usr/local/bin/php /usr/local/bin/wp search-replace "https://www.hero.inceptionimages.com/wp-content/uploads/Meet-The-Doctor-LP4H.webp" "${doctor_media_url}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "https://www.chiro.inceptionimages.com/wp-content/uploads/2017/04/falcon-logo.png" "${clinic_media_url}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "CLINIC NAME*" "${clinicName}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "ADDRESS*" "${address}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "CITY*" "${city}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "STATE*" "${state}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "ZIP*" "${zip}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "(000) 000-0000" "${phone}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "0000000000" "${phonenospace}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "$originalHlId" "${hlId}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "SPECIALOFFER*" "${special}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "\$XX*" "${price}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "FULL NAME*" "${doctorName}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIALNAME1*" "${testimonialName1}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIAL1*" "${testimonial1}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIALNAME2*" "${testimonialName2}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIAL2*" "${testimonial2}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIALNAME3*" "${testimonialName3}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "TESTIMONIAL3*" "${testimonial3}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "$originalSlug" "${slug}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "GFBLP*" "${typeOfLP}" --all-tables --allow-root --quiet

/usr/local/bin/php /usr/local/bin/wp search-replace "${gMapOrig}" "${googleFinal}" --all-tables --allow-root --quiet

#purge the cache to update the images for the user
/usr/local/bin/php /usr/local/bin/wp beaver clearcache --all --allow-root --quiet

#echo the correct slug for the success message
echo "https://www.$rootDomain/$slug"

exit 0