summaryrefslogtreecommitdiffstats
path: root/Mac/OSX/Dist/makedmg
blob: de13ef93bbb69a352d990572ab6c16239acb21be (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
#!/usr/bin/perl -w
#
# make disk image from folder
#
# usage: makedmg src dst name
#
# Donated by Frank Vercruesse


$hdiUtilExec = "/usr/bin/hdiutil";
$hdiDrvExec = "/usr/bin/hdid";
$newfsExec = "/sbin/newfs_hfs";
$duExec = "/usr/bin/du";
$dittoExec = "/usr/bin/ditto";

if ( $#ARGV != 2 ) {
	die "Wrong number of arguments.\nUsage: makedmg src dst name\n";
}

&make_dmg( $ARGV[0], $ARGV[1], $ARGV[2]);


sub make_dmg
{
	my $src = $_[0];
	my $dst = $_[1];
	my $name = $_[2];

	# check dirs
	if( not -d $dst && -d $src ) {
		die "src and dst must be directories\n";
	}

	# calc disk image size
	if( not open( MYPIPE, "$duExec -sk \"${src}\" |") ) {
		die "couldn't open pipe\n";
	}
	(my $dmgsize) = split( /\s+/, <MYPIPE>);
	close( MYPIPE);
	$dmgsize /= 1024;
	$dmgsize = int($dmgsize + 4);
	if( $dmgsize < 5 ) {
		$dmgsize = 5
	}

	# create disk image
	system "cd \"$dst\"; $hdiUtilExec create -megabytes $dmgsize -ov \"_${name}\"";
	if( $? ) { die "couldn't create disk image\n"; }

	# format disk image
	if( not open( MYPIPE, "cd \"$dst\"; $hdiDrvExec -nomount \"_${name}.dmg\" |") ) {
		die "couldn't open pipe\n";
	}
	(my $dev) = split( /\t/, <MYPIPE>);
	$dev =~ s/^(.*\S)\s*$/$1/;
	my( $part, $raw, $pname);
	while( <MYPIPE> ) {
		($part,$pname) = split /\t/;
		if( $pname =~ m/^Apple_HFS/ ) {
			$part =~ s/^\s*(.*\S)\s*$/$1/;
			$raw = $part;
			$raw =~ s/^(\/dev\/)(.+)/$1r$2/;
			last;
		}
	}
	close( MYPIPE);
	system "cd \"$dst\" ; $newfsExec -v \"$name\" $raw";
	if( $? ) { system "$hdiUtilExec eject $dev"; die "couldn't format disk image\n"; }
	system "$hdiUtilExec eject $dev";
	if( $? ) { die "couldn't eject disk image\n"; }

	# copy files
	if( not open( MYPIPE, "cd \"$dst\"; $hdiDrvExec \"_${name}.dmg\" |") ) {
		die "couldn't open pipe\n";
	}
	($dev) = split( /\t/, <MYPIPE>);
	$dev =~ s/^(.*\S)\s*$/$1/;
	my $vname;
	while( <MYPIPE> ) {
		($part,$pname,$vname) = split /\t/;
		if( $pname =~ m/^Apple_HFS/ ) {
			$vname =~ s/^(.*\S)\s*$/$1/;
			last;
		}
	}
	close( MYPIPE);
	system "$dittoExec \"${src}\" \"${vname}\"";
	if( $? ) { system "$hdiUtilExec eject $dev"; die "couldn't copy files\n"; }
	system "$hdiUtilExec eject $dev";
	if( $? ) { die "couldn't eject disk image\n"; }

	# convert disk image
	system "cd \"$dst\"; $hdiUtilExec convert \"_${name}.dmg\" -format UDCO -o \"${name}\"";
	if( $? ) { die "couldn't convert disk image\n"; }
}