#!/usr/bin/bash

FAIL_COUNTER=0

check_pkg() {
    local pkg=$1
    if rpm -q $pkg
    then
        echo "PASS"
    else
        echo "FAIL"
        let "FAIL_COUNTER=FAIL_COUNTER+1"
    fi
}


check_pkg "ibus"
check_pkg "ibus-table"
check_pkg "ibus-table-chinese"
check_pkg "ibus-table-chinese-array"
check_pkg "ibus-table-chinese-cangjie"
check_pkg "ibus-table-chinese-cantonese"
check_pkg "ibus-table-chinese-cantonyale"
check_pkg "ibus-table-chinese-easy"
check_pkg "ibus-table-chinese-erbi"
check_pkg "ibus-table-chinese-quick"
check_pkg "ibus-table-chinese-scj"
check_pkg "ibus-table-chinese-stroke5"
check_pkg "ibus-table-chinese-wu"
check_pkg "ibus-table-chinese-wubi-haifeng"
check_pkg "ibus-table-chinese-wubi-jidian"
check_pkg "ibus-table-chinese-yong"

echo "starting ibus-daemon."

ibus-daemon -v -r -d

if [ $? == 0 ]
then
   echo "PASS"
else
   echo "FAIL"
   let "FAIL_COUNTER=FAIL_COUNTER+1"
fi

echo "give ibus-daemon some time to start properly."
sleep 5

for name in \
    array30-big \
    array30 \
    cangjie-big \
    cangjie3 \
    cangjie5 \
    cantonese \
    cantonhk \
    cantonyale \
    easy-big \
    erbi-qs \
    erbi \
    jyutping \
    quick-classic \
    quick3 \
    quick5 \
    scj6 \
    stroke5 \
    wu \
    wubi-haifeng86 \
    wubi-jidian86 \
    yong
do
    echo "checking whether ibus can list ${name}:"
    ibus list-engine --name-only | grep "table:${name}"
    if [ $? == 0 ]
    then
        echo "PASS"
    else
        echo "FAIL"
        let "FAIL_COUNTER=FAIL_COUNTER+1"
    fi
done

ibus exit

exit ${FAIL_COUNTER}