#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>
#include <unistd.h>


int  save_all = 0;
int save_none = 1;    

DBusConnection *connect_dbus()
{
    DBusError err;
    DBusConnection *connection;
    int ret;

    dbus_error_init(&err);
    /* Connect to Bus*/
    connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
    if(dbus_error_is_set(&err))
    {
        fprintf(stderr,"Connection Err : %s\n",err.message);
        dbus_error_free(&err);
        exit(1);
    }

    ret = dbus_bus_request_name(connection,"test.wei.source", DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
    if(dbus_error_is_set(&err)) 
    {
        fprintf(stderr,"Name Err : %s\n",err.message);
        dbus_error_free(&err);
    }

    return connection;
}

void send_a_method_call(DBusConnection *connection, const char *method_name, int my_int)
{
    DBusError err;
    DBusMessage *msg, *reply;
    DBusMessageIter iter;

    dbus_error_init(&err);

    msg = dbus_message_new_method_call ("com.kylin.MaintainMode","/com/kylin/MaintainMode/Object","com.kylin.MaintainMode.interface", method_name);

    if(msg == NULL) 
    {
        g_printerr("Message NULL");
        exit(1);
    }

    if(method_name != "OpenMaintainMode")
    {
        dbus_message_iter_init_append(msg, &iter);  
        if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &my_int)) 
        {
            fprintf(stderr, "Failed to append arguments\n"); 
            dbus_message_unref(msg);
            exit(1);
            }
    }

    

    reply = dbus_connection_send_with_reply_and_block(connection, msg, -1, &err);
    if(dbus_error_is_set(&err))
    {
        fprintf(stderr, "Method call failed: %s\n", err.message);
        dbus_error_free(&err);
        dbus_message_unref(msg);
        exit(1);
    }

    if (reply)
    {
        dbus_message_iter_init(reply, &iter);
        dbus_bool_t value;
        dbus_message_iter_get_basic(&iter, &value);
        printf("Method returned: %s\n", value ? "true" : "false");
        dbus_message_unref(reply);
    }

    dbus_connection_flush(connection);
    dbus_message_unref(msg);
}

static void usage (void)
{
	    printf   ("用法: ./gbus [OPTIONS]\n"
	              "OPTIONS\n"
	              "  -c, --mmc                        关闭运维模式\n"
	              "  -o, --mmo                        打开运维模式\n"
	              "  -c -a, --save-all                               \n"
	              "  -c -n, --save-none                              \n"
	              "\n"
                  );
}

int main(int argc , char *argv[])
{
    int c = 0;
    int k = 0;
    DBusConnection * connection;
    connection = connect_dbus();

    if(connection == NULL) 
    {
        return -1;
    }

    if(argc < 2)
    {
        printf("%s: 没有提供除程序名外的任何参数\n", argv[0]); 
        printf("请尝试使用 %s -h 来获取更多信息\n", argv[0]);
        return 1; 
    }  

    while ((c = getopt(argc, argv, "oc::h")) != -1)
    {
        switch (c) 
        {
        case 'o':
            if(argc > 2)
            {
                return -1;
            }
            printf("打开运维模式 \n");  
            send_a_method_call(connection, "OpenMaintainMode",NULL);
            break;
        case 'c':
            if(argc <= 2)
            {
                printf("关闭运维模式 \n");
                send_a_method_call(connection, "CloseMaintainMode",NULL);
            }

            while ((k = getopt(argc, argv, "an")) != -1) 
            {
                switch (k) 
                {
                case 'a':
                    send_a_method_call(connection, "CloseMaintainMode", save_all);
                    break;  
                case 'n':
                    send_a_method_call(connection, "CloseMaintainMode", save_none); 
                    break;
                }
            break;
        case 'h':  
            usage();
            break;
        default:  
            abort();  
        }
    }
    }

    return 0;
}


