#!/usr/bin/perl

# Copyright 2018 Cameron Kaiser.
# All rights reserved.

$JSON_DIR = "$ENV{'HOME'}/Library/Application Support/Mozilla/NativeMessagingHosts";

if (! -x "/Applications/Onyx.app/Contents/MacOS/onyx") {
	&dialog("Onyx.app must be in /Applications.");
}
if (-e "${JSON_DIR}/onyx.json") {
	&dialog("Onyx appears to be already installed for this user.");
}

system("/bin/mkdir", "-p", $JSON_DIR);
if (open(K, ">", "${JSON_DIR}/onyx.json")) {
	print K <<'EOF';
{
  "name": "onyx",
  "description": "OverbiteNX Gopher system component",
  "path": "/Applications/Onyx.app/Contents/MacOS/onyx",
  "type": "stdio",
  "allowed_extensions": [ "overbitenx@floodgap.com" ]
}
EOF
	close(K);
	&dialog("Onyx was successfully installed for this user.");
} else {
	&dialog("Onyx was unable to install: $!\n");
}

sub dialog {
	exec("/usr/bin/osascript", "-e", <<"EOF");

tell application "SystemUIServer"
activate
display dialog "@_" buttons ("OK") default button "OK"
end tell
EOF

}
