summaryrefslogtreecommitdiffstats
path: root/contrib/tcsh/MAKESHAR
blob: f2238e8d83b2ad2ea383f103073f7f0e6aa55986 (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
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
#!/bin/sh
#
# MAKESHAR.sh: Make a shar file for the sources
#
# $tcsh: MAKESHAR,v 3.2 2006/03/02 18:46:44 christos Exp $

AWK=/usr/bin/nawk	# Must be nawk or gawk cause of 2D arrays
WC=/usr/ucb/wc		
GREP=/usr/bin/egrep
SORT=/usr/bin/sort
SH=/bin/sh

dirs=
name=kit
files=
verbose=0
size=45000

for i 
do
    case $i in
    -n)	
	name=;;
    -v)
	verbose=1;;
    -d)
	SH=/bin/cat;;
    -s)
	size=$1;;
    *)
	if [ -z "$name" ]
	then
	    name=$i
	elif [ -d $i ]
	then
	    dirs="$dirs $i"
	elif [ -f $i ]
	then
	    files="$files $i"
	else
	    echo "$0: File `$i' not found." 1>&2
	    exit 1
	fi;;
    esac
done
	
if [ \( -z "$files" \) -a \( -z "$dirs" \) ]
then
    echo "Usage: $0 [-n name] [-s size] [-vd] <files>."	1>&2
    exit 1
fi

$WC $files | $GREP -v total | $SORT +2 | $AWK '
    BEGIN {
	i = 0;
	seq = 1;
	size = 0;
	name = 1;
	used = 2;
	verbose='"$verbose"';
	tty = "/dev/tty";
	maxsize = '"$size"';
	dirs = "'"$dirs"'";
    };
    {
	a[i, size] = $3;
	a[i, name] = $4;
	a[i, used] = 0;
	i++;
    };
    END {
	for (maxi = i--; i >= 0; i--) {
	    idx = 0;
	    if (a[i, used] == 0) {
		if (verbose && a[i, size] > maxsize) 
		    printf("Warning: File %s is %d > %d\n",
			   a[i, name], a[i, size], maxsize) > tty;
		s = a[i, size];
		a[i, used] = 1;
		kit[seq, idx++] = i; 
		j = 0;
		while (j < maxi) {
		    # Find the greatest file we can add
		    j = maxi;
		    for (k = 0; k < maxi; k++)
			if (a[k, used] == 0 && a[k, size] + s < maxsize)
			    j = k;
		    if (j < maxi) {
			s += a[j, size];
			a[j, used] = 1;
			kit[seq, idx++] = j; 
		    }
		}
		sizes[seq] = s;
		kit[seq++, idx] = -1; 
	    }
	}
	for (i = 1; i < seq; i++) {
	    printf("shar -n%d -e%d %s ", i, seq - 1, dirs);
	    if (verbose) {
		printf("%3d of %3d: ", i, seq - 1) > tty;
		len = 12;
	    }
	    for (j = 0; kit[i, j] != -1; j++) {
		s = a[kit[i, j], name];
		if (verbose) {
		    clen = length(s) + 1;
		    len += clen;
		    if (len > 70) {
			printf("\n            ") > tty;
			len = 12 + clen;
		    }
		    printf("%s ", s) > tty;
		}
		printf("%s ", s);
	    }
	    printf("> '"$name"'-%d.shar;", i);
	    if (verbose)
		printf("= %5d\n", sizes[i]) > tty;
	}
    }' | $SH
OpenPOWER on IntegriCloud