#!/bin/ksh
# Collection of Mismatched ID Old and New WordPress Blog Postings - RJM Programming - April, 2015

if [ -z "$1" ]; then
  echo 'Enter host:'
  read hostname
  echo 'Enter username:'
  read username
  echo 'Enter password:'
  stty -echo
  read password
  stty echo
  echo $password > /etc/msans.ans
  $0 $hostname $username "." # < /etc/msans.ans
  exit
elif [ -z "$2" ]; then
  hostname=$1
  echo 'Enter username:'
  read username
  echo 'Enter password:'
  stty -echo
  read password
  stty echo
  echo $password > /etc/msans.ans
  $0 $hostname $username "." # < /etc/msans.ans
  exit
elif [ -z "$3" ]; then
  hostname=$1
  username=$2
  echo 'Enter password:'
  stty -echo
  read password
  stty echo
  echo $password > /etc/msans.ans
elif [ "$3" == "." ]; then
  hostname=$1
  username=$2
  echo 'Enter password:'
  if [ -f "/etc/msans.ans" ]; then
    password="`cat /etc/msans.ans`"
  else
    stty -echo
    read password
    stty echo
    echo $password > /etc/msans.ans
  fi
else
  hostname=$1
  username=$2
  password=$3
  echo $password > /etc/msans.ans
fi

for goes in 1 #2 
do
 mysql -D itblog -h $hostname -u $username --delimiter=~ -e "SELECT t1.ID, t2.ID, t1.post_title, t2.post_title, substr(t1.guid, instr(t1.guid, '/ITblog/')) as t1g, substr(t2.guid, instr(t2.guid, '/wordpress/')) as t2g, 'mysql -D itblog -h $hostname -u $username -e @UPDATE rjmprogr_wordpress.wp_posts SET id=', ', guid=replace(guid,^','^,^','^) WHERE id=',' and post_title=^','^;@' FROM itblog.wps_posts t1 JOIN rjmprogr_wordpress.wp_posts t2 ON t2.post_title = t1.post_title and t2.post_status in ('publish','future') and t1.post_status in ('publish','future') WHERE t2.ID != t1.ID;" < /etc/msans.ans > /tmp/msans.ans 2> /dev/null
 if [ -f "/tmp/msans.ans" ]; then
  echo "#!/bin/ksh" > /tmp/msans.ksh
  if [ "$goes" == "1" ]; then
    tail -n +2 /tmp/msans.ans | tr '@' '"' | tr "^" "'" |  awk -F "\t" '{ 
                                                     print $7,$1,$8,$6,$9,$5,$10,$2,$11,$3,$12 
                                                   }' | sed '/\/ITblog\//s//\/wordpress\//g' | sed '/$/s// \< \/etc\/msans.ans/g' | sed "/ '/s//'/g" | sed "/' /s//'/g" >> /tmp/msans.ksh
  fi
  chmod 755 /tmp/msans.ksh
  ksh /tmp/msans.ksh > /tmp/msans.ans
  cat /tmp/msans.ans
  if [ -f "/etc/msans.ans" ]; then
   echo "y" > /etc/msans.ans < /etc/msans.ans
   rm -f /etc/msans.ans < /etc/msans.ans
  fi
 fi
done
exit
