#!/bin/bash

IFS="\|"
cat comments_wp | while read -r post_id timestamp author_ip author author_url_codificato content
do
	author_email=""
	author_url=""
	query="insert into wp_comments (comment_post_id,comment_author,comment_author_email,comment_author_url,comment_author_ip,comment_date,comment_date_gmt,comment_content,comment_karma,comment_approved,comment_agent,comment_type,comment_parent,user_id) VALUES ($post_id,'$author'"

	possible_url=`echo $author_url_codificato | sed s/"%3D"/"\="/g | base64 -d `
	if [ ! -z "$possible_url" ]
	then
		if [ `expr index $possible_url "@" ` -gt 0 ]
		then
			author_email=$possible_url
		else
			author_email="invalid@invalid.invalid"
			author_url="http://$possible_url"
		fi
	else
		author_email="invalid@invalid.invalid"
	fi

	query="$query,'$author_email','$author_url','$author_ip'"

	data=`date -d "1970-01-01 UTC $timestamp seconds" +"%Y-%m-%d %T"`
	dataGMT="CONVERT_TZ('$data','+01:00','+00:00')"
	
	contenuto=`php -r "echo urldecode('$content');" | sed s/"'"/"''"/g `
	
	query="$query,'$data',$dataGMT,'$contenuto',0,1,'StM_av_import','',0"
	
	if [ $author = "StM" ]
	then
		query="$query,1"
	else
		query="$query,0"
	fi

	query="$query); update wp_posts set comment_count=comment_count+1 where ID=$post_id;"

	echo $query
	echo
done

IFS="\ "

