#!/bin/sh
# Set the installation directories for the Xtensa hardware files.

# Customer ID=7729; Build=0x71521; Copyright (c) 2004-2007 Tensilica Inc.  ALL RIGHTS RESERVED.
# These coded instructions, statements, and computer programs are the
# copyrighted works and confidential proprietary information of Tensilica Inc.
# They may not be modified, copied, reproduced, distributed, or disclosed to
# third parties in any manner, medium, or form, in whole or in part, without
# the prior written consent of Tensilica Inc.

#
# usage: fix_hw_install_path <tools_root> <xtensa_root> <config_root>
#

tools="$1"
swtools="$2"
prefix="$3"

factory_tools='C:/././usr/xtensa/tools-7.0';
factory_swtools='C:/././build/tree/RG-2017.5_kuma/tools/swtools-MSWin32-x86';
factory_prefix='C:/././build/build/RG-2017.5/ttiEng/vpawar/hifi3_hikey960/269583/RG-2017.5-win32/hifi3_hikey960';

# Directories to search under PREFIX
subdirs="Hardware CSM";

for sname in $subdirs; do
  SDIR="$prefix/$sname";
  if [ -d "$SDIR" ]; then
    echo "Setting paths in the $sname directory. (This may take a while.)"
    echo "    temporarily making files writable...."
    # Save owner permissions as group permissions
    find $SDIR -type f -perm -u+w | xargs -r chmod -f ug+w
    # Do not make regular files group writeable 
    find $SDIR -type f ! -perm -u+w | xargs -r chmod -f u+w
    echo "    setting the paths...."
    find $SDIR -type f | xargs "$tools/bin/perl" -p -i \
	-e "s'$factory_prefix'$prefix'g ; s'$factory_swtools'$swtools'g ; s'$factory_tools'$tools'g"
    echo "    restoring the original file permissions...."
    # Restore owner permission from group permission
    find $SDIR -type f ! -perm -g+w | xargs -r chmod -f ug-w
    find $SDIR -type f -perm -ug+w | xargs -r chmod -f g-w
  fi
done
