#!/bin/bash
#
# (c) 2014 Timothy Pearson
# All Rights Reserved
#
# WARNING
# This script is not complete!
# Hand editing will likely be required after running to ensure all icon names have been updated
# See http://trinity.etherpad.trinitydesktop.org/60 for a mapping of old KDE3 names to modern XDG names

WORKDIR=$PWD

function rename_files {
	echo '#!/bin/bash' > /tmp/rename_mime_files
	echo '' >> /tmp/rename_mime_files
	echo 'cd $1' >> /tmp/rename_mime_files
	echo '' >> /tmp/rename_mime_files
	echo 'BASEDIR=${PWD##*/}' >> /tmp/rename_mime_files
	echo "ORIGNAME='$1'" >> /tmp/rename_mime_files
	echo "NEWNAME='$2'" >> /tmp/rename_mime_files
	echo '' >> /tmp/rename_mime_files
	echo 'if [[ "$BASEDIR" == ".git" ]]; then' >> /tmp/rename_mime_files
	echo '        echo "Ignoring .git directory"' >> /tmp/rename_mime_files
	echo '        exit 0' >> /tmp/rename_mime_files
	echo 'fi' >> /tmp/rename_mime_files
	echo '' >> /tmp/rename_mime_files
	echo 'ls -d * | sed "s/\(.*\)mime-$ORIGNAME\.png\(.*\)$/git mv \"&\" \"\1mime-$NEWNAME\.png\2\"/" | grep mv | sh' >> /tmp/rename_mime_files
	echo 'ls -d * | sed "s/$ORIGNAME\.png\(.*\)$/git mv \"&\" \"$NEWNAME\.png\1\"/" | grep mv | sh' >> /tmp/rename_mime_files
	chmod 755 /tmp/rename_mime_files

	find ./ -type d -exec /tmp/rename_mime_files {} \; &> /dev/null

	rm /tmp/rename_mime_files
}

function convert_files {
	cd ~/tde-git/tde/main

	echo "==================================================="
	echo "Converting $1..."

	find ./ -type f -iname "*.c*" -not -iwholename '*.git*' -exec sed -i "s/IconSet(\"$1\"/IconSet(\"$2\"/g" {} \;
	find ./ -type f -iname "*.c*" -not -iwholename '*.git*' -exec sed -i "s/IconSet( \"$1\"/IconSet( \"$2\"/g" {} \;

	find ./ -type f -iname "*.c*" -not -iwholename '*.git*' -exec sed -i "s/Icon(\"$1\"/Icon(\"$2\"/g" {} \;
	find ./ -type f -iname "*.c*" -not -iwholename '*.git*' -exec sed -i "s/Icon( \"$1\"/Icon( \"$2\"/g" {} \;

	find ./ -type f -iname "*.ui*" -not -iwholename '*.git*' -exec sed -i "s/<iconset>\"$1\"/<iconset>\"$2\"/g" {} \;

	find ./ -type f -iname "*.directory" -not -iwholename '*.git*' -exec sed -i "s/Icon=$1/Icon=$2/g" {} \;
	find ./ -type f -iname "*.desktop" -not -iwholename '*.git*' -exec sed -i "s/Icon=$1/Icon=$2/g" {} \;
	find ./ -type f -iname "*.protocol" -not -iwholename '*.git*' -exec sed -i "s/Icon=$1/Icon=$2/g" {} \;

	find ./ -type f -iname "*.rc" -not -iwholename '*.git*' -exec sed -i "s/icon=\"$1\"/icon=\"$2\"/g" {} \;

	echo "==================================================="
	echo "Renaming $1..."
	cd $WORKDIR/tdelibs/pics
	rename_files $1 $2
	cd $WORKDIR/tdeartwork/IconThemes
	rename_files $1 $2
	cd $WORKDIR/tdeaccessibility/IconThemes
	rename_files $1 $2
	cd $WORKDIR
	echo "==================================================="

	echo "Still found in files:"
	echo "==================================================="
	find . -not -iwholename '*.git*' | xargs grep "\"$1\"" -sl
	echo "==================================================="
	echo ""
}

