#!/usr/bin/sh

if [ $(pgrep -u $(whoami) xfconfd\|xfce4-panel | wc -l) -lt 2 ]
then
	echo "ERROR: Undercover Mode only works on Xfce desktop" >&2
	notify-send -i dialog-warning 'Undercover Mode only works on Xfce desktop'
	exit 1
fi

DIR=~/.local/share/kali-undercover/
SHARE_DIR=/usr/share/kali-undercover/
CONF_FILES=$SHARE_DIR/config/
XFCE4_DESKTOP_PROFILES=$SHARE_DIR/scripts/xfce4-desktop-profiles.py
KALI_UNDERCOVER_PROFILE=$SHARE_DIR/kali-undercover-profile.tar.bz
USER_PROFILE=$DIR/user-profile.tar.bz

mkdir -p $DIR

# Hide existing notifications
killall xfce4-notifyd 2> /dev/null

enable_undercover() {
	$XFCE4_DESKTOP_PROFILES save $USER_PROFILE
	$XFCE4_DESKTOP_PROFILES load $KALI_UNDERCOVER_PROFILE
	if pgrep -u $(whoami) -x plank > /dev/null
	then
		killall plank
		touch $DIR/plank
	fi
	(cd $CONF_FILES && \
		find . -type f -exec sh -c \
			'[ -f ~/.config/"$1" ] && mv ~/.config/"$1" ~/.config/"${1}.undercover"' _ {} \;)
	cp -r $CONF_FILES/* ~/.config/
	[ -f ~/.face ] && mv ~/.face ~/.face.undercover
	printf ': undercover && export PS1='\''C:${PWD//\//\\\\\}> '\''\n' >> ~/.bashrc
	printf ': undercover && export PS1='\''C:${PWD//\//\\\\}> '\''\n' >> ~/.zshrc
	printf ': undercover && new_line_before_prompt=no\n' >> ~/.zshrc
}

disable_undercover() {
	$XFCE4_DESKTOP_PROFILES load $USER_PROFILE
	[ -f $DIR/plank ] && plank > /dev/null &
	rm -r $DIR
	(cd $CONF_FILES && \
		find . -type f -exec rm ~/.config/{} \;)
	find ~/.config -name '*.undercover' -exec sh -c \
		'mv "$1" "$(echo $1 | sed 's/.undercover//')"' _ {} \;
	[ -f ~/.face.undercover ] && mv ~/.face.undercover ~/.face
	sed -i -e '/: undercover/d' ~/.bashrc ~/.zshrc
}

if [ -f $USER_PROFILE ]
then
	disable_undercover
	sleep 1
	notify-send -i dialog-information 'Desktop settings restored'
else
	enable_undercover
fi

shell=$(cat /proc/$PPID/comm)
if [ $shell = 'bash' ] || [ $shell = 'zsh' ]
then
	clear
	$shell
fi

