#!/bin/sh
# $Id: index-to-ls,v 1.2 1992/08/11 16:31:33 cross Exp $
# INDEX format converter: INDEX-1.0 -> SunOS ls-lR

TZ=GMT
LANG=""
LANGUAGE=""
LC_CTYPE=""
export TZ LANG LANGUAGE LC_CTYPE

if [ $# -ne 1 -o ! -r "$1" ]; then
    echo "Usage: $0 index > ls-lR" 1>&2
    exit 1
fi
date '+%d %m %y' | cat - "$1" | awk '
NR == 1 {
    thisday=$1; thismonth=$2; thisyear=$3; version=0;
    if (thisyear > 1900)
	thisyear -= 1900;
    today=thisday + 30 * thismonth + 365 * thisyear;
    months[1]  = "Jan"; months[2]  = "Feb"; months[3]  = "Mar";
    months[4]  = "Apr"; months[5]  = "May"; months[6]  = "Jun";
    months[7]  = "Jul"; months[8]  = "Aug"; months[9]  = "Sep";
    months[10] = "Oct"; months[11] = "Nov"; months[12] = "Dec";
    next;
}
/^#VERSION/ {
    if ($2 != "1.0") {
	print "wrong version header \"" $2 "\". cannot decode format.";
	err=1;
	exit;
    }
    version=1;
    next;
}
/^$/ || /^#/ {
    next;
}
NF < 5 {
    print "not enough fields in line " NR - 1 ;
    err=1;
    exit;
}
{			# COMPOSE (print see below)
    if (!version) {
	print "no version (#VERSION) header provided. cannot decode format.";
	err=1;
	exit;
    }
    type=substr($1,1,1);
    if (type == "F")
	type="-";
    else if (type == "D")
	type="d";
    else if (type == "L")
	type="l";
    else {
	print "illegal type specification in line " NR - 1 ;
	err=1;
	exit;
    }

    if (substr($1,2,1) == "R")
	perm="r";
    else
	perm="-";
    if (substr($1,3,1) == "W")
	perm= perm "w";
    else
	perm= perm "-";
    if (substr($1,4,1) == "X")
	perm= perm "x";
    else
	perm= perm "-";
    perm= perm perm perm;

    day=substr($3,1,2); monthstr=substr($3,4,3); year=substr($3,8,11) - 1900;
    time=$4;

    for (month = 12; month > 0; month--) {
	if (months[month] == monthstr) break;
    }

    if (month <= 0) {
	print "illegal month specification in line " NR - 1 ;
	err=1;
	exit;
    }

    for(i = length($5); i >= 0 && substr($5,i,1) != "/"; i--)
	;
    if (i < 0) {
	print "filename does not begin with / in line " NR - 1 ;
	err=1;
	exit;
    }

    # must be seeked from left because of embedded spaces in filenames!
    # no offset range check; tested earlier (NF >= 5)
    foff=0;
    for(i = 0; i < 4; i++) {
	while(substr($0,foff,1) != " " && substr($0,foff,1) != "	")
	    foff++;
	while(substr($0,foff,1) == " " || substr($0,foff,1) == "	")
	    foff++;
    }
    if (substr($0,foff,1) == "/")
	foff++;
    if (type == "l") {
	offl=foff;
	len=length($0);
	while(offl < len - 4 && substr($0,offl,4) != " -> ")
	    offl++;
	if (offl >= len - 4) {
	    print "illegal link name syntax in line " NR - 1;
	    err=1;
	    exit;
	}
	path=substr($0,foff,offl-foff);
	link=substr($0,offl+4,1024);
    } else {
	path=substr($0,foff,1024);
    }
    olddepth = depth;
    depth=0;
    len=length(path);
    fileoff=0;
    for(off=0; off < len; off++) {
	if (substr(path,off,1) == "/") {
	    depth++;
	    fileoff=off+1;
	}
    }
    file=substr(path,fileoff,1024);

    line=sprintf("%s%s  1 dummy    %8d ", type, perm, $2);
    if (day + 30 * month + 365 * year + 6 * 30 > today) {
	line=line sprintf("%s %2d %s", monthstr, day, time);
    } else {
	line=line sprintf("%s %2d %5d", monthstr, day, year + 1900);
    }
    line=line " " file;
    if (type == "l") {
	line=line " -> " link;
    }
    line=line "\n";

    # DIVERT or PRINT composed line

    if (olddepth > depth) {
	for(d = olddepth - 1; d >= depth; d--) {
	    subdirs[d] = subdirs[d] "\n" name[d+1] ":\ntotal " count[d+1] "\n" divert[d+1] subdirs[d+1];
	    divert[d+1] = "";
	    subdirs[d+1] = "";
	    name[d+1] = "";
	    count[d+1] = 0;
	}
    }
    if (type == "d") {
	divert[depth] = divert[depth] line;
	count[depth]++;
	depth++;	# remember that we began a new directory
	name[depth] = file; 
	count[depth] = 0;
	subdirs[depth] = "";
	next;
    } 
    if (olddepth < depth ) {
	print "directory name for new directory missing in line " NR - 1;
	err=1;
	exit;
    }
	
    divert[depth] = divert[depth] line;
    count[depth]++;
}
END {
    if (err) exit;
    for(; depth >= 0; depth--) {
	if (name[depth+1] != "") {
	    subdirs[depth] = subdirs[depth] "\n" name[depth+1] ":\ntotal " count[depth+1] "\n" divert[depth+1] subdirs[depth+1];
	}
    }
    print "total " count[0] "\n" divert[0] subdirs[0];
}
'
