# shorten < cpio-file > transformed file
#
# author:  Dr. Ulrich Lauther (Siemens ZFE BT SE 14)
#
# filters a cpio-file and cuts all filenames to maxlen characters
# include directives are adjusted accordingly
# a check is made to make sure, all new filenames are unique
#
# the cpio-file mast have been generated using the -c option !!!
#
# only includes of the form <LEDA/*.h> are processed
#
# a filter "rpl" for replacing any strings (including '\0', not handled by tr)
# is needed
#

rpl '\0' '\n' |
awk '
BEGIN { maxlen = 14; maxoct = "000016"; maxoct1 = "000017" # maxoct1 one more !
        # for testing: maxlen =  4; maxoct = "000004"; maxoct1 = "000005"
	null = "@@null@@"
      }

/^070707/ { # header
          filename = substr($0,77)
	  if (filename == "TRAILER!!!") {
	    printf("%s%s",$0,null)
	    exit
	  }
          s1 = substr($0,1,59)
          namelength = substr($0,60,6)
	  if (namelength+0 <= maxoct+0) {
	    printf("%s%s",$0,null)
	  }
	  else {
            filelength = substr($0,66,11)
	    split(filename,x,".")
	    filename = substr(x[1],1,maxlen-2) "." x[2]
	    printf ("%s%s%s%s%s",s1,maxoct1,filelength,filename,null)
	  }
	  count[filename]++
	  next
      }
/# *include  *<LEDA\// { #include <LEDA/basic.h>
	i1 = index($0,"/")
	s1 = substr($0,1,i1) 
	i2 = index($0,".")
	if (i2 - i1 > maxlen-2) {
	  name = substr($0,i1+1,maxlen-2)
	  rest = substr($0,i2)
	  printf ("%s%s%s",s1,name,rest)
	  # keep length of file constant:
	  for (i = 1; i < i2 - i1 - maxlen + 2; i++) printf(" ")
	  printf("\n")
	}
	else print
	next
      }
      { print
      }
END   { for (name in count) {
          if(count[name] > 1) {
	    printf("%s %d times !!\n",name, count[name]) >> "/dev/tty"
	  }
	}
      }
' | rpl @@null@@ '\0'

