#!/bin/sh

set -e

umask 022

echo -n "Updating mozilla-thunderbird chrome registry..."

if [ "$1" = "-v" ]; then
    VERBOSE=1
    RM_FLAGS="-v"
    MV_FLAGS="-v"
    INSTALL_FLAGS="-v"
    echo
fi

unset MOZILLA_FIVE_HOME || :
MOZILLA_FIVE_HOME=/usr/lib/mozilla-thunderbird
export MOZILLA_FIVE_HOME

# PATH
PATH=/usr/lib/mozilla-thunderbird:$PATH
export PATH

# added /usr/lib : don't load your local library
LD_LIBRARY_PATH=/usr/lib/mozilla-thunderbird${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

# fake home to capture .thunderbird
HOME=`mktemp -d /tmp/mozilla-thunderbird-pkg.XXXXXX`
export HOME

/sbin/ldconfig

VARDIR=/var/lib/mozilla-thunderbird
LIBDIR=/usr/lib/mozilla-thunderbird
EXTDIR=${VARDIR}/extensions.d
DATADIR=${VARDIR}/chrome.d

# cleaning VARDIR
rm ${RM_FLAGS} -fr ${VARDIR}/chrome ${VARDIR}/components ${VARDIR}/extensions/*

# cleaning LIBDIR
rm ${RM_FLAGS} -rf ${LIBDIR}/chrome/overlayinfo
rm ${RM_FLAGS} -f  ${LIBDIR}/chrome/*.rdf
rm ${RM_FLAGS} -f  ${LIBDIR}/components/*.dat
rm ${RM_FLAGS} -f  ${LIBDIR}/components.ini
rm ${RM_FLAGS} -f  ${LIBDIR}/defaults.ini

# purge ${LIBDIR}/defaults/profile/extensions/installed-extensions.txt symlink
# we used to create
if [ -L ${LIBDIR}/defaults/profile/extensions/installed-extensions.txt ]; then
  rm ${RM_FLAGS} -f ${LIBDIR}/defaults/profile/extensions/installed-extensions.txt
fi

# create VARDIR
install ${INSTALL_FLAGS} -d ${VARDIR}/chrome/overlayinfo
install ${INSTALL_FLAGS} -d ${VARDIR}/components

# create symlinks to LIBDIR
ln -sf ${VARDIR}/chrome/overlayinfo ${LIBDIR}/chrome/overlayinfo
ln -sf ${VARDIR}/extensions/installed-extensions.txt \
    ${LIBDIR}/extensions

# purge installed-extensions-processed.txt
rm ${RM_FLAGS} -f ${LIBDIR}/extensions/installed-extensions-processed.txt

# purge Extensions.rdf
rm ${RM_FLAGS} -f ${LIBDIR}/extensions/Extensions.rdf

# Handle extensions {uid} directories (for compatibility purpose with
# previously packaged extensions):
# remove those that are symlinks pointing to nowhere
# and remove those that are directory and are in defaults/profile/extensions
for f in `find ${LIBDIR}/extensions/ -maxdepth 1 -name '{*}'`; do
    if [ -L $f ] && [ ! -e $f ]; then
        rm ${RM_FLAGS} -f $f
    elif [ ! -L $f ] && [ -d $f ] && [ -d ${LIBDIR}/defaults/profile/extensions/`basename $f` ]; then
        rm ${RM_FLAGS} -rf $f
    elif [ -d "$f" ]; then
      # remove empty chrome directories
      [ -d "${f}/chrome" ] && rmdir ${RM_FLAGS} --ignore-fail-on-non-empty ${f}/chrome
      # remove Uninstall file, it will be recreated by thunderbird-bin -register
      [ -f "$f/uninstall/Uninstall" ] && rm ${RM_FLAGS} ${f}/uninstall/Uninstall
      # remove empty uninstall directory
      [ -d "$f/uninstall" ] && rmdir ${RM_FLAGS} --ignore-fail-on-non-empty ${f}/uninstall
      # remove empty extension directory
      [ ! -L "$f" ] && rmdir ${RM_FLAGS} --ignore-fail-on-non-empty ${f}
    fi
done
# create symlinks that are not already there
for f in `find ${LIBDIR}/defaults/profile/extensions/ -maxdepth 1 -name '{*}'`; do
    [ "${VERBOSE}" ] && echo "W: ${f} should be in ${LIBDIR}/extensions/" >&2
    if [ ! -e ${LIBDIR}/extensions/`basename $f` ]; then
        ln -s $f ${LIBDIR}/extensions
    fi
done

# gen installed-extensions.txt
TMPFILE=`tempfile`
for f in `echo ${EXTDIR}/*.txt | sort`; do
    while read line; do
    	EXTUID=`echo ${line} | cut -f 2 -d ,`
#	echo EEE $EXTUID ${EXTDIR}
        EXTUIDDIR=${LIBDIR}/extensions/${EXTUID}
        if [ -e ${EXTUIDDIR} ]; then
	    if [ -e ${EXTUIDDIR}/install.rdf ]; then
	        if grep ${EXTUID} ${EXTUIDDIR}/install.rdf 2>&1 > /dev/null; then
	            echo $line >> ${TMPFILE}
	        elif [ "${VERBOSE}" ]; then
		    echo "W: ${f}: ${EXTUIDDIR}/install.rdf doesn't contain the UID." >& 2
		fi
	    elif [ "${VERBOSE}" ]; then
	        echo "W: ${f}: ${EXTUIDDIR}/install.rdf doesn't exist." >&2
	    fi
	elif [ "${VERBOSE}" ]; then
	    echo "W: ${f}: ${EXTUIDDIR} doesn't exist." >&2
	fi
    done < $f
done

mv ${MV_FLAGS} ${TMPFILE} ${VARDIR}/extensions/installed-extensions.txt
chmod 0644 ${VARDIR}/extensions/installed-extensions.txt

if [ "$VERBOSE" ]; then
    for f in `find ${LIBDIR}/extensions/ -maxdepth 1 -name '{*}'`; do
        if ! grep `basename $f` ${VARDIR}/extensions/installed-extensions.txt 2>&1 > /dev/null; then
	    echo "W: `basename $f` not in ${VARDIR}/extensions/installed-extensions.txt" >&2
	fi
    done
fi

# gen
mozilla-thunderbird-bin -register >/dev/null 2>&1 || echo "E: Registration process existed with status: $?" >&2

if [ -e ${LIBDIR}/extensions/installed-extensions.txt ]; then
    echo "E: ${LIBDIR}/extensions/installed-extensions.txt still present. Registration might have gone wrong." >&2
fi

# move and link
for file in $(find ${LIBDIR}/components/*.dat 2> /dev/null); do
    mv ${MV_FLAGS} ${file} ${VARDIR}/components/
    ln -fs ${VARDIR}/components/`basename "${file}"` ${LIBDIR}/components/
done

for file in $(find ${LIBDIR}/chrome/*.rdf 2> /dev/null); do
    mv ${MV_FLAGS} ${file} ${VARDIR}/chrome/
    ln -fs ${VARDIR}/chrome/`basename "${file}"` ${LIBDIR}/chrome/
done

mv ${MV_FLAGS} ${LIBDIR}/defaults.ini ${VARDIR}/
ln -fs ${VARDIR}/defaults.ini ${LIBDIR}/

mv ${MV_FLAGS} ${LIBDIR}/components.ini ${VARDIR}/
ln -fs ${VARDIR}/components.ini ${LIBDIR}/

mv ${MV_FLAGS} ${LIBDIR}/extensions/Extensions.rdf ${VARDIR}/extensions/
ln -fs ${VARDIR}/extensions/Extensions.rdf ${LIBDIR}/extensions/

# gen installed-chrome.txt
TMPFILE=$HOME/installed-chrome.txt
for f in `echo ${DATADIR}/* | sort`; do
    cat $f >> ${TMPFILE}
done

mv ${MV_FLAGS} ${TMPFILE} ${VARDIR}/chrome/installed-chrome.txt
chmod 0644 ${VARDIR}/chrome/installed-chrome.txt

rm ${RM_FLAGS} -fr ${HOME}

regchrome
regxpcom

# done
echo done.
