#!/bin/bash
# Thanks to lucabon for this script that makes a symlink to the
# rustlib instead of trying to copy all of /usr/lib (which also happens
# to not work when /usr/lib64 is the system lib directory).

TMP=$1
LIBDIRSUFFIX=$2

triplet=`basename $(dirname $(echo /usr/lib$LIBDIRSUFFIX/rustlib/*/lib))`
d=$TMP/build/$triplet/stage0-sysroot/lib$LIBDIRSUFFIX/rustlib/$triplet/lib

count=`date +%s`

while true
do
  if [ -d $d -a ! -h $d ];then
    echo "`date` - Relinking..."
    rm -rfv $d; ln -sv /usr/lib$LIBDIRSUFFIX/rustlib/$triplet/lib $d
  fi
  let elapsed=`date +%s`-$count
  if [ $elapsed -gt 60  ]; then
    running=`ps -ef | grep $TMP | grep -v grep | grep -vc linkrustlib.sh`
    if [ $running -gt 0 ]; then
      count=`date +%s`
    elif [ $elapsed -gt 120 ]; then
      echo "EXITING"
      exit
    fi
  fi
done
