#!/usr/bin/env perl
my
$subcommand
=
shift
@ARGV
;
my
$scriptname
= basename $0;
my
$methodargv
= {};
my
$hainekocmd
=
undef
;
my
$commandset
=
undef
;
my
$daemonargs
= [
'start'
,
'stop'
,
'reload'
,
'restart'
,
'status'
];
if
(
$subcommand
) {
$commandset
=
join
(
' '
,
$scriptname
,
@ARGV
);
if
(
grep
{
$subcommand
eq
$_
}
@$daemonargs
) {
my
$scriptroot
= Path::Class::Dir->new( dirname $0 );
my
$serverroot
=
$scriptroot
->parent->absolute();
my
$procidfile
=
sprintf
(
"%s/run/haineko.pid"
,
$serverroot
);
$procidfile
=
'/tmp/haineko.pid'
unless
-w
$serverroot
.
'/run'
;
$methodargv
= {
'command'
=>
$commandset
,
'pidfile'
=>
$procidfile
,
};
$hainekocmd
= Haineko::CLI::Daemon->new(
%$methodargv
);
if
(
$subcommand
eq
'start'
) {
$SIG
{
'INT'
} =
sub
{
$hainekocmd
->removepf;
exit
(0);
};
$hainekocmd
->parseoptions;
$hainekocmd
->run;
}
else
{
$hainekocmd
->parseoptions;
if
(
$subcommand
=~ m/(?:stop|reload|restart)/ ) {
$hainekocmd
->ctrl(
$subcommand
);
}
else
{
my
$p
=
$hainekocmd
->readpf;
printf
( STDERR
"pid = %d\n"
,
$p
)
if
defined
$p
;
}
}
}
else
{
if
(
$subcommand
eq
'help'
||
$subcommand
eq
'--help'
) {
my
$climodules
= [
'Daemon'
,
'Setup'
,
'Password'
];
$methodargv
= {
'command'
=>
$commandset
};
$hainekocmd
= Haineko::CLI::Help->new(
%$methodargv
);
for
my
$e
(
@$climodules
,
''
) {
my
$c
=
'Haineko::CLI'
;
$c
.=
'::'
.
$e
if
length
$e
;
Module::Load::load(
$c
);
$hainekocmd
->add(
$c
->help(
'subcommand'
),
'subcommand'
);
}
$hainekocmd
->mesg;
help();
exit
(0);
}
elsif
(
$subcommand
eq
'version'
) {
Haineko::CLI->version;
exit
(0);
}
elsif
(
$subcommand
=~ m/(?:pw|passwd|password)/ ) {
$methodargv
= {
'command'
=>
$commandset
};
$hainekocmd
= Haineko::CLI::Password->new(
%$methodargv
);
$hainekocmd
->parseoptions;
print
$hainekocmd
->make;
print
"\n"
;
exit
(0);
}
elsif
(
$subcommand
eq
'setup'
||
$subcommand
eq
'make-setup-data'
) {
$methodargv
= {
'command'
=>
$commandset
};
$hainekocmd
= Haineko::CLI::Setup->new(
%$methodargv
);
$hainekocmd
->parseoptions;
if
(
$subcommand
eq
'setup'
) {
$hainekocmd
->init;
}
else
{
$hainekocmd
->make;
exit
(0);
}
}
else
{
printf
( STDERR
"Unknown sub command: %s\n"
,
$subcommand
);
exit
(1);
}
}
}
else
{
printf
( STDERR
"Try %s help\n"
, basename
$scriptname
);
}
sub
help {
printf
( STDERR
"Try '%s SUBCOMMAND --help'\n"
,
$scriptname
);
}