#!/bin/sh
# Copyright and license see bottom of this file
echo "====Detecting hime install===="
HIME_BINARY=$(whereis hime | cut -d ' ' -f 2)
if [ -x "$HIME_BINARY" ]; then
    echo "==>Hime binary found."
else
    echo
    echo "==>Your hime is not installed properly. Bye."
    echo
    exit
fi
if [ -x "$HIME_BINARY" ]; then
    echo
    echo "==>$HIME_BINARY:$("$HIME_BINARY" -v 2>&1 | grep version | sed -e 's/$/, linked to/') $(ldd "$HIME_BINARY" | awk '/gtk/{print $3}')"

    find /usr/share/doc/ -path "*hime*" -name ChangeLog.Debian.gz \
        -exec echo -n "==>{}: " \; -exec zcat {} \; -exec head -n 1 \;
    find /usr/share/doc/ -path "*hime*" -name ChangeLog \
        -exec echo -n "==>{}: " \; -exec head -n 1 {} \;
fi
echo
echo
echo "====Detecting OS/Distribution===="
# OS / distribution
uname -a | sed -e "s/$(hostname)//" -e "s/$USER//" | tr -s ' ' ' '

echo
lsb_release -a 2>/dev/null | grep -v "LSB Version"
find /etc -name "*version" \
    -exec echo "{}:" \; -exec cat {} \; 2>/dev/null

find /etc -name "*release" \
    -exec echo "{}:" \; -exec cat {} \; 2>/dev/null

echo
echo "/etc/issue: $(head -n 1 /etc/issue | sed 's/$/\\/' | cut -d "\\" -f 1)"

echo
echo
echo "====Detecting Environment Variables===="
if [ -n "$XMODIFIERS" ] || [ -n "$LC_ALL" ] || [ -n "$LC_CTYPE" ] ||
    [ -n "$LANG" ] || [ -n "$LANGUAGE" ] || [ -n "$GTK_IM_MODULE" ] ||
    [ -n "$QT_IM_MODULE" ] ; then
    echo
    [ -n "$XMODIFIERS" ] && echo "XMODIFIERS   : XMODIFIERS=$XMODIFIERS"
    [ -n "$LANG" ] && echo "LANG               : LANG=$LANG"
    [ -n "$LANGUAGE" ] && echo "LANGUAGE       : LANGUAGE=$LANGUAGE"
    [ -n "$LC_CTYPE" ] && echo "LC_CTYPE       : LC_CTYPE=$LC_CTYPE"
    [ -n "$LC_ALL" ] && echo "LC_ALL           : LC_ALL=$LC_ALL"
    locale 2>/dev/null | grep -v '"' | sed 's/=/\t      : /' | grep -v ': $'
    [ -n "$GTK_IM_MODULE" ] && echo "GTK_IM_MODULE: GTK_IM_MODULE=$GTK_IM_MODULE"
    [ -n "$QT_IM_MODULE" ] && echo "QT_IM_MODULE : QT_IM_MODULE=$QT_IM_MODULE"
fi

echo
echo
echo "====Detecting hime status===="
echo "==>Running hime number: ""$(pgrep ^hime$ | wc -l)"

echo
# TODO: get tempdir from TMPDIR, TMP, and TEMP
echo "==>hime socket number: $(find "/tmp/.hime-$USER" -name "socket*" | wc -l)"

echo
echo "==>System library path: "
ls -d /lib /lib32 /lib64 /usr/lib /usr/lib32 /usr/lib64 2>/dev/null

echo "====Immodule status===="
# TODO: detect more binary name of gtk-query-immodules-2.0
GTK_QUERY_2=$(whereis gtk-query-immodules-2.0 | cut -d ' ' -f 2)

if [ ! -x "$GTK_QUERY_IMMODULES_2" ]; then
    GTK_QUERY_IMMODULES_2="$(find /usr/lib -path "*libgtk2.0-0*" -name gtk-query-immodules-2.0)"
fi

if [ -x "$GTK_QUERY_2" ]; then
    echo
    echo "==>gtk-query-immodules-2.0:"
    ls -d "$("$GTK_QUERY_2" | grep hime | cut -d '"' -f 2 | grep '/')"
fi
echo
echo "==>gtk-2.0 immodule cache"
find /etc /usr/lib64 /usr/lib /usr/local/etc \
    -path "*gtk-2.0*" -name "*immodules*" \
    -exec grep hime '{}' \; 2>/dev/null |
    cut -d '"' -f 2 | grep so

# TODO: detect more binary name of gtk-query-immodules-3.0
GTK_QUERY_3=$(whereis gtk-query-immodules-3.0 | cut -d ' ' -f 2)

if [ ! -x "$GTK_QUERY_IMMODULES_3" ]; then
    GTK_QUERY_IMMODULES_3="$(find /usr/lib -path "*libgtk-3.0*" -name gtk-query-immodules-3.0)"
fi

if [ -x "$GTK_QUERY_3" ]; then
    echo
    echo "==>gtk-query-immodules-3.0:"
    ls -d "$("$GTK_QUERY_3" | grep hime | cut -d '"' -f 2 | grep '/')"
fi
echo
echo "==>gtk-3.0 immodule cache"
find /usr/lib64 /usr/lib /usr/local/lib \
    -path "*gtk-3.0*" -name "immodules.cache*" \
    -exec grep hime '{}' \; 2>/dev/null |
    cut -d '"' -f 2 | grep so

# Copyright (C) 2012 Wen-Yen Chuang <caleb AT calno DOT com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
