about summary refs log tree commit diff
path: root/src/nix-instantiate
blob: 4823d221252ddf8ce9897f4230e70446ab66beda (plain) (blame)
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
#! /usr/bin/perl -w

use strict;
use FileHandle;
use File::Spec;

my $outdir = $ARGV[0];

my %donetmpls = ();

sub convert {
    my $descr = shift;

    if (defined $donetmpls{$descr}) {
        return $donetmpls{$descr};
    }

    my ($x, $dir, $fn) = File::Spec->splitpath($descr);

    print "$descr\n";

    my $IN = new FileHandle;
    my $OUT = new FileHandle;
    my $outfile = "$outdir/$fn";
    open $IN, "< $descr" or die "cannot open $descr";
    open $OUT, "> $outfile" or die "cannot create $outfile";

    while (<$IN>) {
        chomp;

        if (/^(\w+)\s*=\s*([+\w\d\.\/-]+)\s*(\#.*)?$/) {
            my $name = $1;
            my $file = $2;
            $file = File::Spec->rel2abs($file, $dir);
            my $out = `md5sum $file`;
            die unless ($? == 0);
            $out =~ /^([0-9a-f]+)\s/;
            my $hash = $1;
            print $OUT "$name = $hash\n";
        } elsif (/^(\w+)\s*<-\s*([+\w\d\.\/-]+)\s*(\#.*)?$/) {
            my $name = $1;
            my $file = $2;
            $file = File::Spec->rel2abs($file, $dir);
            $file = convert($file);
            my $out = `md5sum $file`;
            die unless ($? == 0);
            $out =~ /^([0-9a-f]+)\s/;
            my $hash = $1;
            print $OUT "$name <- $hash\n";
        } else {
            print $OUT "$_\n";
        }
    }

    $donetmpls{$descr} = $outfile;
    return $outfile;
}

for (my $i = 1; $i < scalar @ARGV; $i++) {
    convert(File::Spec->rel2abs($ARGV[$i]));
}