#!/bin/bash

set -e

export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
export CHECKBOX_DATA=${CHECKBOX_DATA:-.}
export CHECKBOX_SHARE=${CHECKBOX_SHARE:-.}
export CHECKBOX_FRONTEND=${CHECKBOX_FRONTEND:-$CHECKBOX_SHARE/qt/frontend}
export CHECKBOX_OPTIONS=${CHECKBOX_OPTIONS:---whitelist-file=$CHECKBOX_SHARE/data/whitelists/default.whitelist}
export PYTHONPATH=$PYTHONPATH:$CHECKBOX_SHARE

if [ $CHECKBOX_DATA != '.' ]
then
    old_data=$HOME/.checkbox
    if [ -d $old_data ] && [ ! -d $CHECKBOX_DATA ]
    then
        mv -f $old_data $CHECKBOX_DATA
    fi
fi

if [ -e $CHECKBOX_FRONTEND ]
then
    (cd $CHECKBOX_FRONTEND; qmake; make; ./checkbox-qt-service &)
fi

python3 $CHECKBOX_SHARE/run "$@" $CHECKBOX_SHARE/configs/$(basename $0).ini
