File: oggsymlinks

package info (click to toggle)
draai 20201215-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 656 kB
  • sloc: sh: 854; perl: 170; makefile: 28
file content (186 lines) | stat: -rwxr-xr-x 4,415 bytes parent folder | download | duplicates (8)
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
#! /usr/bin/perl

# This file is maintained at http://git.mdcc.cx/draai
#	oggsymlinks - maintain a symlink forest
#	Copyright (C) 2008  Wessel Dankers <wsl@fruit.je>
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use utf8;
use Carp;

my $dot = ord '.';
my $slash = ord '/';

my $verbose = $ENV{'DR_VERBOSE'};
my $debug = $ENV{'DR_DEBUG'};
my $dryrun = $ENV{'DR_DRYRUN'};

my $target = shift @ARGV;

my %target;

opendir TARGET, $target
	or croak "Can't open $target: $!";

while(my $artist = readdir TARGET) {
	next if ord($artist) == $dot;
	my $dir = "$target/$artist";
	my %albums;
	unless(opendir ARTIST, $dir) {
		my $err = $!;
		unless(-d $dir) {
			carp "$dir is not a directory, skipping";
			undef $target{$artist};
			next;
		}
		croak "Can't open $dir: $err";
	}
	while(my $album = readdir ARTIST) {
		next if ord($album) == $dot;
		my $link = "$dir/$album";
		my $dest = readlink $link;
		$albums{$album} = $dest;
		unless(defined $dest) {
			my $err = $!;
			if(!-l $link) {
				carp "$link is not a symlink, skipping";
			} else {
				carp "Can't read symlink $link: $err";
			}
			next;
		}
	}

	$target{$artist} = \%albums;
}

closedir TARGET;

sub path {
	my ($dir, $path) = @_;
	return $path if ord($path) == $slash;
	return "$dir/$path";
}

my %source;

foreach my $source (@ARGV) {
	# strip multiple/trailing slashes
	$source =~ s|//+|/|g;
	$source =~ s|(.)/$|$1|;

	my $psource = path($target, $source);
	opendir SOURCE, $psource
		or croak "Can't open $psource: $!";

	while(my $artist = readdir SOURCE) {
		next if ord($artist) == $dot;
		my $dir = "$source/$artist";
		my $pdir = "$psource/$artist";
		my $dude;
		$source{$artist} = {} unless exists $source{$artist};
		$dude = $source{$artist};
		my %albums;
		unless(opendir ARTIST, $pdir) {
			my $err = $!;
			unless(-d $pdir) {
				carp "$pdir is not a directory, skipping";
				next;
			}
			croak "Can't open $pdir: $!";
		}
		while(my $album = readdir ARTIST) {
			print "Inspecting $source, $artist, $album\n" if $debug;
			next if ord($album) == $dot;
			$dude->{$album} = $source;
		}
	}

	closedir SOURCE;
}

# remove redundant/bad symlinks and directories
while(my ($artist, $tartist) = each(%target)) {
	next unless defined $tartist;

	my $sartist = $source{$artist};

	while(my ($album, $link) = each(%$tartist)) {
		next unless defined $link;
		my $to = "$target/$artist/$album";
		if(defined $sartist) {
			my $src = $sartist->{$album};
			next if
				defined $src
				and $link eq path('..', "$src/$artist/$album");
		}
		if($verbose) {
			print "Would run: " if $dryrun;
			print "unlink $to\n";
		}
		unless($dryrun) {
			unlink $to or croak "Can't create remove $to: $!";
		}
		delete $tartist->{$album};
	}

	next if defined $sartist;
	next if keys %$tartist;
	my $dir = path($target, $artist);
	if($verbose) {
		print "Would run: " if $dryrun;
		print "rmdir $dir\n";
	}
	unless($dryrun) {
		rmdir $dir or warn "Can't rmdir $dir: $!";
	}
}

# create missing directories and symlinks
while(my ($artist, $sartist) = each(%source)) {
	my $tartist;

	if(exists $target{$artist}) {
		$tartist = $target{$artist};
		next unless defined $tartist;
	} else {
		my $dir = path($target, $artist);
		if($verbose) {
			print "Would run: " if $dryrun;
			print "mkdir $dir\n";
		}
		unless($dryrun) {
			mkdir $dir or croak "Can't mkdir $dir: $!";
		}
		$tartist = {};
	}

	while(my ($album, $src) = each(%$sartist)) {
		print "Checking for missing symlinks for $src, $artist, $album\n" if $debug;
		next if exists $tartist->{$album};
		my $from = path('..', "$src/$artist/$album");
		my $to = "$target/$artist/$album";
		if($verbose) {
			print "Would run: " if $dryrun;
			print "symlink $from, $to\n";
		}
		unless($dryrun) {
			symlink $from, $to
				or croak "Can't create symlink to $to at $from: $!";
		}
	}
}