package org.xbib.net.socket.notify;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xbib.net.socket.v4.unix.UnixDatagramSocket;

public class SystemdNotify {

    private static final Logger logger = Logger.getLogger(SystemdNotify.class.getName());

    public SystemdNotify() {
    }

    public static void sendNotify() throws IOException {
        sendNotify("READY=1");
    }

    public static void sendNotify(String text) throws IOException {
        String socketName = System.getenv("NOTIFY_SOCKET");
        if (socketName != null) {
            UnixDatagramSocket socket = new UnixDatagramSocket(socketName);
            socket.send(text.getBytes(StandardCharsets.US_ASCII), text.length());
            socket.close();
        } else {
            logger.log(Level.WARNING, "no socket path in NOTIFY_SOCKET environment variable");
        }
    }
}
