File: postinst

package info (click to toggle)
blootbot 1.1.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,420 kB
  • ctags: 586
  • sloc: perl: 15,941; sh: 154; makefile: 56; sql: 45
file content (132 lines) | stat: -rw-r--r-- 4,418 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl -w

use strict;
use Debconf::Client::ConfModule qw(:all);

my $version = version(2.0);

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

my %action = (
	      configure => sub {
		my $users = "/var/lib/blootbot/blootbot.users";
		my $chan = "/var/lib/blootbot/blootbot.chan";
		my $servers = "/etc/blootbot/ircII.servers";
		my $config = "/etc/blootbot/blootbot.config";
		
		if (! -e $users) {
		  print STDERR "Creating sample /var/lib/blootbot/blootbot.users, you will need\n";
		  print STDERR "to edit this and set a password before the bot is ready for use\n";
		  print STDERR "See /usr/share/doc/blootbot/README.Debian for details\n\n";
		  system("cp /usr/share/blootbot/data/template.users $users 1>&2");
		}

		if (! -e $chan) {
		  # I don't feel the need to warn about this, it's dynamically configurable via DCC
		  system("cp /usr/share/blootbot/data/template.chan $chan 1>&2");
		}

		if (! -e $servers) {
		  print STDERR "The bot will by default join Open Projects Net, edit\n";
		  print STDERR "/etc/blootbot/ircII.servers to change this\n";
		  system("cp /usr/share/blootbot/data/template.servers $servers 1>&2");
		}

		if (! -e $config) {
		  print STDERR "Creating a default config file as /etc/blootbot/blootbot.config\n";
		  system("cp /usr/share/blootbot/data/template.config $config 1>&2");
		}

		# Parse the debconf stuff into the config file
		{
		  my $name = get('blootbot/name');
		  my $password = get('blootbot/password');
		  my $db_host = get('blootbot/db/host');
		  my $db_username = get('blootbot/db/username');
		  my $db_name = get('blootbot/db/name');
		  my $db_password = get('blootbot/db/password');
		
		  # Abusive loading of the offending file...
		  my @lines = `cat $config`;

		  rename $config,"$config.old";
		  open (OUT,">$config");

		  my $line;
		  my $auto = 0;
		  foreach $line (@lines) {
		    $auto = 1 if $line =~ /^\#AUTO /;
		    if ($auto)
		      {
			$line =~ s/^(\s*set\s+ircNick\s+).*$/$1$name/;
			$line =~ s/^(\s*set\s+nickServ_pass\s+).*$/$1$password/;
			$line =~ s/^(\s*set\s+SQLHost\s+).*$/$1$db_host/;
			$line =~ s/^(\s*set\s+DBName\s+).*$/$1$db_name/;
			$line =~ s/^(\s*set\s+SQLUser\s+).*$/$1$db_username/;
			$line =~ s/^(\s*set\s+SQLPass\s+).*$/$1$db_password/;
		      }
		    print OUT $line;
		  }
		  close OUT;
		}

		# Create the database and set up permissions (blootbot can create the tables itself)
		my $rc = system("/usr/sbin/blootbotsetup </dev/tty >/dev/tty") >> 8;
		die "blootbotsetup failed, rc == $rc" if $rc;

		# Do we already have the "blootbot" user?
		if (!defined getpwnam("blootbot")) {
		  system("adduser --disabled-password --no-create-home --system blootbot >/dev/tty");
		}

		system("chown -R blootbot /var/log/blootbot /var/run/blootbot /var/lib/blootbot /etc/blootbot >/dev/tty");
		system("chmod -R 700 /etc/blootbot /var/log/blootbot /var/lib/blootbot >/dev/tty");
	      },
	      'abort-upgrade' => sub {},
	      'abort-remove' => sub {},
	      'abort-deconfigure' => sub {}
);

my $mode = shift @ARGV;

if (defined $mode) {
  if (exists $action{$mode}) {
    $action{$mode}->();
  } else {
    print STDERR "postinst called with unknown argument '$mode'\n";
    exit 0;
  }
} else {
  print STDERR "postinst called without arguments\n";
  exit 0;
}

# This handles the debhelper stuff. Gotta love perl :)
# Let's get back to real stdout, not debconf
close STDIN;
close STDOUT;
open(STDIN, "</dev/tty");
open(STDOUT, ">/dev/tty");

my $temp="set -e\nset -- @ARGV\n" . << 'EOF';
#DEBHELPER#
EOF
system ($temp) / 256 == 0
  or die "Problem with debhelper scripts: $!";

exit 0;