#!/bin/sh
# platform=`uname -i`
platform=`echo $HOST_ARCH | cut -d\- -f2-100`
linkname=`readlink O.linux-${platform}`

if [ -n "$linkname" ]; then {
  echo O.linux-${platform} is linked with "$linkname"

  if [ "$linkname" != "O.linux-${platform}_P" ]; then {
    if [ -d O.linux-${platform}_P ]; then
      rm -f O.linux-${platform}	
      ln -sf O.linux-${platform}_P O.linux-${platform}
    else
      mkdir O.linux-${platform}_P
      rm -f O.linux-${platform}
      ln -sf O.linux-${platform}_P O.linux-${platform}
    fi
  }
  fi 
}
else
  if [ ! -d O.linux-${platform}_P ]; then 
    echo "This is the first time to call this script."
    echo "An O.linux-${platform}_P directory will be created for parallel elegant"
  else   #called make clean already
    echo "The O.linux-${platform}_P directory will be cleaned"
    mv O.linux-${platform}_P O.linux-${platform}    
  fi
  make clean
  mkdir O.linux-${platform}_P
  ln -sf O.linux-${platform}_P O.linux-${platform}
fi

if [ -f Makefile.TMP ]
then
  mv -f Makefile.TMP Makefile
fi
# Avoid start from every beginning if Pelegant is not built sucessfully
touch ../../bin/linux-${platform}/Pelegant  
make Pelegant
