#!/usr/bin/perl -w

#Illegal seek (on pipe)
my $espipe=29;

open(MOUNT,"mount |") or
	die "$0: open(MOUNT,\"mount |\") failed: $!\n";

#data fields to gather from output of mount(8)
my $match_mount='^(.+) on (.+) type (.+) \((.*?)\)\n*$';
#                  device
#                          mount point
#                                    type
#                                           options
my @mount=();

while (<MOUNT>){
	#$_='bar';
	if (/$match_mount/) {
		my $device=$1;
		my $mount_point=$2;
		my $type=$3;
		my $options=$4;
		#skip filesystems we're not presently interested in
		(
			#must be one of these types ...
			$type =~
				/
					^	(?:
							ext[23] |
							reiserfs
						)
					$
				/ox
				||
			#or one of these type and ...
			$type =~
				/
					^	(?:
							ntfs |
							vfat |
							fat
						)
					$
				/ox
				&&
			#mounted readonly
			$options =~
				/
					(?:^|,)
						ro
					(?:,|$)
				/ox
		)	&&
			#and not one of these mount points
			$mount_point !~
			m!
				^
					(?:
						/+mnt |
						/+var/+local/+pub/+iso |
						/+home/+r/+root/+tmp/+mnt
					)
				(?:$|/)
			!ox
		or next;
		#push device mount_point type options on our array,
		#split out the options
		push @mount,[$device,$mount_point,$type,[split(/,/,$options)]]
	}
	else {
		print ("else\n");
		print STDERR ("$0: ",(m'^(.*?)\n*$')," failed to match $match_mount\n");
	}
}

close(MOUNT);
if	(
		$!	&&
		#quietly ignore illegal seek on pipe
		$!	!=	$espipe
	)	{
	die "$0: close(MOUNT,\"mount |\") failed: $!\n";
}
elsif ($?)	{
	die "$0: close(MOUNT,\"mount |\") apparently failed, wait() apparently returned: $?\n";
}

@mount=sort {
	#handle highest priorities (if present) first:
	#/boot, / (root), /usr, /var, /home
	for $pri (
		'/boot',
		'/',
		'/usr',
		'/var',
		'/home'
	) {
		if(@$a[1] eq $pri && @$b[1] ne $pri) { return -1; }
		if(@$b[1] eq $pri && @$a[1] ne $pri) { return 1; }
	}
	#everything else is higher and compares normally
	#print ("@$a[1] cmp @$b[1] ",@$a[1] cmp @$b[1],"\n");
	@$a[1] cmp @$b[1];
}	@mount;

foreach $line (@mount) {
	print join("\0",(@{$line}[0..2]),join(',',@{${$line}[3]})),"\n";
}
