#!/usr/bin/env bash

OLD_IFS="$IFS"
IFS="$(printf '\n+')"

generate_random_string()
{
	cat /dev/urandom | LC_ALL=C tr -dc 'a-z' | fold -w 6 | head -n 1
}

if [ -z "$KDB" ];
then
    KDB=kdb
fi
KDB=$(which $KDB)

ORIG_PATHS="$($KDB mount-list-all-files)"
ENV="$(env 2>/dev/null )"

if [ -z "$ENV" ];
then
	echo "no environment variables found"
	exit 1
fi

for env in $ENV;
do
	NAME="$(echo "$env" | cut -d '=' -f1 2>/dev/null)"
	if [ "$NAME" = "KDB" ] || [ "$NAME" = "PATH" ];
	then
		continue
	fi
	ORIG_VALUE="$(echo "$env" | cut -d '=' -f2- 2>/dev/null)"
	NEW_VALUE=$(generate_random_string)
	export "$NAME"="$NEW_VALUE" > /dev/null 2>&1
	NEW_PATHS="$($KDB mount-list-all-files 2>/dev/null)"
	DIFF="$(comm -3 <(echo "$ORIG_PATHS" | sort 2>/dev/null) <(echo "$NEW_PATHS" | sort 2>/dev/null))"
	if [ ! -z "$DIFF" ];
	then
		echo "$NAME influences $DIFF"
	fi
	export "$NAME"="\"$ORIG_VALUE\"" > /dev/null 2>&1
done
