/*
   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 <QFile>
#include <QDir>
#include <QFileInfo>
#include <QString>
#include <QtGlobal>
#include <kstandarddirs.h>
#include <kdesktopfile.h>
#include <kcomponentdata.h>
#include <iostream>
#include <cstdlib>

using namespace std;

/*
 * tool to enabale/disable kio-giobridge
 * installs/removes kio-giobridge protocol files to/from the
 * local kde4 services directory
 */

KComponentData kComponentData("giobridge-mgr");

static void pUsage(int argc, char ** argv)
{
    cout << "usage: " << argv[0] << " enable|disable" << endl;
}

static void cleanProtocols(const QString & dir)
{
    QDir qDir(dir);
    QFileInfoList fiList = qDir.entryInfoList (QStringList ("*.protocol"));
    foreach(QFileInfo fi, fiList)
    {
        QString filePath = fi.absoluteFilePath();

        KDesktopFile de(filePath);
        QMap< QString, QString > entries = de.entryMap("Protocol");
        if (entries["Vendor"] == "kio-giobridge")
        {
            cout << "removing: " << filePath.toLatin1().data() << endl;
            if (!QFile::remove(filePath))
            {
                cout << "error " << /* fileToDelete.error() <<*/ "cannot delete: " << filePath.toLatin1().data() << endl;
                exit(1);
            }
        }
        else
        {
            cout << "keeping: " << filePath.toLatin1().data() << endl;
        }
    }
}

static void copyProtocols(const QStringList & srcFiles, const QString & targetDirPath)
{
    cout << "installing protocols to " <<  targetDirPath.toLatin1().data() << endl;
    foreach(QString srcFilePath, srcFiles)
    {
        cout << "copying: " << srcFilePath.toLatin1().data() << endl;

        QFile srcFile(srcFilePath);
        QFileInfo srcFileInfo(srcFile);
        QString targetFileName = targetDirPath + srcFileInfo.fileName();

        cout << "to: " << targetFileName.toLatin1().data() << endl;

        bool success = srcFile.copy(targetFileName);
        if (!success)
        {
            cout << "error " << srcFile.error() << " copying: "<<  srcFile.fileName().toLatin1().data() << endl;
            exit(1);
        }
    }
}

static QStringList srcFileList()
{
    QStringList files = KGlobal::dirs()->findAllResources("data", "kio-giobridge-protocols/*.protocol");
    return files;
}

static QString targetDirPath()
{
    return KGlobal::dirs()->saveLocation("services");
    //return KGlobal::dirs()->localkdedir() + "/share/kde4/services/";
}

int main(int argc, char ** argv)
{
    if (argc <= 1)
    {
        pUsage(argc, argv);
        return 1;
    }

    QString command(argv[1]);

    if (command == "disable")
    {
        QString _targetDirPath = targetDirPath();
        cleanProtocols(_targetDirPath);
    }
    else if (command == "enable")
    {
        QStringList _srcFiles = srcFileList();
        QString _targetDirPath = targetDirPath();

        cleanProtocols(_targetDirPath);
        copyProtocols(_srcFiles, _targetDirPath);
    }
    else
    {
        pUsage(argc, argv);
        return 1;
    }
}
