
# this script produces a file 'fullindex.ind' that contains the index of all
# five (main) GAP manual books (i.e. ref, tut, ext, prg, new).
echo "Collecting indices"
awk '$0 !~ /@/ {$0=tolower($0)} {print}' ref/manual.idx >fullindex.idl.pre
awk '{i=0;a=$0;\
      rst=index(a,"{");\
      while (rst>0) {i=i+rst;a=substr(a,rst+1);rst=index(a,"{");}\
      print substr($0,1,i) "R\\idxkern" substr($0,i+1);}'\
  fullindex.idl.pre >fullindex.idl
awk '$0 !~ /@/ {$0=tolower($0)} {print}' tut/manual.idx >fullindex.idl.pre
awk '{i=0;a=$0;\
      rst=index(a,"{");\
      while (rst>0) {i=i+rst;a=substr(a,rst+1);rst=index(a,"{");}\
      print substr($0,1,i) "T\\idxkern" substr($0,i+1);}'\
  fullindex.idl.pre >>fullindex.idl
awk '$0 !~ /@/ {$0=tolower($0)} {print}' ext/manual.idx >fullindex.idl.pre
awk '{i=0;a=$0;\
      rst=index(a,"{");\
      while (rst>0) {i=i+rst;a=substr(a,rst+1);rst=index(a,"{");}\
      print substr($0,1,i) "E\\idxkern" substr($0,i+1);}'\
  fullindex.idl.pre >>fullindex.idl
awk '$0 !~ /@/ {$0=tolower($0)} {print}' prg/manual.idx >fullindex.idl.pre
awk '{i=0;a=$0;\
      rst=index(a,"{");\
      while (rst>0) {i=i+rst;a=substr(a,rst+1);rst=index(a,"{");}\
      print substr($0,1,i) "P\\idxkern" substr($0,i+1);}'\
  fullindex.idl.pre >>fullindex.idl
awk '$0 !~ /@/ {$0=tolower($0)} {print}' new/manual.idx >fullindex.idl.pre
awk '{i=0;a=$0;\
      rst=index(a,"{");\
      while (rst>0) {i=i+rst;a=substr(a,rst+1);rst=index(a,"{");}\
      print substr($0,1,i) "N\\idxkern" substr($0,i+1);}'\
  fullindex.idl.pre >>fullindex.idl
cp fullindex.idl fullindex.idx
makeindex -l fullindex.idl

# put hyphenation help into long commands
echo "Hyphenating long index entries"
echo '{a=index($0,"`");\
    if (a==0) {print $0;}\
    else {\
      b=index($0,"'"'"'");\
      if (b-a>40) {\
	anf=substr($0,1,a);\
	mid=substr($0,a+1,b-a-1);\
	end=substr($0,b);\
	b=length(mid);\
	i=1;\
	cap=1;\
	mid2=substr(mid,1,i-1);\
	while (i<=length(mid)) {\
	  c=substr(mid,i,1);\
	  if (c!=tolower(c)){\
	    if (cap==0) mid2 = mid2 "\\-";\
	    cap=1;\
	  }\
	  else cap=0;\
	  mid2=mid2 c;\
	  i=i+1;\
	}\
	print anf mid2 end;\
      }\
      else print $0;\
    }
  }' >mh.awk
cp fullindex.ind fullindex.idl
awk -f mh.awk fullindex.idl >fullindex.ind
rm mh.awk
rm fullindex.idl.pre
rm fullindex.idl