convert_files "sound" "audio-x-generic"
convert_files "font" "font-x-generic"
convert_files "image" "image-x-generic"
convert_files "html" "text-html"
convert_files "document" "text-x-generic"
convert_files "document2" "text-x-generic-template"
convert_files "shellscript" "text-x-script"
convert_files "video" "video-x-generic"
convert_files "vcard" "x-office-address-book"
convert_files "vcalendar" "x-office-calendar"
convert_files "kword_kwd" "x-office-document"
convert_files "applix" "application-x-applix-word"
convert_files "ascii" "text-vnd\.tde\.ascii"
convert_files "binary" "application-octet-stream"
convert_files "bt" "application-x-bittorrent"
convert_files "cdimage" "application-x-cd-image"
convert_files "cdr" "application-x-cdr"
convert_files "cdtrack" "application-x-cda"
convert_files "colorscm" "application-x-kcsrc"
convert_files "core" "application-x-core"
convert_files "database" "application-vnd\.oasis\.opendocument\.database"
convert_files "deb" "application-x-deb"
convert_files "drawing" "application-vnd\.oasis\.opendocument\.graphics"
convert_files "dvi" "application-x-lyx"
convert_files "empty" "application-x-zerosize"
convert_files "exec" "application-x-executable"
convert_files "exec_wine" "application-x-mswinurl"
convert_files "file_locked" "application-vnd\.tde\.file\.locked"
convert_files "file_temporary" "application-vnd\.tde\.file\.temporary"
convert_files "font_bitmap" "application-x-font-snf"
convert_files "font_truetype" "application-x-font-ttf"
convert_files "font_type1" "application-x-font-type1"
convert_files "gettext" "application-x-gettext"
convert_files "gf" "application-x-tex-gf"
convert_files "info" "application-vnd\.tde\.info"
convert_files "karbon_karbon" "application-x-karbon"
convert_files "kchart_chrt" "application-x-kchart"
convert_files "kexi_kexi" "application-x-kexi"
convert_files "kformula_kfo" "application-x-kformula"
convert_files "kivio_flw" "application-x-kivio"
convert_files "kpresenter_kpr" "application-x-kpresenter"
convert_files "krita_kra" "application-x-krita"
convert_files "kspread_ksp" "application-x-kspread"
convert_files "kugar_kud" "application-x-kugar"
convert_files "log" "text-x-log"
convert_files "make" "text-x-makefile"
convert_files "man" "application-x-troff-man"
convert_files "metafont" "application-x-metafont"
convert_files "midi" "audio-midi"
convert_files "misc" "application-vnd\.tde\.misc"
convert_files "netscape_doc" "application-x-netscape"
convert_files "pdf" "application-pdf"
convert_files "pk" "application-x-tex-pk"
convert_files "postscript" "application-postscript"
convert_files "presentation" "application-vnd\.oasis\.opendocument\.presentation"
convert_files "quicktime" "video-x-quicktime"
convert_files "readme" "text-x-readme"
convert_files "recycled" "application-x-trash"
convert_files "resource" "application-vnd\.tde\.resource"
convert_files "rpm" "application-x-rpm"
convert_files "rtf" "text-rtf"
convert_files "soffice" "application-x-soffice"
convert_files "source_c" "text-x-csrc"
convert_files "source_cpp" "text-x-c++src"
convert_files "source_f" "text-x-fortran"
convert_files "source_h" "text-x-hsrc"
convert_files "source_java" "text-x-java"
convert_files "source_j" "text-x-jsrc"
convert_files "source_l" "text-x-lsrc"
convert_files "source_moc" "text-x-mocsrc"
convert_files "source_o" "text-x-osrc"
convert_files "source_php" "text-x-php"
convert_files "source_pl" "text-x-perl"
convert_files "source" "text-x-src"
convert_files "source_p" "text-x-psrc"
convert_files "source_py" "text-x-python"
convert_files "source_s" "text-x-asm"
convert_files "source_y" "text-x-ysrc"
convert_files "spreadsheet" "application-vnd\.oasis\.opendocument\.spreadsheet"
convert_files "tar" "application-x-tar"
convert_files "tdemultiple" "application-vnd\.tde\.tdemultiple"
convert_files "template_source" "application-vnd\.tde\.template_source"
convert_files "tex" "text-x-tex"
convert_files "tgz" "application-x-tarz"
convert_files "txt2" "application-vnd\.tde\.text\.alt"
convert_files "txt" "text-plain"
convert_files "vectorgfx" "image-svg+xml"
convert_files "widget_doc" "application-x-designer"
convert_files "wordprocessing" "application-vnd\.oasis\.opendocument\.text"
convert_files "zip" "application-vnd\.tde\.overlay\.zip"
