#!/bin/sh -e
# $Id$
# Originally written by Karl Berry. Public domain.
# 
# Replace symlinks to files with the actual files.
# Symlinks to anything else are not touched.

if test x"$1" = x--save-links; then
  savelinks=true
  shift
else
  savelinks=false
fi

for f in "$@"; do
  if test ! -h "$f"; then
    echo "$0: skipping non-symlink: $f" >&2
    continue
  elif test ! -f "$f"; then
    echo "$0: skipping symlink to non-file: $f" >&2
    file "$f" >&2
    continue
  fi
  
  cp -p --dereference "$f" "$f".file  # expand link
  mv -v "$f" "$f".link                # move link out of the way
  mv -v "$f".file "$f"                # replace with regular file
  $savelinks || rm "$f".link          # remove link unless keeping
done
