#!/usr/bin/env python3

import socket
import sys

def run(snap):
    clientsocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    clientsocket.connect("/run/snapd.socket")

    body = "{ \"action\": \"install\" } "
    clientsocket.sendall("""POST http://localhost/v2/snaps/{snap} HTTP/1.1
Host: localhost
User-Agent: agent
Content-Type: application/json
Accept: */*
Content-Length: {length}

{body}""".format(snap=snap, length=str(len(body)), body=body).encode("utf-8"))
    return clientsocket.recv(8192)

if __name__ == '__main__':
    run(sys.argv[1])
