File: handle_links

package info (click to toggle)
libzip 1.11.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,632 kB
  • sloc: ansic: 17,309; sh: 85; perl: 55; makefile: 5
file content (74 lines) | stat: -rwxr-xr-x 1,416 bytes parent folder | download | duplicates (17)
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
#!/usr/bin/env perl

use strict;

my $operation = shift @ARGV;

if ($operation !~ m/^(install|uninstall)$/) {
    print STDERR "$0: unknown operation $operation\n";
    exit(1);
}

my %options = ();

for my $arg (@ARGV) {
    if ($arg =~ m/([^=]*)=(.*)/) {
        $options{$1} = $2;
    }
    else {
        print STDERR "$0: can't parse option [$arg]\n";
        exit(1);
    }
}

for my $option (qw(command directory extension file)) {
    unless (defined($options{$option})) {
        print STDERR "$0: required variable $option not provided\n";
        exit(1);
    }
}

my $fh;
unless (open $fh, '<', $options{file}) {
    print STDERR "$0: can't open links file '$options{file}': $!";
    exit(1);
}

my @cmd = split /\s+/, $options{command};

while (my $line = <$fh>) {
    chomp $line;
    my @args = split /\s+/, $line;

    process(@args);
}

sub process {
    my ($source, @destinations) = @_;

    my @args = (@cmd);

    if ($operation eq 'install') {
        push @args, "$options{directory}/$source.$options{extension}";
    }

    for my $destination (@destinations) {
        push @args, "$options{directory}/$destination.$options{extension}";
        run_command(@args);
        pop @args;
    }
}

sub run_command {
    print (join ' ', @_);
    print "\n";

    my $ret = system(@_);

    if ($ret != 0) {
        print STDERR "$0: command failed: $?\n";
        exit(1);
    }

    return 1;
}