#! /bin/bash

#
## DESCRIPTION:
#    Uploading MP4 to Wordpress and Telegram.
#    Additionally:
#    - editing Wordpress page from template
#    - recompressing video if size over 50MB
#    This is only a local "proof of conept" for testing and debugging.
#

#
## DEPENDENCIES: sudo apt|yum install -y curl ffmpeg libxml2-utils jq
#

#
## FUNCTIONS
#

addtologs() {
	echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
}
execpause() {
	read -p "Press [ENTER] to continue... "
}
execquite() {
	addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
	if [ "${show}" != "qn" ];then
		execpause
	fi
	exit
}
execerror() {
	addtologs "error: $1"
	curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="error: $1" >> /dev/null 2>&1
	execquite
}
getconfig() {
	logs=$(cat $conf | grep "logs=" | cut -d= -f2)
	pathroot=$(cat $conf | grep "pathroot=" | cut -d= -f2)
	vidnamesarray=($(cat $conf | grep "vidnamesarray=" | cut -d= -f2))
	telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2)
	telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2)
	tgpreviewlink=$(cat $conf | grep "tgpreviewlink=" | cut -d= -f2)
	tgpreviewtext=$(cat $conf | grep "tgpreviewtext=" | cut -d= -f2)
	wpxmlrpclink=$(cat $conf | grep "wpxmlrpclink=" | cut -d= -f2)
	wpxmlrpcuser=$(cat $conf | grep "wpxmlrpcuser=" | cut -d= -f2)
	wpxmlrpcpass=$(cat $conf | grep "wpxmlrpcpass=" | cut -d= -f2)
	wppageauthor=$(cat $conf | grep "wppageauthor=" | cut -d= -f2)
	wppagelinkis=$(cat $conf | grep "wppagelinkis=" | cut -d= -f2)
	wpeditpageid=$(cat $conf | grep "wpeditpageid=" | cut -d= -f2)
	wpedituserid=$(cat $conf | grep "wpedituserid=" | cut -d= -f2)
	wpeditdateis=$(cat $conf | grep "wpeditdateis=" | cut -d= -f2)
	wptemplateis=$(cat $conf | grep "wptemplateis=" | cut -d= -f2)
	youtubelink=$(cat $conf | grep "youtubelink=" | cut -d= -f2)

	defaultdp01=($(cat $conf | grep "defaultdp01=" | cut -d= -f2))
	defaultwp01=($(cat $conf | grep "defaultwp01=" | cut -d= -f2))
	defaultmp01=($(cat $conf | grep "defaultmp01=" | cut -d= -f2))
	defaultyp01=($(cat $conf | grep "defaultyp01=" | cut -d= -f2))

	defaultdp02=($(cat $conf | grep "defaultdp02=" | cut -d= -f2))
	defaultwp02=($(cat $conf | grep "defaultwp02=" | cut -d= -f2))
	defaultmp02=($(cat $conf | grep "defaultmp02=" | cut -d= -f2))
	defaultyp02=($(cat $conf | grep "defaultyp02=" | cut -d= -f2))

	defaultdp04=($(cat $conf | grep "defaultdp04=" | cut -d= -f2))
	defaultwp04=($(cat $conf | grep "defaultwp04=" | cut -d= -f2))
	defaultmp04=($(cat $conf | grep "defaultmp04=" | cut -d= -f2))
	defaultyp04=($(cat $conf | grep "defaultyp04=" | cut -d= -f2))

	defaultdp05=($(cat $conf | grep "defaultdp05=" | cut -d= -f2))
	defaultwp05=($(cat $conf | grep "defaultwp05=" | cut -d= -f2))
	defaultmp05=($(cat $conf | grep "defaultmp05=" | cut -d= -f2))
	defaultyp05=($(cat $conf | grep "defaultyp05=" | cut -d= -f2))

	defaultdp11=($(cat $conf | grep "defaultdp11=" | cut -d= -f2))
	defaultwp11=($(cat $conf | grep "defaultwp11=" | cut -d= -f2))
	defaultmp11=($(cat $conf | grep "defaultmp11=" | cut -d= -f2))
	defaultyp11=($(cat $conf | grep "defaultyp11=" | cut -d= -f2))

	defaultdp12=($(cat $conf | grep "defaultdp12=" | cut -d= -f2))
	defaultwp12=($(cat $conf | grep "defaultwp12=" | cut -d= -f2))
	defaultmp12=($(cat $conf | grep "defaultmp12=" | cut -d= -f2))
	defaultyp12=($(cat $conf | grep "defaultyp12=" | cut -d= -f2))


	currentdp01=($(cat $conf | grep "currentdp01=" | cut -d= -f2))
	if [ -z "$currentdp01" ];then
		currentdp01=(${defaultdp01[*]})
	fi
	currentwp01=($(cat $conf | grep "currentwp01=" | cut -d= -f2))
	if [ -z "$currentwp01" ];then
		currentwp01=(${defaultwp01[*]})
	fi
	currentmp01=($(cat $conf | grep "currentmp01=" | cut -d= -f2))
		if [ -z "$currentmp01" ];then
		currentmp01=(${defaultmp01[*]})
	fi
	currentyp01=($(cat $conf | grep "currentyp01=" | cut -d= -f2))
	if [ -z "$currentyp01" ];then
		currentyp01=(${defaultyp01[*]})
	fi

	currentdp02=($(cat $conf | grep "currentdp02=" | cut -d= -f2))
	if [ -z "$currentdp02" ];then
		currentdp02=(${defaultdp02[*]})
	fi
	currentwp02=($(cat $conf | grep "currentwp02=" | cut -d= -f2))
	if [ -z "$currentwp02" ];then
		currentwp02=(${defaultwp02[*]})
	fi
	currentmp02=($(cat $conf | grep "currentmp02=" | cut -d= -f2))
	if [ -z "$currentmp02" ];then
		currentmp02=(${defaultmp02[*]})
	fi
	currentyp02=($(cat $conf | grep "currentyp02=" | cut -d= -f2))
	if [ -z "$currentyp02" ];then
		currentyp02=(${defaultyp02[*]})
	fi

	currentdp04=($(cat $conf | grep "currentdp04=" | cut -d= -f2))
	if [ -z "$currentdp04" ];then
		currentdp04=(${defaultdp04[*]})
	fi
	currentwp04=($(cat $conf | grep "currentwp04=" | cut -d= -f2))
	if [ -z "$currentwp04" ];then
		currentwp04=(${defaultwp04[*]})
	fi
	currentmp04=($(cat $conf | grep "currentmp04=" | cut -d= -f2))
	if [ -z "$currentmp04" ];then
		currentmp04=(${defaultmp04[*]})
	fi
	currentyp04=($(cat $conf | grep "currentyp04=" | cut -d= -f2))
	if [ -z "$currentyp04" ];then
		currentyp04=(${defaultyp04[*]})
	fi

	currentdp05=($(cat $conf | grep "currentdp05=" | cut -d= -f2))
	if [ -z "$currentdp05" ];then
		currentdp05=(${defaultdp05[*]})
	fi
	currentwp05=($(cat $conf | grep "currentwp05=" | cut -d= -f2))
	if [ -z "$currentwp05" ];then
		currentwp05=(${defaultwp05[*]})
	fi
	currentmp05=($(cat $conf | grep "currentmp05=" | cut -d= -f2))
	if [ -z "$currentmp05" ];then
		currentmp05=(${defaultmp05[*]})
	fi
	currentyp05=($(cat $conf | grep "currentyp05=" | cut -d= -f2))
	if [ -z "$currentyp05" ];then
		currentyp05=(${defaultyp05[*]})
	fi

	currentdp11=($(cat $conf | grep "currentdp11=" | cut -d= -f2))
	if [ -z "$currentdp11" ];then
		currentdp11=(${defaultdp11[*]})
	fi
	currentwp11=($(cat $conf | grep "currentwp11=" | cut -d= -f2))
	if [ -z "$currentwp11" ];then
		currentwp11=(${defaultwp11[*]})
	fi
	currentmp11=($(cat $conf | grep "currentmp11=" | cut -d= -f2))
	if [ -z "$currentmp11" ];then
		currentmp11=(${defaultmp11[*]})
	fi
	currentyp11=($(cat $conf | grep "currentyp11=" | cut -d= -f2))
	if [ -z "$currentyp11" ];then
		currentyp11=(${defaultyp11[*]})
	fi

	currentdp12=($(cat $conf | grep "currentdp12=" | cut -d= -f2))
	if [ -z "$currentdp12" ];then
		currentdp12=(${defaultdp12[*]})
	fi
	currentwp12=($(cat $conf | grep "currentwp12=" | cut -d= -f2))
	if [ -z "$currentwp12" ];then
		currentwp12=(${defaultwp12[*]})
	fi
	currentmp12=($(cat $conf | grep "currentmp12=" | cut -d= -f2))
	if [ -z "$currentmp12" ];then
		currentmp12=(${defaultmp12[*]})
	fi
	currentyp12=($(cat $conf | grep "currentyp12=" | cut -d= -f2))
	if [ -z "$currentyp12" ];then
		currentyp12=(${defaultyp12[*]})
	fi
}
setconfig() {
	if [ -z "$target" ] || [ "$target" == "-d" ];then
		if [ ! -z "${vidnamesarray[0]}" ] && [ ! -z "${vidlinksarray[0]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp01=')#currentdp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[1]}" ] && [ ! -z "${vidlinksarray[1]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp02=')#currentdp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[2]}" ] && [ ! -z "${vidlinksarray[2]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp04=')#currentdp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[3]}" ] && [ ! -z "${vidlinksarray[3]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp05=')#currentdp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[4]}" ] && [ ! -z "${vidlinksarray[4]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp11=')#currentdp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[5]}" ] && [ ! -z "${vidlinksarray[5]}" ];then
			sed -i "s#$(cat $conf | grep 'currentdp12=')#currentdp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" $conf
		fi
	fi
	if [ "$target" == "-w" ];then
		if [ ! -z "${vidnamesarray[0]}" ] && [ ! -z "${vidlinksarray[0]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp01=')#currentwp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[1]}" ] && [ ! -z "${vidlinksarray[1]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp02=')#currentwp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[2]}" ] && [ ! -z "${vidlinksarray[2]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp04=')#currentwp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[3]}" ] && [ ! -z "${vidlinksarray[3]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp05=')#currentwp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[4]}" ] && [ ! -z "${vidlinksarray[4]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp11=')#currentwp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[5]}" ] && [ ! -z "${vidlinksarray[5]}" ];then
			sed -i "s#$(cat $conf | grep 'currentwp12=')#currentwp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" $conf
		fi
	fi
	if [ "$target" == "-m" ];then
		if [ ! -z "${vidnamesarray[0]}" ] && [ ! -z "${vidlinksarray[0]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp01=')#currentmp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[1]}" ] && [ ! -z "${vidlinksarray[1]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp02=')#currentmp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[2]}" ] && [ ! -z "${vidlinksarray[2]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp04=')#currentmp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[3]}" ] && [ ! -z "${vidlinksarray[3]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp05=')#currentmp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[4]}" ] && [ ! -z "${vidlinksarray[4]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp11=')#currentmp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[5]}" ] && [ ! -z "${vidlinksarray[5]}" ];then
			sed -i "s#$(cat $conf | grep 'currentmp12=')#currentmp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" $conf
		fi
	fi
	if [ "$target" == "-y" ];then
		if [ ! -z "${vidnamesarray[0]}" ] && [ ! -z "${vidlinksarray[0]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp01=')#currentyp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[1]}" ] && [ ! -z "${vidlinksarray[1]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp02=')#currentyp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[2]}" ] && [ ! -z "${vidlinksarray[2]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp04=')#currentyp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[3]}" ] && [ ! -z "${vidlinksarray[3]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp05=')#currentyp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[4]}" ] && [ ! -z "${vidlinksarray[4]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp11=')#currentyp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" $conf
		fi
		if [ ! -z "${vidnamesarray[5]}" ] && [ ! -z "${vidlinksarray[5]}" ];then
			sed -i "s#$(cat $conf | grep 'currentyp12=')#currentyp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" $conf
		fi
	fi
}

#
## PARAMETERS
#

show=${1}
conf=${2}
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
	conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
fi
ever=${4}
if [ -z "$ever" ] || [ "$ever" == "-" ];then
	ever=1
fi
if [ ! -z $(echo "$ever" | grep -o "^[0-9][0-9]*$") ];then
	:
else
	execerror "$ever - wrong argument"
fi
when=${3}
if [ -z "$when" ];then
	viddate=$(date +"%Y").$(date +"%m").$(date +"%d")
fi
if [ "$when" == "-d" ];then
	d=$(date -d "-${ever} day" +"%d")
	m=$(date +"%m")
	if [ "$(date -d "-${ever} day" +'%m')" != "$(date +'%m')" ];then
		m=$(date -d "-${ever} day" +'%m')
	fi
	y=$(date +"%Y")
	if [ "$(date -d "-${ever} day" +'%Y')" != "$(date +'%Y')" ];then
		y=$(date -d "-${ever} day" +'%Y')
	fi
	viddate=${y}.${m}.${d}
fi
if [ "$when" == "-w" ];then
	w=$(date -d "-${ever} week" +"%V")
	y=$(date +"%Y")
	if [ "$(date -d "-${ever} week" +'%Y')" != "$(date +'%Y')" ];then
		y=$(date -d "-${ever} week" +'%Y')
	fi
	viddate=${y}-w${w}
fi
if [ "$when" == "-m" ];then
	m=$(date -d "-${ever} month" +"%m")
	y=$(date +"%Y")
	if [ "$(date -d "-${ever} month" +'%Y')" != "$(date +'%Y')" ];then
		y=$(date -d "-${ever} month" +'%Y')
	fi
	viddate=${y}.${m}
fi
if [ "$when" == "-y" ];then
	y=$(date -d "-${ever} year" +"%Y")
	viddate=${y}
fi
vidname=${viddate}.mp4
only=${5}

#
## VARIABLES
#

time=$(date +%s)
cd "$(dirname "$(realpath "$0")")"
if [ ! -e ${conf} ];then
	execerror "Not found config file: ${conf}"
else
	getconfig
fi
if [ -z "${logs}" ];then
	logs=/dev/null
elif [ ! -e ${logs} ];then
	touch ${logs}
fi
if ! command -v curl &> /dev/null || ! command -v ffmpeg &> /dev/null || ! command -v xmllint &> /dev/null || ! command -v jq &> /dev/null; then
    execerror "Not found dependencies"
fi

#
## RUN
#

vidpathsarray=()
vidlinksarray=()
vidtgidsarray=()
vidtgcpsarray=()
for name in ${vidnamesarray[*]}
do
	vidmatch=$name\_$vidname
	while read FILE; do
		vidpathsarray+=("${FILE}")
	done < <(find $pathroot -name "*$vidmatch" | sort)
done
for item in "${!vidpathsarray[@]}"
do
	### WORDPRESS UPLOAD VIDEO
	if [ ! "$only" == "--onlytg" ];then
		# This realisation isn't optimal, but it fixes a few issues with large files:
		#    variable=$(base64 $file)                      -> "xrealloc: cannot allocate"
		#    response=$(curl -X POST -d @${file}.xml $url) -> "curl: option -d: out of memory"
		filetype=video/mp4
		echo '<?xml version="1.0"?>
		<methodCall><methodName>wp.uploadFile</methodName>
		<params>
		<param><value>1</value></param>
		<param><value>'"${wpxmlrpcuser}"'</value></param>
		<param><value>'"${wpxmlrpcpass}"'</value></param>
		<param><value><struct>
		<member><name>name</name><value><string>'"$(basename ${vidpathsarray[$item]})"'</string></value></member>
		<member><name>type</name><value><string>'"${filetype}"'</string></value></member>
		<member><name>bits</name><value><base64>' > ${vidpathsarray[$item]}.xml
		base64 --wrap=0 ${vidpathsarray[$item]} >> ${vidpathsarray[$item]}.xml
		echo '</base64></value></member>
		</struct></value></param>
		</params>
		</methodCall>' >> ${vidpathsarray[$item]}.xml
		curl -X POST -T ${vidpathsarray[$item]}.xml $wpxmlrpclink > ${vidpathsarray[$item]}-response.xml \
		&& vidlinksarray+=("$(xmllint --xpath '//member[contains(name,"link")]/value/string/text()' ${vidpathsarray[$item]}-response.xml)") \
		&& vidlinkcodeis=$(curl --output /dev/null --silent --write-out "%{http_code}" ${vidlinksarray[$item]}) \
		|| execerror "${vidpathsarray[$item]}-response.xml"
		if [[ ${vidlinkcodeis} -eq 200 ]];then
			rm ${vidpathsarray[$item]}.xml
			rm ${vidpathsarray[$item]}-response.xml
			echo "$(date +'%Y.%m.%d-%H:%M:%S') sent ${vidpathsarray[$item]} to ${vidlinksarray[$item]}" | tee -a $logs
			echo -e "WORDPRESS UPLOAD VIDEO RESULT:"
			echo -e "item: $item"
			echo -e "type: $filetype"
			echo -e "name: ${vidnamesarray[$item]}"
			echo -e "base: $(basename ${vidpathsarray[$item]})"
			echo -e "path: ${vidpathsarray[$item]}"
			echo -e "link: ${vidlinksarray[$item]}"
			echo -e "code: $vidlinkcodeis"
		else
			vidlinksarray[$item]=''
		fi
	fi
	### TELEGRAM SEND/UPLOAD VIDEO
	if [ ! "$only" == "--onlywp" ];then
		videofullpath=${vidpathsarray[$item]}
		vidcompressed=$(echo ${vidpathsarray[$item]} | sed -e 's/.mp4/-compressed.mp4/g')
		if [ -n "$(find "$videofullpath" -prune -size +51380224c)" ];then
			duration=$(ffprobe -i "$videofullpath" -show_entries format=duration -v quiet -of csv="p=0" | cut -d'.' -f 1) \
			&& ffmpeg -i "$videofullpath" -c:v libx264 -b:v $((49*8*1000/$duration))k -vf "scale=960:540,fps=25,format=yuv420p" -preset veryslow "$vidcompressed" -y -loglevel quiet -stats \
			&& videofullpath=$vidcompressed \
			|| execerror "ffmpeg convert ${videofullpath} to ${vidcompressed}"
		fi
		response=$(curl -s -F chat_id=$telegramchatid \
				   -F video=@${videofullpath} \
				   -F caption="$(echo $(basename ${videofullpath}) | sed -e 's/.mp4//g')" \
				   $telegramapiurl/sendVideo) \
		&& curl -s -F chat_id=$telegramchatid \
			   -F message_id=$(echo $response | jq -r '.result.message_id') \
			   $telegramapiurl/deleteMessage \
		&& vidtgidsarray+=("$(echo $response | jq -r '.result.video.file_id')") \
		&& vidtgcpsarray+=("$(echo $response | jq -r '.result.caption')") \
		&& echo "$(date +'%Y.%m.%d-%H:%M:%S') sent ${videofullpath} to ${vidtgidsarray[$item]} Telegram file ID" | tee -a $logs \
		|| execerror "sent ${videofullpath} to $telegramchatid Telegram Chat ID"
		if [ -e $vidcompressed ];then
			rm $vidcompressed
		fi
	fi
done
### TELEGRAM SEND MEDIAGROUP
if [ ! "$only" == "--onlywp" ];then
	response=$(curl -s -F chat_id=$telegramchatid \
			   -F media='[
			   {"type":"photo","media":"'${tgpreviewlink}'","caption":"period: '${viddate}'\nsource: '${tgpreviewtext}'\nstream: '${youtubelink}'"},
			   {"type":"video","media":"'${vidtgidsarray[0]}'"},
			   {"type":"video","media":"'${vidtgidsarray[1]}'"},
			   {"type":"video","media":"'${vidtgidsarray[2]}'"},
			   {"type":"video","media":"'${vidtgidsarray[3]}'"},
			   {"type":"video","media":"'${vidtgidsarray[4]}'"},
			   {"type":"video","media":"'${vidtgidsarray[5]}'"}]' \
			   -H "Content-Type:multipart/form-data" \
			   $telegramapiurl/sendMediaGroup)
fi
### WORDPRESS UPDATE PAGE
if [ ! "$only" == "--onlytg" ];then
	setconfig
	getconfig
	xml=$(cat ./${wptemplateis})

	xml=${xml//wpeditpageid/${wpeditpageid}}
	xml=${xml//wpxmlrpcuser/${wpxmlrpcuser}}
	xml=${xml//wpxmlrpcpass/${wpxmlrpcpass}}

	xml=${xml//youtubelink/${youtubelink}}

	xml=${xml//"currentdp01[0]"/${currentdp01[0]}}
	xml=${xml//"currentdp01[1]"/${currentdp01[1]}}
	xml=${xml//"currentdp02[0]"/${currentdp02[0]}}
	xml=${xml//"currentdp02[1]"/${currentdp02[1]}}
	xml=${xml//"currentdp04[0]"/${currentdp04[0]}}
	xml=${xml//"currentdp04[1]"/${currentdp04[1]}}
	xml=${xml//"currentdp05[0]"/${currentdp05[0]}}
	xml=${xml//"currentdp05[1]"/${currentdp05[1]}}
	xml=${xml//"currentdp11[0]"/${currentdp11[0]}}
	xml=${xml//"currentdp11[1]"/${currentdp11[1]}}
	xml=${xml//"currentdp12[0]"/${currentdp12[0]}}
	xml=${xml//"currentdp12[1]"/${currentdp12[1]}}

	xml=${xml//"currentwp01[0]"/${currentwp01[0]}}
	xml=${xml//"currentwp01[1]"/${currentwp01[1]}}
	xml=${xml//"currentwp02[0]"/${currentwp02[0]}}
	xml=${xml//"currentwp02[1]"/${currentwp02[1]}}
	xml=${xml//"currentwp04[0]"/${currentwp04[0]}}
	xml=${xml//"currentwp04[1]"/${currentwp04[1]}}
	xml=${xml//"currentwp05[0]"/${currentwp05[0]}}
	xml=${xml//"currentwp05[1]"/${currentwp05[1]}}
	xml=${xml//"currentwp11[0]"/${currentwp11[0]}}
	xml=${xml//"currentwp11[1]"/${currentwp11[1]}}
	xml=${xml//"currentwp12[0]"/${currentwp12[0]}}
	xml=${xml//"currentwp12[1]"/${currentwp12[1]}}

	xml=${xml//"currentmp01[0]"/${currentmp01[0]}}
	xml=${xml//"currentmp01[1]"/${currentmp01[1]}}
	xml=${xml//"currentmp02[0]"/${currentmp02[0]}}
	xml=${xml//"currentmp02[1]"/${currentmp02[1]}}
	xml=${xml//"currentmp04[0]"/${currentmp04[0]}}
	xml=${xml//"currentmp04[1]"/${currentmp04[1]}}
	xml=${xml//"currentmp05[0]"/${currentmp05[0]}}
	xml=${xml//"currentmp05[1]"/${currentmp05[1]}}
	xml=${xml//"currentmp11[0]"/${currentmp11[0]}}
	xml=${xml//"currentmp11[1]"/${currentmp11[1]}}
	xml=${xml//"currentmp12[0]"/${currentmp12[0]}}
	xml=${xml//"currentmp12[1]"/${currentmp12[1]}}

	xml=${xml//"currentyp01[0]"/${currentyp01[0]}}
	xml=${xml//"currentyp01[1]"/${currentyp01[1]}}
	xml=${xml//"currentyp02[0]"/${currentyp02[0]}}
	xml=${xml//"currentyp02[1]"/${currentyp02[1]}}
	xml=${xml//"currentyp04[0]"/${currentyp04[0]}}
	xml=${xml//"currentyp04[1]"/${currentyp04[1]}}
	xml=${xml//"currentyp05[0]"/${currentyp05[0]}}
	xml=${xml//"currentyp05[1]"/${currentyp05[1]}}
	xml=${xml//"currentyp11[0]"/${currentyp11[0]}}
	xml=${xml//"currentyp11[1]"/${currentyp11[1]}}
	xml=${xml//"currentyp12[0]"/${currentyp12[0]}}
	xml=${xml//"currentyp12[1]"/${currentyp12[1]}}

	xml=${xml//wpedituserid/${wpedituserid}}
	xml=${xml//wppageauthor/${wppageauthor}}
	xml=${xml//wppagelinkis/${wppagelinkis}}
	xml=${xml//wpeditdateis/${wpeditdateis}}

	response=$(curl -d "$xml" $wpxmlrpclink)
	if echo "$response" | grep -q '<boolean>1</boolean>';then
		addtologs "update $(echo "$xml" | grep 'Link' | cut -d'>' -f 6 | cut -d'<' -f 1))"
	else
		echo $response | xmllint --format - > $pathroot/wp-page${wpeditpageid}-response.xml 2>/dev/null
		execerror "$pathroot/wp-page${wpeditpageid}-response.xml"
	fi
fi
execquite