#!/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/>.
#

# Usage:
#   module-manager list-available
#   module-manager list-enabled <python_root>
#   module-manager enable <python_root> <module_name>
#   module-manager disable <python_root> <module_name>

# list of modules that may be enabled/disabled
modules="owncloud"

case "$1" in
"list-available")
    # TODO: Replace this with something like "aptitude search -F %p plinth-"
    echo "$modules"
    ;;

"list-enabled")
    # TODO: Replace this with something like 'aptitude search -F %p | grep "plinth-"'
    for module in "$modules"
    do
        if [ -e "$2"/modules/enabled/"$module" ] ; then
            echo "$module"
        fi
    done
    ;;

"enable")
    # TODO: Replace this with "aptitude install plinth-<module>"
    for module in "$modules"
    do
        if [ "$3" = "$module" ] ; then
            if [ ! -e "$2"/modules/enabled/"$3" ] ; then
                touch "$2"/modules/enabled/"$3"
                RETVAL=$?
                if [ $RETVAL -eq 0 ] ; then
                    echo "enabled" "$3"
                else
                    echo "failed to enable" "$3"
                fi
                exit $RETVAL
            fi
        fi
    done
    echo "failed to enable invalid module" "$3"
    exit 1
    ;;

"disable")
    # TODO: Replace this with "aptitude purge plinth-<module>"
    for module in "$modules"
    do
        if [ "$3" = "$module" ] ; then
            if [ -e "$2"/modules/enabled/"$3" ] ; then
                rm -f "$2"/modules/enabled/"$3"
                RETVAL=$?
                if [ $RETVAL -eq 0 ] ; then
                    echo "disabled" "$3"
                else
                    echo "failed to disable" "$3"
                fi
                exit $RETVAL
            fi
        fi
    done
    echo "failed to disable invalid module" "$3"
    exit 1
    ;;
esac
