#!/bin/bash

BASEDIR=$(dirname $0)
TOOLCHAINDIR=$BASEDIR/../
TARGET=armv5l-timesys-linux-gnueabi
PATH=$BASEDIR/../fake/bin:$BASEDIR/../ccache:$BASEDIR:$BASEDIR/../usr/bin:$BASEDIR/../sbin:$BASEDIR/../usr/sbin:/usr/bin:/bin \

cross_make install DESTDIR=$BASEDIR/../

# Reset the libtool archive files (.la) for relative libdir path
cd $TOOLCHAINDIR/lib && for la in $(find . -name '*.la');do \
    subdir=$(dirname $la | sed -re 's,\.,,g'); \
    sed -i -re "s,^libdir.*,libdir='$TOOLCHAINDIR/lib${subdir}'," \
    -e "s,(\-[RLI])/usr,\1$TOOLCHAINDIR,g" \
    -e "s, /usr/, $TOOLCHAINDIR/,g" $la; \
done

cd $TOOLCHAINDIR/$TARGET/lib/ && for la in $(find . -name '*.la');do \
    subdir=$(dirname $la | sed -re 's,\.,,g'); \
    sed -i -re "s,^libdir.*,libdir='$TOOLCHAINDIR/$TARGET/lib${subdir}'," \
    -e "s,(\-[RLI])/usr,\1$TOOLCHAINDIR,g" \
    -e "s, /usr/, $TOOLCHAINDIR/,g" $la; \
done


# Reset -config scripts for relative path
for config in $(find $TOOLCHAINDIR -name '*-config');do \
    sed -i -re "s,=/usr,=$TOOLCHAINDIR,g" \
    -e "s,=\"/usr,=\"$TOOLCHAINDIR,g" \
    -e "s, /usr/, $TOOLCHAINDIR/,g" \
    -e "s,(\-[RLI])/usr,\1$TOOLCHAINDIR,g" \
    $config;done

for pc in $(find $TOOLCHAINDIR/lib/pkgconfig $TOOLCHAINDIR/usr/lib/pkgconfig $TOOLCHAINDIR/share/pkgconfig $TOOLCHAINDIR/usr/share/pkgconfig -name '*.pc' 2>/dev/null);do \
    sed -i -re "s,=/usr,=$TOOLCHAINDIR,g" \
    -e "s,=\"/usr,=\"$TOOLCHAINDIR,g" \
    -e "s,(\-[RLI])/usr,\1$TOOLCHAINDIR,g" \
    $pc;done

