#! /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:
#   - curl
#   - ffmpeg
#   - libxml2-utils
#   - jq
#
# PARAMETERS:
#   1: "qn" - execution without pauses
#   2: custom configuration file path
#   3: periods: '' - today | '-d' - yesterday | '-w' - last week | '-m' - last month | '-y' - last year
#   4: period multiplier: '' - 1 day|week|month|year
#   5: publishing '--onlytg' - only to Telegram | '--onlywp' - only to Wordpress
#
# FUNCTIONS:
#

#######################################
# Print message and add to log.
# Globals:
#   logs
# Arguments:
#   1: message to print and logging
#######################################
addtologs() {
  echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
}

#######################################
# Waiting for press [ENTER].
# Globals:
#   None
# Arguments:
#   None
#######################################
execpause() {
  read -r -p "Press [ENTER] to continue... "
}

#######################################
# Exit procedure.
# Globals:
#   show
# Arguments:
#   None
#######################################
execquite() {
  addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
  if [ "${show}" != "qn" ]; then
  execpause
  fi
  exit
}

#######################################
# Error exit procedure with Telegram notification.
# Globals:
#   telegramapiurl
#   telegramchatid
# Arguments:
#   1: message to print and logging
#######################################
execerror() {
  addtologs "error: $1"
  curl -s -X POST "${telegramapiurl}/sendMessage" \
  -d "chat_id=${telegramchatid}" \
  -d "text=$(basename -s .sh "$0") error: $1" \
  >> /dev/null 2>&1
  execquite
}

#######################################
# Parsing config file and creating global vars.
# Globals:
#   None
# Arguments:
#   None
#######################################
getconfig() {
  logs=$(grep "logs=" "${conf}" | cut -d= -f2)
  pathroot=$(grep "pathroot=" "${conf}" | cut -d= -f2)
  IFS=" " read -r -a vidnamesarray <<< "$(grep "vidnamesarray=" "${conf}" | cut -d= -f2)"
  telegramapiurl=$(grep "telegramapiurl=" "${conf}" | cut -d= -f2)
  telegramchatid=$(grep "telegramchatid=" "${conf}" | cut -d= -f2)
  tgpreviewlink=$(grep "tgpreviewlink=" "${conf}" | cut -d= -f2)
  tgpreviewtext=$(grep "tgpreviewtext=" "${conf}" | cut -d= -f2)
  wpxmlrpclink=$(grep "wpxmlrpclink=" "${conf}" | cut -d= -f2)
  wpxmlrpcuser=$(grep "wpxmlrpcuser=" "${conf}" | cut -d= -f2)
  wpxmlrpcpass=$(grep "wpxmlrpcpass=" "${conf}" | cut -d= -f2)
  wppageauthor=$(grep "wppageauthor=" "${conf}" | cut -d= -f2)
  wppagelinkis=$(grep "wppagelinkis=" "${conf}" | cut -d= -f2)
  wpeditpageid=$(grep "wpeditpageid=" "${conf}" | cut -d= -f2)
  wpedituserid=$(grep "wpedituserid=" "${conf}" | cut -d= -f2)
  wpeditdateis=$(grep "wpeditdateis=" "${conf}" | cut -d= -f2)
  wptemplateis=$(grep "wptemplateis=" "${conf}" | cut -d= -f2)
  youtubelink=$(grep "youtubelink=" "${conf}" | cut -d= -f2)

  IFS=" " read -r -a defaultdp01 <<< "$(grep "defaultdp01=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp01 <<< "$(grep "defaultwp01=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp01 <<< "$(grep "defaultmp01=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp01 <<< "$(grep "defaultyp01=" "${conf}" | cut -d= -f2)"

  IFS=" " read -r -a defaultdp02 <<< "$(grep "defaultdp02=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp02 <<< "$(grep "defaultwp02=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp02 <<< "$(grep "defaultmp02=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp02 <<< "$(grep "defaultyp02=" "${conf}" | cut -d= -f2)"

  IFS=" " read -r -a defaultdp04 <<< "$(grep "defaultdp04=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp04 <<< "$(grep "defaultwp04=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp04 <<< "$(grep "defaultmp04=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp04 <<< "$(grep "defaultyp04=" "${conf}" | cut -d= -f2)"

  IFS=" " read -r -a defaultdp05 <<< "$(grep "defaultdp05=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp05 <<< "$(grep "defaultwp05=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp05 <<< "$(grep "defaultmp05=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp05 <<< "$(grep "defaultyp05=" "${conf}" | cut -d= -f2)"

  IFS=" " read -r -a defaultdp11 <<< "$(grep "defaultdp11=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp11 <<< "$(grep "defaultwp11=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp11 <<< "$(grep "defaultmp11=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp11 <<< "$(grep "defaultyp11=" "${conf}" | cut -d= -f2)"

  IFS=" " read -r -a defaultdp12 <<< "$(grep "defaultdp12=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultwp12 <<< "$(grep "defaultwp12=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultmp12 <<< "$(grep "defaultmp12=" "${conf}" | cut -d= -f2)"
  IFS=" " read -r -a defaultyp12 <<< "$(grep "defaultyp12=" "${conf}" | cut -d= -f2)"

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

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

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

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

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

  IFS=" " read -r -a currentdp12 <<< "$(grep "currentdp12=" "${conf}" | cut -d= -f2)"
  if [ -z "${currentdp12[1]}" ]; then
    currentdp12=${defaultdp12[*]}
  fi
  IFS=" " read -r -a currentwp12 <<< "$(grep "currentwp12=" "${conf}" | cut -d= -f2)"
  if [ -z "${currentwp12[1]}" ]; then
    currentwp12=${defaultwp12[*]}
  fi
  IFS=" " read -r -a currentmp12 <<< "$(grep "currentmp12=" "${conf}" | cut -d= -f2)"
  if [ -z "${currentmp12[1]}" ]; then
    currentmp12=${defaultmp12[*]}
  fi
  IFS=" " read -r -a currentyp12 <<< "$(grep "currentyp12=" "${conf}" | cut -d= -f2)"
  if [ -z "${currentyp12[1]}" ]; then
    currentyp12=${defaultyp12[*]}
  fi
}

