#!/bin/bash
#
# Refresh local firejail symlinks
#

# Parameters
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
PATTERN=""

# Ensure we are in the right folder
cd $DIRNAME

# Add global folder
if [ -d "/etc/firejail" ]; then
  PATTERN="$PATTERN /etc/firejail/*profile"
fi

# Add local folder
if [ -d "$HOME/.config/firejail" ]; then
  PATTERN="$PATTERN $HOME/.config/firejail/*profile"
fi

if [ ! -z "$PATTERN" ]; then
  ls -1 $PATTERN | \
    sed -e 's|/etc/firejail/||g' -e "s|$HOME/.config/firejail/||g" -e 's/.profile$//' | sort | uniq | while read profile; do
  if which $profile &> /dev/null; then
    if [ ! -h "$profile" ]; then
      ln -s /usr/bin/firejail $profile
    fi
  fi
done
fi