#!/bin/bash
# -*- mode: sh; tab-width: 8; coding: utf-8-unix -*-

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore

. /usr/local/src/usr_local_src.bash || exit 2
# [ `id -u` -eq 0 ] && ERROR $prog should not be run as root && exit 2

export LOG_DIR=$PREFIX/var/log/$ROLE

DESC=""

cd /usr/local/src || exit 4

if [ "$#" -eq 0 ] ; then
    # /usr/lib/python3.9/site-packages/owtf/scripts/ssl/verify_ssl_cipher_check.sh
    [ -f /usr/local/bin/ssl-cipher-check.pl ] || \
	wget -cP /usr/local/bin/ http://unspecific.com/ssl/ssl-cipher-check.pl

    if [ $USER = root ] ; then
	# https://unix.stackexchange.com/questions/271661/disable-gnome-keyring-daemon
	command -v keepassxc.bash
	EXE=`command -v keepassxc.bash`
	[ -z "$EXE" ] && EXE=`command -v keepassxc`
	if [ -z "$EXE" ] ; then
	    export PYTHON_KEYRING_BACKEND=keyring.backends.SecretService.Keyring
	    ELTS=`ps ax|grep gnome-keyring-daemon|grep -v grep|sed -e 's/^ *//' -e 's/ .*//'`
	    [ -n "$ELTS" ] && kill $ELTS
	    if [ -d /etc/pam.d ] ; then
		cd /etc/pam.d
		grep -l '^[^#].*pam_gnome_keyring.so' * | while read file ; do
		    [ -f .$file.dst ] || cp -p $file .$file.dst
		    sed -e 's/.*pam_gnome_keyring.so.*/#&/' -i $file 
		done
	    fi
	    file=/usr/local/share/dbus-1/services/org.freedesktop.secrets.service
	    if [ ! -f $file ] || ! grep -q $EXE $file ; then
		cat > $file <<EOF
[D-BUS Service]
Name=org.freedesktop.secrets
Exec=$EXE
EOF
	    fi
	fi
    fi

    if [ $USER != root ] ; then
	# https://unix.stackexchange.com/questions/271661/disable-gnome-keyring-daemon
	
	[ -d ~/.config/autostart ] || mkdir ~/.config/autostart
	cd /etc/xdg/autostart/
	for file in * ; do
	    [ -f ~/.config/autostart/$file ] || {
		cat > ~/.config/autostart/$file <<EOF
[Desktop Entry]
Hidden=true
EOF
		INFO created ~/.config/autostart/$file - cp /dev/null to reenable
	    }
	done

	# https://pypi.org/project/keyring/
	A=`python3.bash -c "import keyring.util.platform_; print(keyring.util.platform_.config_root())"` || exit 1$?
	[ -f "$A" ] || touch "$A"
	# $HOME/.config/python_keyring
	[ -s "$A" ] || cat > "$A" <<EOF
[backend]
default-keyring=keyring.backends.SecretService.Keyring
EOF

	A=`python3 -c "import keyring.util.platform_; print(keyring.util.platform_.data_root())"`
	[ -d "$A" ] || mkdir "$A"

	if `which keepassxc` && ps ax | grep -v grep | grep -q keepassxc ; then
	    python3 -m keyring --list-backends | \
		grep -q keyring.backends.SecretService.Keyring  || \
		WARN NO keyring.backends.SecretService.Keyring

	    # string "org.freedesktop.secrets"
	    dbus-send --session --dest=org.freedesktop.DBus \
		      --type=method_call --print-reply \
		      /org/freedesktop/DBus org.freedesktop.DBus.ListNames | \
		grep -q 'org.freedesktop.secrets' || \	    
	    WARN NO org.freedesktop.DBus.ListNames

	else
	    python3 -m keyring --list-backends || \
		WARN NO keyring.backends
	fi

	if `which gajim` || [ -f $PREFIX/bin/gajim ] ; then
	    [ -f $HOME/.config/gajim/config ] || cat >  $HOME/.config/gajim/config <<EOF
proxies.Tor.bosh_wait_for_restart_response = False
proxies.Tor.useauth = False
proxies.Tor.bosh_useproxy = True
proxies.Tor.bosh_http_pipelining = False
proxies.Tor.bosh_content = text/xml; charset=utf-8
proxies.Tor.bosh_uri =
proxies.Tor.bosh_wait = 30
proxies.Tor.host = 127.0.0.1
proxies.Tor.user =
proxies.Tor.pass =
proxies.Tor.bosh_hold = 2
proxies.Tor.type = socks5
proxies.Tor.port = 9050
EOF

	fi
	
    fi

    exit 0

elif [ "$1" = check ] ; then
    [ -f /var/local/src/var_local_src.bash ] || exit 0
    . /var/local/src/var_local_src.bash || exit 10
    
    msys_var_local_src_prog_key $1 || exit 10$?
    
elif [ "$1" = 'lint' ] ; then
    [ -f /var/local/src/var_local_src.bash ] || exit 0
    . /var/local/src/var_local_src.bash
    msys_var_local_src_prog_key $1 || exit 20$?
    ols_run_tests_shellcheck $ROLE || exit 21$?
    ols_run_tests_pylint ||  exit 22$?
    
elif [ "$1" = 'test' ] ; then
    msys_var_local_src_prog_key $1 || exit 51$?
fi