#######################################
# Writing changes to configuration file.
# Globals:
#   conf
#   when
#   vidnamesarray
#   vidlinksarray
# Arguments:
#   None
#######################################
setconfig() {
  if [ -z "${when}" ] || [ "${when}" == "-d" ]; then
    if [ -n "${vidnamesarray[0]}" ] && [ -n "${vidlinksarray[0]}" ]; then
      sed -i "s#$(grep 'currentdp01=' "${conf}")#currentdp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[1]}" ] && [ -n "${vidlinksarray[1]}" ]; then
      sed -i "s#$(grep 'currentdp02=' "${conf}")#currentdp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[2]}" ] && [ -n "${vidlinksarray[2]}" ]; then
      sed -i "s#$(grep 'currentdp04=' "${conf}")#currentdp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[3]}" ] && [ -n "${vidlinksarray[3]}" ]; then
      sed -i "s#$(grep 'currentdp05=' "${conf}")#currentdp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[4]}" ] && [ -n "${vidlinksarray[4]}" ]; then
      sed -i "s#$(grep 'currentdp11=' "${conf}")#currentdp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[5]}" ] && [ -n "${vidlinksarray[5]}" ]; then
      sed -i "s#$(grep 'currentdp12=' "${conf}")#currentdp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" "${conf}"
    fi
  fi
  if [ "${when}" == "-w" ]; then
    if [ -n "${vidnamesarray[0]}" ] && [ -n "${vidlinksarray[0]}" ]; then
      sed -i "s#$(grep 'currentwp01=' "${conf}")#currentwp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[1]}" ] && [ -n "${vidlinksarray[1]}" ]; then
      sed -i "s#$(grep 'currentwp02=' "${conf}")#currentwp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[2]}" ] && [ -n "${vidlinksarray[2]}" ]; then
      sed -i "s#$(grep 'currentwp04=' "${conf}")#currentwp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[3]}" ] && [ -n "${vidlinksarray[3]}" ]; then
      sed -i "s#$(grep 'currentwp05=' "${conf}")#currentwp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[4]}" ] && [ -n "${vidlinksarray[4]}" ]; then
      sed -i "s#$(grep 'currentwp11=' "${conf}")#currentwp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[5]}" ] && [ -n "${vidlinksarray[5]}" ]; then
      sed -i "s#$(grep 'currentwp12=' "${conf}")#currentwp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" "${conf}"
    fi
  fi
  if [ "${when}" == "-m" ]; then
    if [ -n "${vidnamesarray[0]}" ] && [ -n "${vidlinksarray[0]}" ]; then
      sed -i "s#$(grep 'currentmp01=' "${conf}")#currentmp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[1]}" ] && [ -n "${vidlinksarray[1]}" ]; then
      sed -i "s#$(grep 'currentmp02=' "${conf}")#currentmp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[2]}" ] && [ -n "${vidlinksarray[2]}" ]; then
      sed -i "s#$(grep 'currentmp04=' "${conf}")#currentmp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[3]}" ] && [ -n "${vidlinksarray[3]}" ]; then
      sed -i "s#$(grep 'currentmp05=' "${conf}")#currentmp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[4]}" ] && [ -n "${vidlinksarray[4]}" ]; then
      sed -i "s#$(grep 'currentmp11=' "${conf}")#currentmp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[5]}" ] && [ -n "${vidlinksarray[5]}" ]; then
      sed -i "s#$(grep 'currentmp12=' "${conf}")#currentmp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" "${conf}"
    fi
  fi
  if [ "${when}" == "-y" ]; then
    if [ -n "${vidnamesarray[0]}" ] && [ -n "${vidlinksarray[0]}" ]; then
      sed -i "s#$(grep 'currentyp01=' "${conf}")#currentyp01=${vidnamesarray[0]} ${vidlinksarray[0]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[1]}" ] && [ -n "${vidlinksarray[1]}" ]; then
      sed -i "s#$(grep 'currentyp02=' "${conf}")#currentyp02=${vidnamesarray[1]} ${vidlinksarray[1]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[2]}" ] && [ -n "${vidlinksarray[2]}" ]; then
      sed -i "s#$(grep 'currentyp04=' "${conf}")#currentyp04=${vidnamesarray[2]} ${vidlinksarray[2]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[3]}" ] && [ -n "${vidlinksarray[3]}" ]; then
      sed -i "s#$(grep 'currentyp05=' "${conf}")#currentyp05=${vidnamesarray[3]} ${vidlinksarray[3]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[4]}" ] && [ -n "${vidlinksarray[4]}" ]; then
      sed -i "s#$(grep 'currentyp11=' "${conf}")#currentyp11=${vidnamesarray[4]} ${vidlinksarray[4]}#" "${conf}"
    fi
    if [ -n "${vidnamesarray[5]}" ] && [ -n "${vidlinksarray[5]}" ]; then
      sed -i "s#$(grep 'currentyp12=' "${conf}")#currentyp12=${vidnamesarray[5]} ${vidlinksarray[5]}#" "${conf}"
    fi
  fi
}

