/*
 * vim:ts=4:sw=4:expandtab
 *
 * i3 - an improved dynamic tiling window manager
 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
 *
 * i3-msg/main.c: Utility which sends messages to a running i3-instance using
 * IPC via UNIX domain sockets.
 *
 * This (in combination with libi3/ipc_send_message.c and
 * libi3/ipc_recv_message.c) serves as an example for how to send your own
 * messages to i3.
 *
 * Additionally, it’s even useful sometimes :-).
 *
 */
#include "libi3.h"

#include <err.h>
#include <getopt.h>
#include <i3/ipc.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <yajl/yajl_parse.h>

/*
 * Having verboselog() and errorlog() is necessary when using libi3.
 *
 */
void verboselog(char *fmt, ...) {
    va_list args;

    va_start(args, fmt);
    vfprintf(stdout, fmt, args);
    va_end(args);
}

void errorlog(char *fmt, ...) {
    va_list args;

    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}

static char *last_key = NULL;

int main(int argc, char *argv[]) {
#if defined(__OpenBSD__)
    if (pledge("stdio rpath unix", NULL) == -1)
        err(EXIT_FAILURE, "pledge");
#endif
    char *socket_path = NULL;
    int o, option_index = 0;
    char *payload = NULL;

    static struct option long_options[] = {
        {"socket", required_argument, 0, 's'},
        {"version", no_argument, 0, 'v'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}};

    char *options_string = "s:t:vhqm";

    while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
        if (o == 's') {
            free(socket_path);
            socket_path = sstrdup(optarg);
        } else if (o == 'v') {
            puts("i3-msg " I3_VERSION "\n");
            return 0;
        } else if (o == 'h') {
            puts("i3-msg " I3_VERSION "\n");
            puts("i3-msg [-s <socket>] [-m] <message>\n");
            return 0;
        } else if (o == '?') {
            exit(EXIT_FAILURE);
        }
    }

    /* Use all arguments, separated by whitespace, as payload.
     * This way, you don’t have to do i3-msg 'mark foo', you can use
     * i3-msg mark foo */
    while (optind < argc) {
        if (!payload) {
            payload = sstrdup(argv[optind]);
        } else {
            char *both;
            sasprintf(&both, "%s %s", payload, argv[optind]);
            free(payload);
            payload = both;
        }
        optind++;
    }

    if (!payload)
        payload = sstrdup("");

    int sockfd = ipc_connect(socket_path);
    if (ipc_send_message(sockfd, strlen(payload), (uint8_t *)payload) == -1)
        err(EXIT_FAILURE, "IPC: write()");
    free(payload);

    uint32_t reply_length;
    uint8_t *reply;
    int ret;
    if ((ret = ipc_recv_message(sockfd, &reply_length, &reply)) != 0) {
        if (ret == -1)
            err(EXIT_FAILURE, "IPC: read()");
        exit(1);
    }
    printf("%s", reply);
    return 0;
}
