{
DaemonApplication a(argc, argv, help);
std::string devicePath, bootVersion;
uint16_t port=2977;
int i, j=1;
for(i=1; i<argc; i++) {
const char * arg=argv[i];
if(arg[0]=='-') {
if(strcmp(arg, "-p")==0) {
CoreApplication::checkOptionArg(i, argc, argv, true);
port=atoi(argv[i]);
} else if(strcmp(arg, "-d")==0) {
CoreApplication::checkOptionArg(i, argc, argv, true);
devicePath=argv[i];
} else if(strcmp(arg, "-boot-screen")==0) {
CoreApplication::checkOptionArg(i, argc, argv, true);
bootVersion=argv[i];
} else {
fprintf(stderr, "%s: bad option %s, see -help\n", a.applicationName(), argv[i]);
CoreApplication::exit(2);
}
} else {
argv[j++]=argv[i];
}
}
if(j < argc) {
argv[j]=0;
argc=j;
}
if(argc>1) {
fprintf(stderr, "%s: bad option %s, see -help\n", a.applicationName(), argv[1]);
CoreApplication::exit(2);
}
if(devicePath.size()==0) {
fprintf(stderr, "%s: missing device path, see -help\n", a.applicationName());
CoreApplication::exit(2);
}
std::vector<std::string> * devicePaths=File::complete(devicePath);
switch(devicePaths->size()) {
case 0:
fprintf(stderr, "%s: no device named %s\n", a.applicationName(), devicePath.data());
delete devicePaths;
CoreApplication::exit(2);
case 1:
devicePath=devicePaths->front();
delete devicePaths;
break;
default:
fprintf(stderr, "%s: more than one device named %s\n", a.applicationName(), devicePath.data());
delete devicePaths;
CoreApplication::exit(2);
}
if(bootVersion.size()>0) {
CrystalfontzDevice d(devicePath);
d.connect();
d.configureBoot(bootVersion);
return 0;
}
a.fork();
a.start();
EventLoop loop;
CrystalfontzDevice * device=new CrystalfontzDevice(devicePath);
device->connect();
if(!EventLoop::instance()->isTerminated()) {
device->firstPage();
loop.addStream(device);
CrystalfontzServer * server=new CrystalfontzServer(device);
while(!server->listen(port, 50)) {
Log::write(1, "cannot listen to port %hu, retry in 10 seconds\n", port);
sleep(10);
}
loop.addStream(server);
loop.exec();
}
a.close();
return 0;
}