summaryrefslogtreecommitdiff
path: root/Build-tools/Do-pkg
blob: 67c0e612828bc04d2bc479db1d3db33d3671aa3f (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl -w
#
# Do-pkg - convert a binary distribution into a Mac OS X PKG and put it
# inside a Disk Image (.dmg)
#
# Use the "--help" option for more info!
#
# written by Lenz Grimmer <lenz@mysql.com>
#

use Getopt::Long;
Getopt::Long::Configure ("bundling");

$opt_dry_run= undef;
$opt_help= undef;
$opt_log= undef;
$opt_mail= "";
$opt_suffix= undef;
$opt_verbose= undef;
$opt_version= undef;

GetOptions(
	"dry-run",
	"help|h",
	"log|l:s",
	"mail|m=s",
	"suffix=s",
	"verbose|v",
	"version=s",
) || &print_help;

# Include helper functions
chomp($PWD= `pwd`);
$LOGGER= "$PWD/logger.pm";
if (-f $LOGGER)
{
	do "$LOGGER";
}
else
{
	die "ERROR: $LOGGER cannot be found!\n";
}

$PM= "/Developer/Applications/PackageMaker.app/Contents/MacOS/PackageMaker";
$TMP= "/tmp/PKGBUILD";
$PKGROOT= "$TMP/PMROOT";
$PKGDEST= "$TMP/PKG";
$RESOURCE_DIR= "$TMP/Resources";
$SUFFIX= $opt_suffix;
$VERSION= $opt_version;
($MAJOR, $MINOR, $RELEASE)= split(/\./, $VERSION);
$NAME= "mysql$SUFFIX-$VERSION";
chomp($HOST= `hostname`);
chomp($ID= `whoami`);
$HOST=~ /^([^.-]*)/;
$HOST= $1;
$LOGFILE= "$PWD/Logs/$HOST-$MAJOR.$MINOR$SUFFIX.log";
$BUILDDIR= "$PWD/$HOST";
$SUPFILEDIR= <$BUILDDIR/mysql*-$VERSION/support-files/MacOSX>;
$TAR= <$BUILDDIR/$NAME-apple-darwin*-powerpc.tar.gz>;
$INFO= <$SUPFILEDIR/Info.plist>;
$DESC= <$SUPFILEDIR/Description.plist>;
@RESOURCES= qw/ ReadMe.txt postinstall preinstall /;

&print_help("") if ($opt_help || !$opt_suffix || !$opt_version);

#
# Override predefined Log file name
#
if (defined $opt_log)
{
	if ($opt_log ne "")
	{
		if ($opt_log =~ /^\/.*/)
		{
			$LOGFILE= $opt_log;
		}
		else
		{
			$LOGFILE= $PWD . "/" . $opt_log;
		}
	}
}

# Creating the UFS disk image requires root privileges
die("You must be root to run this script!") if ($ID ne "root" && !$opt_dry_run);

foreach $file ($TAR, $INFO, $DESC)
{
	&abort("Unable to find $file!") if (!-f $file);
}

# Remove old temporary build directories first
&logger("Cleaning up temporary build directories");
&run_command("rm -rf $TMP", "Could not clean up $TMP!");
&logger("Creating temp directories");
foreach $dir ($TMP, $PKGROOT, $PKGDEST, $RESOURCE_DIR)
{
	if (!-d $dir)
	{
		&run_command("mkdir $dir", "Could not make directory $dir!");
	}
}

foreach $resfile (@RESOURCES)
{
	$command= "cp $SUPFILEDIR/$resfile $RESOURCE_DIR";
	&run_command($command, "Error while copying $SUPFILEDIR/$resfile to $RESOURCE_DIR");
}

# Extract the binary tarball and create the "mysql" symlink
&logger("Extracting $TAR to $PKGROOT");
&run_command("gnutar zxf $TAR -C $PKGROOT", "Unable to extract $TAR!");
&run_command("cd $PKGROOT ; ln -s mysql* ./mysql", "Unable to create symlink!");
&run_command("chown -R root.wheel $PKGROOT/*", "Cannot chown $PKGROOT!");

# Now build the PGK using PackageMaker
# The "|| true" is a nasty hack to work around a problem with Package Maker
# returning a non-zero value, even though the package was created correctly
&logger("Running PackageMaker");
$command= "$PM -build -p $PKGDEST/$NAME.pkg -f $PKGROOT -r $RESOURCE_DIR -i $INFO -d $DESC || true";
&run_command($command, "Error while building package!");

&logger("Removing $PKGROOT");
&run_command("rm -rf $PKGROOT", "Unable to remove $PKGROOT!");

# Determine the size of the Disk image to be created and add a 5% safety
# margin for filesystem overhead
&logger("Determining required disk image size for $PKGDEST");
if (! $opt_dry_run)
{
	chomp($_= `du -sk $PKGDEST`);
	@size= split();
	$size= int($size[0]+($size[0]*0.05));
	&logger("Disk image size: $size KB");
}

&abort("Zero bytes? Something is wrong here!") if ($size == 0);

# Now create and mount the disk image
$TMPNAME= $NAME . ".tmp";
&logger("Creating temporary Disk image $TMPNAME.dmg");
$command= "hdiutil create $TMPNAME -size ${size}k -ov -fs UFS -volname $NAME";
&run_command($command, "Unable to create disk image $TMPNAME.dmg!");
&logger("Attaching Disk image $TMPNAME.dmg");
&run_command("hdid $TMPNAME.dmg", "Unable to attach $TMPNAME.dmg!");

# Install the PKG into the .dmg
chomp($mountpoint=`mount | grep "\/Volumes\/$NAME" | cut -f3 -d" "`) if (!$opt_dry_run);
&logger("Copying $PKGDEST/$NAME.pkg to Disk image /Volumes/$NAME");
&run_command("ditto $PKGDEST /Volumes/$NAME", "Could not copy $PKGDEST to /Volumes/$NAME!");
&run_command("ditto $RESOURCE_DIR/ReadMe.txt /Volumes/$NAME", "Could not copy $RESOURCE_DIR/ReadMe.txt to /Volumes/$NAME!");
chomp($mountpoint=`mount | grep "\/Volumes\/$NAME" | cut -f1 -d" "`) if (!$opt_dry_run);
&abort("/Volumes/$NAME not attached!") if (!$mountpoint && !$opt_dry_run);
&logger("Unmounting $mountpoint");
&run_command("hdiutil detach $mountpoint", "Unable to detach $mountpoint");
&run_command("rm -f $NAME.dmg", "Unable to remove $NAME.dmg!") if (-f "$NAME.dmg");
&logger("Compressing disk image");
$command= "hdiutil convert $TMPNAME.dmg -format UDZO -imagekey zlib-level=9 -o $NAME.dmg";
&run_command($command, "Unable to compress disk image!");

# Final cleanups
&logger("Removing $TMPNAME.dmg");
&run_command("rm -f $TMPNAME.dmg", "Unable to remove $TMPNAME.dmg!");
&logger("Removing $TMP");
&run_command("rm -rf $TMP", "Unable to remove $TMP!");

&logger("SUCCESS: $NAME.dmg created.") if (!$opt_dry_run);
exit 0;

sub print_help
{
	my $message= $_[0];
	if ($message ne "")
	{
		print "\n";
		print "ERROR: $message\n";
	}
	print <<EOF;

Usage: Do-pkg <options> --suffix=<suffix> --version=<version>

Creates a Mac OS X installation package (PKG) and stores it inside
a Disk Image (.dmg) file. You need to create a binary distribution
tarball with scripts/make_binary_distribution first!

NOTE: You need to run this script with root privileges (required
      to create the disk image)

Options:

    --dry-run                 Dry run without executing
-h, --help                    Print this help
-l, --log[=<filename>]        Write a log file [to <filename>]
                              (default is "$LOGFILE")
-m, --mail=<address>          Mail a failure report to the given address
                              (and include a log file snippet, if logging
                              is enabled)
                              Note that the \@-Sign needs to be quoted!
                              Example: --mail=user\\\@domain.com
    --suffix=<suffix>         The package suffix (e.g. "-standard" or "-pro)
    --version=<version>       The MySQL version number (e.g. 4.0.11-gamma)
-v, --verbose                 Verbose execution

EOF
	exit 1;
}