#!/bin/bash

  DBNAME=tracdb		# your postgres database you used for trac
  DBUSER=tracuser	# your database user

    TRAC=/home/trac	# where is the parent directory of your trac instance

MOINMOIN=/home/moin	# where is your moinmoin instance
 WWWUSER=www-data	# uid of webserver
WWWGROUP=www-data	# gid of webserver


cd $MOINMOIN/data/pages


for i in $(psql -U $DBUSER $DBNAME -c "select name from wiki where author != 'trac' group by name"|grep --invert-match "^ *name *$" | grep --invert-match '^----*$' | grep --invert-match \( )
do
	text=$(psql -U $DBUSER $DBNAME -c "select text from wiki where name='$i' order by version desc limit 1"|grep --invert-match "^ *text *$" | grep --invert-match '^--------------*$' | grep --invert-match '([^)]*)' )
	echo "$i"
	id=$(echo "$i"|sed -e 's/\//\(2f\)/g' -e 's/-/\(2d\)/g')
	mkdir -p "$id/revisions"
	echo 00000001 > "$id/current"
	echo "$text" | sed -e 's/^ *=/=/g' -e 's/\[wiki:\(.*\)\]/[:\1]/g' > "$id/revisions/00000001"
done

for i in $(psql -U $DBUSER $DBNAME -c "select id,filename from attachment where author != 'trac'" |grep --invert-match "^ *id *| *filename *$" | grep --invert-match '^----*+---*$' | grep --invert-match \( | tr -d ' ' )
do
	ii=$(echo $i  | cut -d \| -f 1)
	id=$(echo $ii | sed -e 's/\//\(2f\)/g' -e 's/-/\(2d\)/g')
	filename=$(echo $i|cut -d \| -f 2)
	echo "$ii"
	mkdir -p "$id/attachments"
	cp "$TRAC/attachments/wiki/$ii/$filename" "$id/attachments"
	case "$filename" in
		*.jpg | *.png | *.bmp | *.gif )	echo "|| $filename || [[ImageLink($filename,,width=200)]] ||" >> "$id/revisions/00000001" ;;
		* )				echo "|| attachment:$filename ||" >> "$id/revisions/00000001" ;;
	esac
done

chown $WWWUSER:$WWWGROUP * -R



