PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/tcsh/MAKESHAR

https://bitbucket.org/freebsd/freebsd-head/
Shell | 121 lines | 112 code | 3 blank | 6 comment | 20 complexity | e57650c8226845503a1b46a1b28db1fb MD5 | raw file
  1#!/bin/sh
  2#
  3# MAKESHAR.sh: Make a shar file for the sources
  4#
  5# $tcsh: MAKESHAR,v 3.2 2006/03/02 18:46:44 christos Exp $
  6
  7AWK=/usr/bin/nawk	# Must be nawk or gawk cause of 2D arrays
  8WC=/usr/ucb/wc		
  9GREP=/usr/bin/egrep
 10SORT=/usr/bin/sort
 11SH=/bin/sh
 12
 13dirs=
 14name=kit
 15files=
 16verbose=0
 17size=45000
 18
 19for i 
 20do
 21    case $i in
 22    -n)	
 23	name=;;
 24    -v)
 25	verbose=1;;
 26    -d)
 27	SH=/bin/cat;;
 28    -s)
 29	size=$1;;
 30    *)
 31	if [ -z "$name" ]
 32	then
 33	    name=$i
 34	elif [ -d $i ]
 35	then
 36	    dirs="$dirs $i"
 37	elif [ -f $i ]
 38	then
 39	    files="$files $i"
 40	else
 41	    echo "$0: File `$i' not found." 1>&2
 42	    exit 1
 43	fi;;
 44    esac
 45done
 46	
 47if [ \( -z "$files" \) -a \( -z "$dirs" \) ]
 48then
 49    echo "Usage: $0 [-n name] [-s size] [-vd] <files>."	1>&2
 50    exit 1
 51fi
 52
 53$WC $files | $GREP -v total | $SORT +2 | $AWK '
 54    BEGIN {
 55	i = 0;
 56	seq = 1;
 57	size = 0;
 58	name = 1;
 59	used = 2;
 60	verbose='"$verbose"';
 61	tty = "/dev/tty";
 62	maxsize = '"$size"';
 63	dirs = "'"$dirs"'";
 64    };
 65    {
 66	a[i, size] = $3;
 67	a[i, name] = $4;
 68	a[i, used] = 0;
 69	i++;
 70    };
 71    END {
 72	for (maxi = i--; i >= 0; i--) {
 73	    idx = 0;
 74	    if (a[i, used] == 0) {
 75		if (verbose && a[i, size] > maxsize) 
 76		    printf("Warning: File %s is %d > %d\n",
 77			   a[i, name], a[i, size], maxsize) > tty;
 78		s = a[i, size];
 79		a[i, used] = 1;
 80		kit[seq, idx++] = i; 
 81		j = 0;
 82		while (j < maxi) {
 83		    # Find the greatest file we can add
 84		    j = maxi;
 85		    for (k = 0; k < maxi; k++)
 86			if (a[k, used] == 0 && a[k, size] + s < maxsize)
 87			    j = k;
 88		    if (j < maxi) {
 89			s += a[j, size];
 90			a[j, used] = 1;
 91			kit[seq, idx++] = j; 
 92		    }
 93		}
 94		sizes[seq] = s;
 95		kit[seq++, idx] = -1; 
 96	    }
 97	}
 98	for (i = 1; i < seq; i++) {
 99	    printf("shar -n%d -e%d %s ", i, seq - 1, dirs);
100	    if (verbose) {
101		printf("%3d of %3d: ", i, seq - 1) > tty;
102		len = 12;
103	    }
104	    for (j = 0; kit[i, j] != -1; j++) {
105		s = a[kit[i, j], name];
106		if (verbose) {
107		    clen = length(s) + 1;
108		    len += clen;
109		    if (len > 70) {
110			printf("\n            ") > tty;
111			len = 12 + clen;
112		    }
113		    printf("%s ", s) > tty;
114		}
115		printf("%s ", s);
116	    }
117	    printf("> '"$name"'-%d.shar;", i);
118	    if (verbose)
119		printf("= %5d\n", sizes[i]) > tty;
120	}
121    }' | $SH