#!/bin/sh

# script to extract and create aliases from source files

# $Id: install-aliases,v 1.7 2000/12/06 22:17:42 jsh Exp $

verbose=no
dry_run=no

# copy a .la file, editing the `dlname=' line to the new stem
copy_la_file () {
  la_src="$1"; la_dest="$2"; shift 2

  old_stem=`echo "$la_src" | sed -e 's|^.*/\([^/]*\)\.[^/]*$|\1|'`
  new_stem=`echo "$la_dest" | sed -e 's|^.*/\([^/]*\)\.[^/]*$|\1|'`

  sed -e '/^dlname=/ s/'"${old_stem}"'/'"${new_stem}"'/' \
      < "$la_src" > "$la_dest"
}

# return the outermost directory of the filename
prefix () {
  case "$1" in
    */*)
      echo "$1" | sed -e 's|^\([^/]*\)/.*$|\1|'
      ;;
    *)
      echo ""
      ;;
  esac
}

# return the filename without its outermost directory
postfix () {
  echo "$1" | sed -e 's|^[^/]*/\(.*\)$|\1|'
}

# strip common leading directories from $2
strip_common_prefix () {
  str1="$1"; str2="$2"; shift 2
  pfx=`prefix "$str1"`; str1=`postfix "$str1"`
  while [ x"$pfx" != x ]; do
    case "$str2" in
      $pfx*)
	str2=`postfix "$str2"`
	;;
      *)
	break
	;;
    esac
    pfx=`prefix "$str1"`; str1=`postfix "$str1"`
  done
  echo "$str2"
}

# turns `FOO/BAR/BAZ' into `FOO/BAR/libBAZ'
lib_prefixed () {
  echo "$1" | sed -e 's|\([^/]*\)$|lib\1|'
}

dosome () {
  destroot="$1"; src="$2"; dest="$3"; shift 3

  for g in `echo "${destroot}/${src}.*"`; do
    if [ -r "$g" ]; then
      suf=`echo "$g" | sed -e 's|^'"${destroot}/${src}"'\(.*\)$|\1|'`
      if [ $verbose = yes ]; then
	echo "${src}${suf} -> ${dest}${suf}"
      fi
      if [ $dry_run = no ]; then
	rm -f "${destroot}/${dest}${suf}"
	case "$suf" in
	  .la)
	    copy_la_file "${destroot}/${src}${suf}" \
		"${destroot}/${dest}${suf}"
	    ;;
	  *)
	    # make sure links are relative to their own directory
	    local_src=`strip_common_prefix "$dest" "$src"`
	    ln -s "${local_src}${suf}" "${destroot}/${dest}${suf}"
	    ;;
	esac
      fi
    fi
  done
}

doit () {
  destroot="$1"; shift
  for f in $*; do
    src=`echo "$f" | sed -e 's/^\(.*\)#\(.*\)$/\1/'`
    dest=`echo "$f" | sed -e 's/^\(.*\)#\(.*\)$/\2/'`
    dosome "$destroot" "$src" "$dest"

    # libtool sometimes needs to prefix module libraries with a `lib'
    # prefix; this will try to pick up such libraries
    dosome "$destroot" `lib_prefixed "$src"` `lib_prefixed "$dest"`
  done
}

while [ x"$1" != x ]; do
  case $1 in
    -l)
      files=`find "$2" -name '*.jl' -print`
      aliases=`grep '(define-structure-alias' $files |
        sed -e 's/^.*(define-structure-alias \([^ ]*\) \([^ ]*\)).*$/\2#\1/' |
	tr . /`
      doit "$3" $aliases
      shift 3
      ;;

    -c)
      files=`find "$2" -name '*.c' -print`
      aliases=`grep '::alias:' $files |
        sed -e 's/^.*::alias:\([^ ]*\) \(.*\)::.*$/\2#\1/' |
	tr . /`
      doit "$3" $aliases
      shift 3
      ;;

    -v) verbose=yes; shift ;;
    -n) dry_run=yes; verbose=yes; shift ;;

    *)
      echo "usage [-v] [-n] [-l LISP-SRC-DIR LISP-INST-DIR] [-c C-SRC-DIR LA-INST-DIR]"
      exit 1
      ;;
  esac
done
