#!/bin/sh
#
# harctoolbox script
# JPackage Project <http://www.jpackage.org/> //Patched

group_msg="You should be part of the \"lock\" group to run irscrutinizer
Run e. g.,    sudo usermod -aG lock  $USER
After that you must logout and login again for the new group
to be effective (on command line you can use newgrp(1) to avoid this)"

root_msg="Thou shall not run this program as root!
You have been warned."

# Set up so we are in grouo "lock", exit on errors
id -Gn | grep -q lock  || {
    if [ "$EUID" != "0" ]; then
        zenity --warning --text="$group_msg"
        exit 1
    fi
}

if [ "$( id -gn )" != lock ]; then
    exec sg lock "$0 $*"
fi

if [ "$EUID" = "0" ]; then
       zenity --warning --text="$root_msg"
fi

# Source jpackage functions library
_prefer_jre="true"
. /usr/share/java-utils/java-functions

# Source system prefs
if [ -f /etc/java/harctoolbox.conf ] ; then
  . /etc/java/harctoolbox.conf
fi

# Source user prefs
if [ -f $HOME/.harctoolboxrc ] ; then
  . $HOME/.harctoolboxrc
fi

# Add link to crystal icons to CLASSPATH, glob library path
CLASSPATH=/usr/share/harctoolbox
lib="/usr/lib*/DecodeIR/libDecodeIR.so"

# Configuration
MAIN_CLASS=org.harctoolbox.irscrutinizer.IrScrutinizer
BASE_FLAGS="$BASE_FLAGS -Dharc.decodeir.library.path=$lib"
BASE_OPTIONS=""
BASE_JARS="tonto harctoolbox antlr32-runtime DecodeIR ExchangeIR"
BASE_JARS="$BASE_JARS stringtemplate beust-jcommander minimal-json RXTXcomm"

# Set parameters
set_jvm
set_classpath $BASE_JARS
set_flags $BASE_FLAGS
set_options $BASE_OPTIONS

# Fix config files
IRSCRUTINIZERHOME=${IRSCRUTINIZERHOME:-"$HOME/.config/harctoolbox"}
test -d $IRSCRUTINIZERHOME || mkdir -p $IRSCRUTINIZERHOME
for f in protocols.ini IrpProtocols.ini girr.xsd exportformats.xml; do
    test -f $IRSCRUTINIZERHOME/$f || \
        cp /usr/share/harctoolbox/etc/$f $IRSCRUTINIZERHOME/$f
done

# Let's start
export IRSCRUTINIZERHOME
run "$@"
