#!/bin/bash
#
# (c) 2015 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_device_files
	echo '' >> /tmp/rename_device_files
	echo 'cd $1' >> /tmp/rename_device_files
	echo '' >> /tmp/rename_device_files
	echo 'BASEDIR=${PWD##*/}' >> /tmp/rename_device_files
	echo "ORIGNAME='$1'" >> /tmp/rename_device_files
	echo "NEWNAME='$2'" >> /tmp/rename_device_files
	echo '' >> /tmp/rename_device_files
	echo 'if [[ "$BASEDIR" == ".git" ]]; then' >> /tmp/rename_device_files
	echo '        echo "Ignoring .git directory"' >> /tmp/rename_device_files
	echo '        exit 0' >> /tmp/rename_device_files
	echo 'fi' >> /tmp/rename_device_files
	echo '' >> /tmp/rename_device_files
	echo 'ls -d * | sed "s/\(.*\)device-$ORIGNAME\.png\(.*\)$/git mv \"&\" \"\1device-$NEWNAME\.png\2\"/" | grep mv | sh' >> /tmp/rename_device_files
	echo 'ls -d * | sed "s/$ORIGNAME\.png\(.*\)$/git mv \"&\" \"$NEWNAME\.png\1\"/" | grep mv | sh' >> /tmp/rename_device_files
	chmod 755 /tmp/rename_device_files

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

	rm /tmp/rename_device_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 "system" "computer"
convert_files "hdd_unmount" "drive-harddisk"
convert_files "hdd_mount" "drive-harddisk-mounted"
convert_files "hdd_unmount_encrypt" "drive-harddisk-encrypted"
convert_files "hdd_unmount_decrypt" "drive-harddisk-decrypted"
convert_files "hdd_mount_decrypt" "drive-harddisk-decrypted-mounted"
#convert_files "hicolor/32x32/apps/kxkb.png" "input-keyboard"
#convert_files "mouse" "input-mouse"
#convert_files "tablet" "input-tablet"
convert_files "sd_mmc_unmount" "media-flash-sd_mmc"
convert_files "sd_mmc_mount" "media-flash-sd_mmc-mounted"
convert_files "sd_mmc_unmount_encrypt" "media-flash-sd_mmc-encrypted"
convert_files "sd_mmc_unmount_decrypt" "media-flash-sd_mmc-decrypted"
convert_files "sd_mmc_mount_decrypt" "media-flash-sd_mmc-decrypted-mounted"
convert_files "usbpendrive_unmount" "media-flash-usb"
convert_files "usbpendrive_mount" "media-flash-usb-mounted"
convert_files "usbpendrive_unmount_encrypt" "media-flash-usb-encrypted"
convert_files "usbpendrive_unmount_decrypt" "media-flash-usb-decrypted"
convert_files "usbpendrive_mount_decrypt" "media-flash-usb-decrypted-mounted"
convert_files "smart_media_unmount" "media-flash-smart_media"
convert_files "smart_media_mount" "media-flash-smart_media-mounted"
convert_files "smart_media_unmount_encrypt" "media-flash-smart_media-encrypted"
convert_files "smart_media_unmount_decrypt" "media-flash-smart_media-decrypted"
convert_files "smart_media_mount_decrypt" "media-flash-smart_media-decrypted-mounted"
convert_files "memory_stick_unmount" "media-flash-memory_stick"
convert_files "memory_stick_mount" "media-flash-memory_stick-mounted"
convert_files "memory_stick_unmount_encrypt" "media-flash-memory_stick-encrypted"
convert_files "memory_stick_unmount_decrypt" "media-flash-memory_stick-decrypted"
convert_files "memory_stick_mount_decrypt" "media-flash-memory_stick-decrypted-mounted"
convert_files "compact_flash_unmount" "media-flash-compact_flash"
convert_files "compact_flash_mount" "media-flash-compact_flash-mounted"
convert_files "compact_flash_unmount_encrypt" "media-flash-compact_flash-encrypted"
convert_files "compact_flash_unmount_decrypt" "media-flash-compact_flash-decrypted"
convert_files "compact_flash_mount_decrypt" "media-flash-compact_flash-decrypted-mounted"
convert_files "3floppy_unmount" "media-floppy-3_5"
convert_files "3floppy_mount" "media-floppy-3_5-mounted"
convert_files "5floppy_unmount" "media-floppy-5_14"
convert_files "5floppy_mount" "media-floppy-5_14-mounted"
convert_files "zip_unmount" "media-floppy-zip"
convert_files "zip_mount" "media-floppy-zip-mounted"
convert_files "cdrom_unmount" "media-optical-cdrom"
convert_files "cdrom_mount" "media-optical-cdrom-mounted"
convert_files "cdrom_unmount_encrypt" "media-optical-cdrom-encrypted"
convert_files "cdrom_unmount_decrypt" "media-optical-cdrom-decrypted"
convert_files "cdrom_mount_decrypt" "media-optical-cdrom-decrypted-mounted"
convert_files "cdwriter_unmount" "media-optical-cdwriter"
convert_files "cdwriter_mount" "media-optical-cdwriter-mounted"
convert_files "cdwriter_unmount_encrypt" "media-optical-cdwriter-encrypted"
convert_files "cdwriter_unmount_decrypt" "media-optical-cdwriter-decrypted"
convert_files "cdwriter_mount_decrypt" "media-optical-cdwriter-decrypted-mounted"
convert_files "dvd_unmount" "media-optical-dvd"
convert_files "dvd_mount" "media-optical-dvd-mounted"
convert_files "dvd_unmount_encrypt" "media-optical-dvd-encrypted"
convert_files "dvd_unmount_decrypt" "media-optical-dvd-decrypted"
convert_files "dvd_mount_decrypt" "media-optical-dvd-decrypted-mounted"
convert_files "cdaudio_unmount" "media-optical-cdaudio"
convert_files "cdaudio_mount" "media-optical-cdaudio-mounted"
convert_files "tape_unmount" "media-tape"
convert_files "tape_mount" "media-tape-mounted"
convert_files "ipod_unmount" "multimedia-player"
convert_files "ipod_mount" "multimedia-player-mounted"
#convert_files "printer1" "printer"