#! /bin/sh
# icecc -- A simple distributed compiler system
#
# Copyright (C) 2004 by the Icecream Authors
# GPL

target_files=

is_contained ()
{
  case " $target_files " in
    *" $1 "* ) return 0 ;;
    *"=$1 "* ) return 0;;
    * ) return 1 ;;
  esac
}

add_file ()
{
  local name="$1"
  local path="$1";
  if test -n "$2"; then
    name="$2"
  fi
  test -z "$name" && return
  path=$(readlink -f $path)
  toadd="$name=$path"
  if test "$name" = "$path"; then
    toadd=$path
  fi
  is_contained "$toadd" && return
  echo "adding file $toadd"
  target_files="$target_files $toadd"
  if test -x "$path"; then
    # Only call ldd when it makes sense
    if file "$path" | grep 'ELF' > /dev/null 2>&1 ; then
	   # ldd now outputs ld as /lib/ld-linux.so.xx on current nptl based glibc
		# this regexp parse the outputs like:
		# ldd /usr/bin/gcc
		#         linux-gate.so.1 =>  (0xffffe000)
		#         libc.so.6 => /lib/tls/libc.so.6 (0xb7e81000)
		#         /lib/ld-linux.so.2 (0xb7fe8000)
		# covering both situations ( with => and without )
      for lib in `ldd "$path" | sed -n 's,^[^/]*\(/[^ ]*\).*,\1,p'`; do
	# Check wether the same library also exists in the parent directory,
	# and prefer that on the assumption that it is a more generic one.
	local baselib=`echo "$lib" | sed 's,/[^/]*\(/[^/]*\)$,\1,'`
	test -f "$baselib" && lib=$baselib
	add_file "$lib"
      done
    fi
  fi
}

if test -z "`type -p gcc`"; then
  echo "create-env: No gcc found in path."
  exit 1
fi

added_gcc=/usr/bin/gcc
added_gxx=/usr/bin/g++

if test "$1" = "--respect-path"; then
  added_gcc=`which gcc` 
  added_gxx=`which g++` 
# Perhaps we are on Gentoo.  They provide a wrapper for GCC.
elif test -x /usr/bin/gcc-config; then
  gccpath=`/usr/bin/gcc-config -B`
  if test -d "$gccpath" && test -x "$gccpath/gcc"; then
    added_gcc=$gccpath/gcc
    added_gxx=$gccpath/g++
  fi
fi

add_file $added_gcc /usr/bin/gcc 
add_file $added_gxx /usr/bin/g++
add_file /usr/bin/as

add_file `$added_gcc -print-prog-name=cc1`
add_file `$added_gxx -print-prog-name=cc1plus`
specfile=`$added_gcc -print-file-name=specs`
if test -n "$specfile" && test -e "$specfile"; then
  add_file "$specfile"
fi

tempdir=`mktemp -d /tmp/iceccenvXXXXXX`
new_target_files=
for i in $target_files; do 
 case $i in
   *=/*)
    target=`echo $i | cut -d= -f1`
    path=`echo $i | cut -d= -f2`
    ;;
   *)
    path=$i
    target=$i
    ;;
  esac
  mkdir -p $tempdir/`dirname $target`
  cp $path $tempdir/$target
  target=`echo $target | cut -b2-`
  new_target_files="$new_target_files $target"
done

# now sort the files in order to make the md5sums independent
# of ordering
target_files=`for i in $new_target_files; do echo $i; done | sort`
md5=`for i in $target_files; do md5sum $tmpdir/$i; done | sed -e 's/ .*$//' | md5sum | sed -e 's/ .*$//'` || {
  echo "create-env: Couldn't compute MD5 sum."
  exit 2
}
echo "creating $md5.tar.bz2"
mydir=`pwd`
cd $tempdir
tar -cjhf "$mydir/$md5".tar.bz2 $target_files || {
  echo "create-env: Couldn't create archive"
  exit 3
}
cd ..
rm -rf $tempdir
