#!/bin/bash

# This script searches the current directory for Icons in
# YCP and desktop files. These icons are checked for existence
# against the current YaST theme

ICONSDIR=$1
if [ "${ICONSDIR}" == "" ]; then
    ICONSDIR="/usr/share/YaST2/theme/current/icons"
fi
echo "Icons will checked against ${ICONSDIR}"
echo

echo "Checking files in directory: "`pwd`
echo

ICONSRES="22x22 32x32 48x48"

function CheckForIcons {
    for ICONNAME in ${ICONSUSED}; do
	for RES in ${ICONSRES}; do
	    ICONNAME_1="${ICONSDIR}/${RES}/apps/${ICONNAME}.png"
	    ICONNAME_2="${ICONSDIR}/${RES}/apps/yast-${ICONNAME}.png"

	    if [ ! -e "${ICONNAME_1}" ] && [ ! -e "${ICONNAME_2}" ]; then
		echo "No such file: ${ICONNAME_1} (yast-${ICONNAME}.png)"
	    fi
	done
    done
    echo
}

TMPFILE=`mktemp`

# >> YCP files
echo -n > ${TMPFILE}
for FILE in `find -name "*.ycp" | grep -v "\(\.svn/\|testsuite/\|include/\)"`; do
    echo "Processing file ${FILE}"
    ycpc -E ${FILE} 2>/dev/null | grep "Icon (\"" | sed 's/.*Icon (\"\([^)]*\)\").*/\1/' >> ${TMPFILE}
done
echo

ICONSUSED=`sort ${TMPFILE} | uniq; rm -rf ${TMPFILE}`
echo "All used icons (in YCP):"
echo ${ICONSUSED}
echo
# << YCP files

CheckForIcons

# >> desktop files
echo -n > ${TMPFILE}
for FILE in `find -name "*.desktop" | grep -v "\.svn/"`; do
    echo "Processing file ${FILE}"
    grep "Icon=" ${FILE} | sed 's/.*Icon=//' >> ${TMPFILE}
done
echo

ICONSUSED=`sort ${TMPFILE} | uniq; rm -rf ${TMPFILE}`
echo "All used icons (in desktop files):"
echo ${ICONSUSED}
echo
# << desktop files

CheckForIcons