#
# VARIABLES:
#

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 grep -q -o "^[0-9][0-9]*$" <<< "${ever}"; 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

time=$(date +%s)
cd "$(dirname "$(realpath "$0")")" || execerror
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

#
# MAIN:
#

vidpathsarray=()
vidlinksarray=()
vidtgidsarray=()
vidtgcpsarray=()
for name in "${vidnamesarray[@]}"; do
  vidmatch="${name}_${vidname}"
  while read -r 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"
    response="${vidpathsarray[$item]}-response.xml"
    if curl -X POST -T "${vidpathsarray[$item]}.xml" "${wpxmlrpclink}" > "${response}"; then
      vidlinksarray+=("$(xmllint --xpath '//member[contains(name,"link")]/value/string/text()' "${response}")")
      vidlinkcodeis=$(curl --output /dev/null --silent --write-out "%{http_code}" "${vidlinksarray[$item]}")
    else
      execerror "${response}"
    fi
    if [[ ${vidlinkcodeis} -eq 200 ]]; then
      rm "${vidpathsarray[$item]}.xml"
      rm "${response}"
      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=${videofullpath//".mp4"/"-compressed.mp4"}
    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)
      if 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; then
        videofullpath=${vidcompressed}
      else
        execerror "ffmpeg convert ${videofullpath} to ${vidcompressed}"
      fi
    fi
    videobasename=$(basename "${videofullpath}")
    videobasename=${videobasename//".mp4"/""}
    response=$(curl -s \
      -F "chat_id=${telegramchatid}" \
      -F "video=@${videofullpath}" \
      -F "caption=${videobasename}" \
      "${telegramapiurl}/sendVideo")
    if curl -s -F "chat_id=${telegramchatid}" \
      -F "message_id=$(echo "${response}" | jq -r '.result.message_id')" \
      "${telegramapiurl}/deleteMessage"; then
      vidtgidsarray+=("$(echo "${response}" | jq -r '.result.video.file_id')")
      vidtgcpsarray+=("$(echo "${response}" | jq -r '.result.caption')")
      addtologs "sent ${videofullpath} to ${vidtgidsarray[$item]} Telegram file ID"
    else
      execerror "sent ${videofullpath} to ${telegramchatid} Telegram Chat ID"
    fi
    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 $(grep 'Link' "${xml}" | 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
