Bug 25538: Default to --login|-i if no command passed
[koha.git] / debian / scripts / koha-shell
1 #!/usr/bin/perl
2 # koha-shell -- put you in a shell with a koha environment set up
3 # Copyright 2012  Catalyst IT, Ltd
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 use Getopt::Long;
19 use Modern::Perl;
20
21 Getopt::Long::Configure("bundling");
22
23 my %opts;
24 my $res = GetOptions( \%opts, "command|c=s", "help|h", "login|l", "shell|s=s",
25     "preserve-environment|p|m", "verbose|v" );
26
27 if ( !$res || $opts{help} ) {
28     show_help( !$res );
29     exit( !$res );
30 }
31
32 if ( !@ARGV ) {
33     show_help( 1, "An instance name must be supplied." );
34     exit(1);
35 }
36 my $instance = shift @ARGV;
37 if ( !-e "/etc/koha/sites/$instance" ) {
38     show_help( 1, "The instance doesn't exist: $instance" );
39     exit(1);
40 }
41 my $shell = $opts{shell} || $ENV{SHELL} || '/bin/sh';
42
43 # Now we're set up, build the 'su' command
44 my $perl5lib = read_perl5lib( $instance );
45 my @su_args;
46 push @su_args, '/usr/bin/sudo';
47 push @su_args, '--preserve-env' if $opts{'preserve-environment'};
48 push @su_args, '--login' if $opts{login} || !$opts{command};
49 push @su_args, "-u", "$instance-koha";
50 push @su_args,
51     "env "
52   . "KOHA_CONF=/etc/koha/sites/$instance/koha-conf.xml "
53   . "PERL5LIB=$perl5lib $shell"
54   . ( $opts{command} ? " -c '$opts{command}'" : '' );
55
56 print "Command: '".join("' '",@su_args)."'\n" if $opts{verbose};
57 system("@su_args");
58 if ( $? == -1 ) {
59     print STDERR "failed to execute: $!\n";
60 }
61 elsif ( $? & 127 ) {
62     printf STDERR "child died with signal %d, %s coredump\n",
63       ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
64 }
65
66 exit $? >> 8;
67
68 sub show_help {
69     my ( $err, $msg ) = @_;
70
71     my $fh = $err ? *STDERR : *STDOUT;
72     say $fh "Error: " . $msg if $msg;
73     print $fh $_ while <DATA>;
74 }
75
76 sub read_perl5lib {
77     my ( $instance ) = @_;
78
79     # This simulates what the debian shell scripts do:
80     # Read /etc/default/koha-common
81     # Check dev_install in koha-conf.xml
82
83     my $result = `grep "^PERL5LIB=" /etc/default/koha-common`;
84     chomp $result;
85     $result =~ s/^PERL5LIB=\s*//;
86     my $dev_install = `xmlstarlet sel -t -v 'yazgfs/config/dev_install' /etc/koha/sites/$instance/koha-conf.xml`;
87     chomp $dev_install;
88     if ( $dev_install ) {
89         # pick PERL5LIB from the intranetdir entry
90         $result = `xmlstarlet sel -t -v "yazgfs/config/intranetdir" /etc/koha/sites/$instance/koha-conf.xml`;
91     }
92     return $result;
93 }
94
95 __DATA__
96 koha-shell -- gives you a shell with your Koha environment set up
97
98 Usage: koha-shell [options] [instance name]
99
100 Options:
101     -c, --command COMMAND   pass COMMAND to the invoked shell
102     -h, --help              show this help and quit
103     -l, --login             make the shell a login shell
104     -m, -p,
105     --preserve-environment  do not reset environment variables
106     -s, --shell SHELL       use SHELL instead of the one from your environment
107     -v, --verbose           output the full command that will be executed
108
109 The default shell is the one currently in use. Refer to su(1) for more detail
110 on these options.