aboutsummaryrefslogtreecommitdiff
path: root/games/maelstrom/maelstrom_addon_package.pl
blob: 5d3c7bdce3c3ca66ef5244c4647c79bab9d69755 (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
#!/usr/bin/perl -w

# maelstrom_addon_package.pl - create a Slackware tgz
# package from a Maelstrom addon zip file, for use with
# the SlackBuilds.org maelstrom package.

# Beware: only *one* addon may be installed at a time!

# This script is meant to work on a stock Slackware system, so it
# doesn't use any CPAN modules (which makes the code a bit awkward).

# Author: B. Watson (urchlay@slackware.uk)

# This program is released into the public domain; do as ye list wi' her.
# Author not responsible for any damages resulting from the use of this
# program.

use strict;
use File::Find; # core Perl module, not CPAN

our $GAMEDIR = "/usr/share/games/Maelstrom";

chomp(our $pkgver = `date +%Y%m%d`);

our $pkgname;
our $spritefile;
our $soundfile;
our @text_files;

sub usage() {
	warn <<EOF;
Usage: maelstrom_addon_package.pl zipfile [pkgname]

Creates a Slackware tgz package from a Maelstrom addon zip file. The
zipfile may be either a local file (e.g. "my_addon.zip") or a URL
(e.g. "http://example.com/foo_addon.zip").

The output file will be located in /tmp, and will be named

	maelstrom_addon_pkgname-YYYYMMDD-noarch-1_mael.tgz

where pkgname is the [pkgname] argument, or derived from the zipfile name if
no [pkgname] is given on the command line, and YYYYMMDD is the current date.

The resulting .tgz package is suitable for installation with installpkg.
However, only one Maelstrom addon package may be installed at a time
(since the addon filenames are the same, the 2nd one would overwrite the
first one).

Since the script must be able to create files as the root user, you
must run it with root privileges (e.g. with su or sudo).

You can find a collection of Maelstrom add-on zip files here:

	https://www.libsdl.org/projects/Maelstrom/add-ons.html
EOF
	exit 1;
}


sub make_temp_dir() {
	my $dir = "/tmp/maelstrom_addon_" . rand(10000000) . $$;
	system("mkdir -p \"$dir\"") && die "can't create $dir\n";
	return $dir;
}

sub extract($$) {
	my $tmpdir = shift;
	my $archive = shift;
	system("cd \"$tmpdir\" && unzip \"$archive\"") &&
		die "can't extract $archive in $tmpdir\n";
}

sub cleanup(@) {
	for my $tmpdir (@_) {
		system("cd \"$tmpdir\" && rm -rf *");
		system("cd / && rmdir \"$tmpdir\"");
	}
}

sub make_slack_desc($) {
	my $package_dir = shift;

	mkdir($package_dir . "/install");
	open my $f, ">$package_dir/install/slack-desc";
	print $f "$pkgname: Maelstrom add-on sprites/sound package\n";
	print $f "$pkgname:\n" for (1..10);
	close $f;
}

sub copy_files($) {
	my $package_dir = shift;

	system("mkdir -p $package_dir/usr/share/games/Maelstrom");
	system("mkdir -p $package_dir/usr/doc/$pkgname-$pkgver");

	system("cp \"$_\" $package_dir/usr/doc/$pkgname-$pkgver") for @text_files;

	if($spritefile) {
		system("cp \"$spritefile\" " .
				"$package_dir/usr/share/games/Maelstrom/\%Maelstrom_Sprites");
	}

	if($soundfile) {
		system("cp \"$soundfile\" " .
				"$package_dir/usr/share/games/Maelstrom/\%Maelstrom_Sounds");
	}
}

sub wanted {
	my $file = $File::Find::fullname;
	if(/\.txt$/i) {
		warn "Found text file: $file\n";
		push @text_files, $file;
	} elsif(/\%?maelstrom.sounds(?:\..*)?$/i) {
		warn "Found sounds: $file\n";
		warn "warning: Duplicate sounds file $file\n" if $soundfile;
		$soundfile = $file;
	} elsif(/\%?maelstrom.sprites(?:\..*)?$/i) {
		warn "Found sprites: $file\n";
		warn "warning: Duplicate sprites file $file\n" if $spritefile;
		$spritefile = $file;
	}
}

# main()
my $archive = shift || usage();
usage() if $archive =~ /^--?(?:\?|h(?:elp)?)/i;

die "You must run this script as root\n" if $> != 0;

$pkgname = shift || $archive;
for($pkgname) {
	s/.*\///;
	s/\..*//;
	s/-/_/g;
	s/\W//g;
	y/A-Z/a-z/;
}

die "Invalid package name\n" unless $pkgname;

$pkgname =~ s/^/maelstrom_addon_/;

if($archive =~ /^(?:ht|f)tps?:\/\//) {
	system("wget -O /tmp/$pkgname.zip \"$archive\"");
	$archive = "/tmp/$pkgname.zip";
}

my $unzip_dir = make_temp_dir();

extract($unzip_dir, $archive);
find({ wanted => \&wanted, follow => 1 }, $unzip_dir);

unless($spritefile or $soundfile) {
	die "Can't find any sprites or sounds, aborting\n";
}

my $package_dir = make_temp_dir();

copy_files($package_dir);
make_slack_desc($package_dir);

chdir($package_dir) or die $!;
system("/sbin/makepkg -l y -c y /tmp/$pkgname-$pkgver-noarch-1_mael.tgz");
chdir("/");
cleanup($package_dir, $unzip_dir);