| 12
 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
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 
 | #!/usr/bin/perl
#
# Setup
#
# Directives
use strict;
use warnings;
use File::Basename;
use File::Copy;
# Figure out the mode
my $mode = shift @ARGV;
my $framework_prefix = "/usr/lib/mono";
if (!defined $mode)
{
    print STDERR "E: You must supply a mode\n";
    print STDERR "E: Use: install-framework, install, remove-framework, remove, or name\n";
    exit 1;
}
# Name is simply
if ($mode eq "name")
{
    print "Mono\n";
    exit 0;
}
# Emergency System.Native workaround for Mono versions where gacutil does not
# run without a System.Native mapping defined in /etc/mono/config
# See bug #954635
my $monoconfig = '/etc/mono/config';
if (!-e "$monoconfig") {
if (-e "$monoconfig.dpkg-new") {
move("$monoconfig.dpkg-new","$monoconfig") or die "Unable to apply System.Native workaround";
}
}
# Get the base directory
my $basedir = "/usr/share/cli-common/packages.d/";
# Get the base file
my $basename = shift @ARGV;
# We're looking to install a framework
# The program gets the name of the package, then a list of 
# the (framework versionĂ—10, full path) pairs
if ($mode eq "install-framework")
{
    # TODO: We could detect this ourselves
    my %runtime_versions = (20 => "$framework_prefix/2.0",
			    35 => "$framework_prefix/3.5",
			    40 => "$framework_prefix/4.0", 
			    45 => "$framework_prefix/4.5");
    my $uninstall = "$basedir/$basename.mono-framework";
    open UNINSTALL, ">$uninstall"
	or die "E: Cannot open uninstall: $uninstall";
    while (@ARGV)
    {
	my $framework_ver = int(shift @ARGV);
	my $dll = shift @ARGV;
	if (! exists($runtime_versions{$framework_ver}))
	{
	    printf STDERR "W: Attempted to install framework library for unsupported version $framework_ver";
	    next;
	}
	my $target = "$runtime_versions{$framework_ver}/" . basename($dll);
	
	if (-f $target)
	{
	    # Ensure we're idempotent
	    unlink $target;
	}
	symlink $dll, $target
	    or die "E: Unable to install $dll into framework path: $target\n";
	print UNINSTALL "$target\n";
    }
    close UNINSTALL;
    exit 0;
}
# Removing is also simple
if ($mode eq "remove-framework")
{
    # Get the uninstall file
    my $uninstall = "$basedir/$basename.mono-framework";
    if (-f $uninstall)
    {
	# Go through the file
	open UNINSTALL, "<$uninstall" or
	    die "E: Cannot open uninstall file ($!)";
	while (<UNINSTALL>)
	{
	    chomp;
	    unlink($_) or
		printf STDERR "E: Unable to remove $_\n";
	}
	close UNINSTALL;
	unlink($uninstall);
    }
    # We are good
    exit 0;
}
# This program gets the name of a file (ending in .installcligac) and
# a list of assemblies to install, as full paths. The ones given are
# the only ones that passed the white/blacklisting.
my $cligac = "/usr/share/cli-common/packages.d/$basename.installcligac";
if (! -f $cligac)
{
    print STDERR "E: File does not exist: $cligac\n";
    exit 1;
}
# Removing is also simple
if ($mode eq "remove")
{
    # Get the uninstall file
    my $uninstall = "$basedir/$basename.mono";
    if (-f $uninstall)
    {
	# Go through the file
	open UNINSTALL, "<$uninstall" or
	    die "E: Cannot open uninstall file ($!)";
	while (<UNINSTALL>)
	{
		my $assembly = $_;
		chomp($assembly);
		# The uninstall file can contain two formats - full paths to non-assembly entries
		# (i.e. FSharp sigdata/optdata files), or assembly signature stuff in the format
		# "name, Version=x.x.x.x, Culture=neutral, PublicKeyToken=yyyyyyyyyyyyy"
		#
		# We can switch behaviour, based on whether it is a path or not (if it is a path,
		# it has a leading /)
		#
		# If it's not a path, pass the entry to gacutil to uninstall
		if ($assembly !~ /^\//)
		{
		    my $cmd = "/usr/bin/gacutil -u $assembly > /dev/null";
		    my $res = system($cmd);
		    if ($res > 0) {
			print STDERR "W: removing assembly: $assembly failed!\n";
		    }
		}
		# If it is a path, unlink.
		#
		# This is often not actually needed - if a parent assembly is uninstalled
		# from the GAC, the sigdata/optdata files are cleaned automatically
		#
		# We manually unlink if these files are specifically named anyway, in case
		# we ever want to keep companion files in different packages (where they would
		# not be uninstalled by the same GAC cleaning run)
		else
		{
		    # Definitely a link, unlink it please
		    if (-l "$assembly")
		    {
			unlink $assembly;
		    }
		    # The file exists, but is not a link, which means we didn't put it
		    # there, so panic!
		    elsif (-f "$assembly")
		    {
			print STDERR "W: removing non-assembly file: $assembly failed!\n";
		    }
		    # If the file doesn't exist, we do nothing - we don't WANT it to exist
		}
	}
	close UNINSTALL;
	# Unlike the file
	unlink($uninstall);
    }
    # We are good
    exit 0;
}
# The only thing left should be "install"
if ($mode ne "install")
{
    print STDERR "E: Unknown mode: $mode\n";
    print STDERR "E: Use: install-framework, install, remove-framework,  remove or name\n";
    exit 1;
}
# Open up our uninstall file
open UNINSTALL, ">$basedir/$basename.mono"
    or die "E: Cannot open uninstall: $basedir/$basename.mono";
# Go through the file
open CLIGAC, "<$cligac" or die "E: Cannot open: $cligac ($!)";
while (@ARGV)
{
    # Get the assembly name
    my $dll = shift @ARGV;
    
    # Make sure it is there
    if (! -f $dll)
    {
	print STDERR "E: Assembly does not exist: $dll\n";
	exit 1;
    }	
    
    # Split the provided assembly path into its components - folder, basename, and suffix.
    # All three are useful
    my($assemblyfilename, $assemblypath, $assemblysuffix) = (fileparse($dll, qr/\.[^.]*/));
    # If the suffix is .dll, assume this is a simple CLI assembly, and use gacutil for
    # processing
    if (( $assemblysuffix eq ".dll" ) || ( $assemblysuffix eq ".exe" ))
    {
	# Figure out the mono's precise name
	my $fullname = get_full_name($dll);
	# Write out the uninstall file
	print UNINSTALL "$fullname\n";
	# Install the file. We use the "../../../.." to make it a
	# relative path to this program (since gacutil doesn't like
	# absolute paths). There isn't a problem of doing too many
	# since we typically run from the root context.
	my $cmd = "(cd `dirname $dll` && "
	    . "/usr/bin/gacutil -i `basename $dll`"
	    . " > /dev/null)";
	system($cmd) == 0 or die "E: installing Assembly $dll failed\n";
    }
    else
    {
	# If the extension is not .dll, this is some other file format (e.g. FSharp
	# sigdata/optdata) and we cannot use gacutil.
	#
	# First, we determine the path of the assembly which accompanies this data file
	my $parentassembly = "$assemblypath$assemblyfilename.dll";
	# Then extract the assembly information from this "parent" assembly, such as the
	# version and signing token
	my $fullname = get_full_name($parentassembly);
	my($parentname, $parentver, $parentculture, $parenttoken) = split(/, [a-zA-z]*=/, $fullname);
	# And finally, we construct a path to where we know Mono will GAC-install the
	# parent assembly, and put a symlink in there
	my $targetpath = "$framework_prefix/gac/$parentname/$parentver\__$parenttoken/$assemblyfilename$assemblysuffix";
	symlink($dll, $targetpath);
	# And write the path to the symlink into the uninstall file
	print UNINSTALL "$targetpath\n";
    }
}
close CLIGAC;
close UNINSTALL;
# Finish up successfully
exit 0;
# Get the name of the assembly in a manner suitable for uninstall
# using gacutil.
sub get_full_name
{
    # Get the name
    my $dll = shift;
    # Open a pipe to monop
    my $cmd = "LANG=C /usr/bin/mono /usr/share/mono/MonoGetAssemblyName.exe $dll";
    open PIPE, "$cmd |" or die "E: Cannot open pipe to assembly builder $dll";
    # This generate a single line that produces the desired results
    $_ = <PIPE>;
    chomp;
	 # assembly1, Version=1.0.0.0, Culture=en, PublicKeyToken=0123456789abcdef
    return $_;
}
 |