#!/bin/sh
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

if grep --quiet "ip_access: all" /etc/ejabberd/ejabberd.yml; then
    xmpp_inband_enable=true
else
    xmpp_inband_enable=false
fi
xmpp_inband_enable_cur=$xmpp_inband_enable
export xmpp_inband_enable

while [ "$1" ] ; do
    arg="$1"
    shift
    case "$arg" in
        inband_enable|noinband_enable) # Not using disable for consistency with other options
            if [ 'inband_enable' = "$arg" ] ; then
                xmpp_inband_enable=true
            else
                xmpp_inband_enable=false
            fi
            export xmpp_inband_enable
            ;;
        status)
            printstatus() {
                if "$2" ; then
                    echo "$1"
                else
                    echo no"$1"
                fi
            }
            printstatus inband_enable $xmpp_inband_enable_cur
            exit 0
            ;;
        *)
            ;;
    esac
done

if [ "$xmpp_inband_enable" != "$xmpp_inband_enable_cur" ] ; then
    if $xmpp_inband_enable ; then
        sed -i s/"ip_access: trusted_network"/"ip_access: all"/ /etc/ejabberd/ejabberd.yml
    else
        sed -i s/"ip_access: all"/"ip_access: trusted_network"/ /etc/ejabberd/ejabberd.yml
    fi
    ejabberdctl restart || echo "Failed to restart ejabberd with new configuration."
fi
