/*
   Copyright (C) 2008 Norbert Frese <nf2 at scheinwelt at>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License (LGPL) as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later
   version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <glib.h>
#include <gio/gio.h>
#include <string.h>
#include <QString>
#include <QUrl>

static GMainLoop *main_loop;

static void _my_unmountReadyCallback(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    GError * err = NULL;

    if (!g_mount_unmount_finish(G_MOUNT(source_object), res, &err))
    {
        g_print("unmount failed: %s\n", err->message );
        g_error_free(err);
    }
    g_main_loop_quit (main_loop);
}


static GMount * find_mount(const char * findName)
{
    GVolumeMonitor* gVolumeMonitor = g_volume_monitor_get();
    {
        GList* mountList = g_volume_monitor_get_mounts(gVolumeMonitor);
        GList* el2 = mountList;

        while(el2)
        {
            GMount * gMount = (GMount *) el2->data;
            char * mountName = g_mount_get_name(gMount);
            GFile * gFileRoot = g_mount_get_root(gMount);

            char * pointsToUri = g_file_get_uri(gFileRoot);

            g_print("root uri: %s == %s\n", pointsToUri, findName);

            if (strcmp(pointsToUri, findName)==0)
            {
                return gMount;
            }

            g_object_unref(gFileRoot);
            g_free(mountName);
            g_free(pointsToUri);

            el2 = g_list_next(el2);
        }
    }
    return NULL;
}



int main(int argc, char ** argv)
{
    if (argc <= 1)
    {
        g_print("usage: %s \"remote:/display name of mount\"", argv[0]);
        return 1;
    }

    g_type_init();

    const char * unmountUrl_ = argv[1];

    g_print("unmounting gvfs mount: %s\n", unmountUrl_);

    QUrl qurl = QUrl::fromEncoded(unmountUrl_);

    QString mountName = qurl.toString();

    if (mountName.startsWith("/"))
        mountName.remove(0,1);

    const char * mountName_ = strdup(mountName.toUtf8 ().data());

    GMount * foundGMount = find_mount(mountName_);

    if (foundGMount==NULL)
    {
        g_print("mount not found: %s %p\n", mountName_, foundGMount);
        return 1;
    }

    g_mount_unmount(foundGMount, G_MOUNT_UNMOUNT_NONE, NULL, _my_unmountReadyCallback, NULL);

    main_loop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (main_loop);
    g_main_loop_unref(main_loop);
    return 0;
}
