#!/bin/sh

# ldb-ld -o ofile [ -r ] [-destdir d] [ -pkl | -nopkl ] startup [ file | -flag ]...

# -destdir works only with -r, and it's used to give the correct, ultimate, 
# full path names to the symbol-table files that get linked in.  
# A better solution would be to somehow automatically use paths relative to
# wherever the including file is found, but I don't see a clean way of doing that.

# set -x
load=1 output=a.out pkl=0 object= ldargs= destdir=

while [ $# -gt 0 ]; do
	case $1 in 
	-r)		load=0; ldargs="$ldargs $1" ;;
	-o)		output="$2"; shift ;;
	-e)		ldargs="$ldargs $1 $2"; shift ;;
	-pkl)		pkl=1 ;;
	-nopkl)		pkl=0 ;;
	-N)		ldargs="$ldargs $DASHN" ;;
	-destdir)	destdir="$2/" ; shift ;;
	-*)		ldargs="$ldargs $1" ;;
        *crt?\.o)	ldargs="$ldargs $1" ;;
	*)		object="$object $1"; ldargs="$ldargs $1" ;; 
	esac
	shift
done
$LD -o $output $ldargs

if [ $load -ne 0 ]; then
	stabout="$output.lt"
else
	stabout=$output.st
fi

(if [ $load -ne 0 ]; then
	nm $nmopts $output | fgrep _stanchor__V
fi
echo DESTDIR "$destdir"
awk 'BEGIN { printf "READABLE" }' < /dev/null  # echo -n not portable!
for i in $object; do
	if [ -r $i.st ]; then 
          	awk 'BEGIN { printf " 1" }' < /dev/null # echo -n not portable!
        else 
          	awk 'BEGIN { printf " 0" }' < /dev/null # echo -n not portable!
        fi
done
echo ' '
echo OBJECT $object ) | $AWK '
BEGIN { 
  load='"$load"'
  destdir = ""
  if (load) {
    printf "(%s) Architecture.Set\n<< %% start of loadertab\n","'"$architecture"'"
    print "  /anchormap <<"
  } else {
    print "pop % remove stabpathname, which is not used"
  }
}
$1 == "READABLE" {
  n = split($0, readable)
  next
}
$1 == "DESTDIR" {
  if (NF>1) destdir = $2
  next
}
$1 == "OBJECT" { 
  if (load) { 
    print  "  >>"
    printf "  /symtab "
  } 
  printf "[ (%s) %% destination directory\n", destdir
  n = split($0, obj)
  for (i=2; i<=n; i++) {
    stab = obj[i]
    if (readable[i] == "1")    
      if (substr(stab, 1, 1) == "/") 
        printf "        (%s.st)     readstab exch\n", stab
      else
        printf "    dup (%s.st) cat readstab exch\n", stab
    else                       
      printf " %%      %s.st not readable\n", stab
  }
  print "    pop % eliminate destination directory"
  if (load) print "    RegStab"
  printf "  ] stabmerge\n"
  next
}
{ if (col+3+length($3) > 77) {
    files = files "\n\t"
    col = 8
  }
  files = files "(" $3 ") " 
  col += 3 + length($3)
  printf "\t/%s 16#%s\n", $3, $1
}
END {
  if (load) {
    print ">>"
    print "dup checkloadertab"
  }
}' >$stabout

if [ $load -ne 0 ]; then
	nm $nmopts $output | sort | $AWK '
BEGIN { print "dup /proctable [ [" }
$2=="t" || $2=="T" {
  if (full[$1] && $3 ~ /\.o$/) {
    full[$1]++
  } else {
    full[$1]=1
    if ($2=="T") extern="true"
    else extern="false"
    printf "16#%s (%s) %s\n", $1, $3, extern
    if (++count % 300 == 0) print "] ["
  }
}
END { print "] ] arraymerge put" }
' >>$stabout
fi

/bin/rm -f $stabout.pkl # don't catch old pickle
if [ $pkl -ne 0 ]; then
	echo "Built stab file:"
	cat $stabout
	echo $INTERP "'($stabout) readstab ($stabout.pkl) Pickle'"
	$INTERP "($stabout) readstab ($stabout.pkl) Pickle"
	if [ $? -ne 0 ]; then
		rm $stabout.pkl
	else
		rm $stabout
	fi
fi
exit 0
