#!/bin/sh

. config/options

GCC_PKG_DIR=`find $PACKAGES -type d -name gcc`

make -C $PKG_BUILD install

if [ "$2" = toolchain ]; then
  GCC_VERSION=`sed -n 's/.*\/gcc-\([0-9\.]*\)\..*/\1/p' $GCC_PKG_DIR/url`
  CROSS_CC=$TARGET_CC-$GCC_VERSION
  rm -f $TARGET_CC
  echo "#!/bin/sh" >$TARGET_CC
  echo "$ROOT/$TOOLCHAIN/bin/ccache $CROSS_CC \"\$@\"" >>$TARGET_CC
  chmod +x $TARGET_CC
  # To avoid cache trashing
  DATE="0501`echo $GCC_VERSION | sed 's/\([0-9]\)/0\1/g' | sed 's/\.//g'`"
  touch -c -t $DATE $CROSS_CC

  CROSS_CXX=$TARGET_CXX-$GCC_VERSION
  if [ -f "$TARGET_CXX" ]; then
    [ ! -f "$CROSS_CXX" ] && mv $TARGET_CXX $CROSS_CXX
    echo "#!/bin/sh" >$TARGET_CXX
    echo "$ROOT/$TOOLCHAIN/bin/ccache $CROSS_CXX \"\$@\"" >>$TARGET_CXX
    chmod +x $TARGET_CXX
    # To avoid cache trashing
    touch -c -t $DATE $CROSS_CXX
  fi
else
  echo "#!/bin/sh" >$HOST_CC
  echo "$ROOT/$TOOLCHAIN/bin/ccache $LOCAL_CC \"\$@\"" >>$HOST_CC
  chmod +x $HOST_CC
  echo "#!/bin/sh" >$HOST_CXX
  echo "$ROOT/$TOOLCHAIN/bin/ccache $LOCAL_CXX \"\$@\"" >>$HOST_CXX
  chmod +x $HOST_CXX
fi
