#! /bin/bash

test -x /usr/bin/mongoexport || exit 0

GIT_AUTHOR_NAME=rocketchat-server
GIT_AUTHOR_EMAIL=rocketchat-server@localhost.localdomain

/usr/bin/mongoexport --quiet --db=rocketchat --collection=rocketchat_settings \
  | jq '"\(._id): \(.value)"' \
  | grep -v \
    -e 'Cloud_Workspace_Access_Token' \
    -e 'Cloud_Workspace_Supported_Versions_Token' \
    -e 'Cloud_Workspace_AirGapped_Restrictions_Remaining_Days' \
    -e '.*_Count:' \
    -e 'Update_LatestAvailableVersion' \
    -e 'Enterprise_License' \
  > /etc/rocketchat-server/settings_mongoexport

/usr/bin/mongoexport --quiet --db=rocketchat --collection=rocketchat_apps \
  | jq . \
  > /etc/rocketchat-server/apps

# Exit now if there is no git repository to commit to
git -C /etc/rocketchat-server rev-parse 2> /dev/null || exit 0

cd /etc/rocketchat-server
git add .
git commit -q -m "Autocommit rocketchat-server settings" > /dev/null
# git commit would return with exit code 1 most of the time
exit 0
