#!/bin/bash
#
# This script locates a distribution package in a ROSE test directory.
#  examines the package and imports it as a vendor drop into an external repository.
# It assumes a hudson layout of test dir as following:
#   x. ROSE_SOURCE_PATH: a checkout copy of ROSE
#   x. ROSE_BUILD_PATH: a build dir for ROSE
# It conducts sanity check for the package, especially
#   x. Make sure no EDG copyrighted files exist
#   x. Make sure the EDG binaries for all supported platforms do exist
#   x. Remove generated lexer and scanners from source distributions
# It imports the package which passes the check to 
#  the head of an external repository using svn_load_dirs.1.4.5.pl
# Use
#  thisScript ROSE_SOURCE_PATH ROSE_BUILD_PATH
#
# by Liao, 7/31/2008
# Last Modified 10/29/2008
#------------------------------------------------------

# the backup one is for testing only
#REMOTE_SVN_REPOS=file:////home/liao6/backup/SCIDAC.MIRROR
REMOTE_SVN_REPOS=https://outreach.scidac.gov/svn/rose

if [ $# -lt 2 ]
then
  echo This script needs two argument to run.
  echo Example: $0 ROSE_SOURCE_PATH ROSE_BUILD_PATH
  exit 1
fi

# cleanup, svn_load_dirs.pl has some trouble to clean up itself. We help it here as well
# remove previous left over temporary dir generated by svn_load_dirs.??.pl
rm -rf /tmp/svn_load_dirs*

# obtain and test the input parameters
ROSE_SOURCE_PATH=$1
ROSE_BUILD_PATH=$2
if [ -d ${ROSE_SOURCE_PATH} ]; then
  if [ -f ${ROSE_SOURCE_PATH}/rose_config.h.in ] ;
   then
     echo ${ROSE_SOURCE_PATH} is verified to be a rose source tree
   else
     echo Fatal error: ${ROSE_SOURCE_PATH} does not seem to be a rose source tree!
     exit 3
  fi
else  
  echo Fatal error: ${ROSE_SOURCE_PATH} does not exist!
  exit 3
fi

if [ -d ${ROSE_BUILD_PATH} ]; then
  if [ -f ${ROSE_BUILD_PATH}/rose_config.h ] ;
   then
     echo ${ROSE_BUILD_PATH} is verified to be a rose build tree
   else
     echo Fatal error: ${ROSE_BUILD_PATH} does not seem to be a rose build tree!
     exit 3
  fi
else  
  echo Fatal error: ${ROSE_BUILD_PATH} does not exist!
  exit 3
fi

# update ChangeLog2
cd ${ROSE_SOURCE_PATH}
if test -f ${ROSE_SOURCE_PATH}/ChangeLog2; then
  :
else
  echo "Fatal Error: ${ROSE_SOURCE_PATH}/ChangeLog2 does not exist as expected!" 
  exit 1
fi
rm -rf ChangeLog2
git log --name-status --no-merges --pretty=medium &>ChangeLog2

cd ${ROSE_BUILD_PATH}

# we now have a different name scheme
# set max depth to avoid finding 
# src/frontend/CxxFrontend/roseBinaryEDG-x86_64-pc-linux-gnu-GNU-4.2-14fb2b019a16bb36f47609dad91a1807.tar.gz
# find the form of rose-version-rev.tar.gz
ROSE_DISTRIBUTION=$(find . -maxdepth 1  -name  rose-\*-\*.tar.gz)

if [ $? -ne 0 ]; then
   echo "Fatal error: cannot find the distribution package!"
   exit 1
fi

# double check here!! Liao, 9/25/2009

# this will be caught by the error code of tar
#ROSE_DISTRIBUTION_2=`echo $ROSE_DISTRIBUTION | cut -f 3 -d \ `
#if [ "x$ROSE_DISTRIBUTION_2" != "x" ]; then
#    echo "Fatal error Found more than one rose distribution packages!! $ROSE_DISTRIBUTION"
#    exit 3
#fi

echo Found the ROSE distribution package: $ROSE_DISTRIBUTION

#ROSE_SRC=${ROSE_TOP_TEST_DIR}/sourcetree
# hudson's current work directory would be the source tree also
ROSE_SRC=${ROSE_SOURCE_PATH}

UPLOAD_DIR=${ROSE_BUILD_PATH}/upload
rm -rf ${UPLOAD_DIR}
mkdir -p ${UPLOAD_DIR}

# unpack the package to the work dir and get the root dir of the distribution
#----------------------------------------------------
tar xzvf ${ROSE_DISTRIBUTION} -C ${UPLOAD_DIR}
if [ $? -ne 0 ]; then
   echo "Fatal error for: tar xzvf ${ROSE_DISTRIBUTION} -C ${UPLOAD_DIR}"
   exit 1
fi

cd ${UPLOAD_DIR}
ROSE_DIST_DIR=$(ls ${UPLOAD_DIR})
echo "Unpacked ROSE distribution directory is: ${ROSE_DIST_DIR}"

# Find all unwanted directories and remove them
#----------------------------------------------------
find . -name .svn | xargs rm -rf
find . -name Makefile.in~ | xargs rm -rf
find . -name autom4te.cache | xargs rm -rf

# remove any EDG source tree: EDG_SAGE_Connection, EDG_3.10, EDG_3.3 EDG_4.0 etc.
rm -rf ${ROSE_DIST_DIR}/src/frontend/CxxFrontend/EDG/EDG_*

# remove configure and Makefile.in
rm -rf ${ROSE_DIST_DIR}/configure
rm -rf ${ROSE_DIST_DIR}/aclocal.m4
rm -rf ${ROSE_DIST_DIR}/rose_config.h.in
rm -rf ${ROSE_DIST_DIR}/libltdl
find . -name Makefile.in | xargs rm -rf
# remove generated scanners and parsers available from the source distribution
# But should not be put into the external repository
rm -rf ${ROSE_DIST_DIR}/src/frontend/SageIII/ompparser.cc
rm -rf ${ROSE_DIST_DIR}/src/frontend/SageIII/ompparser.h
rm -rf ${ROSE_DIST_DIR}/src/frontend/SageIII/omplexer.cc
rm -rf ${ROSE_DIST_DIR}/src/frontend/OpenFortranParser_SAGE_Connection/preproc-fortran.cc
rm -rf ${ROSE_DIST_DIR}/src/frontend/OpenFortranParser_SAGE_Connection/preproc-fortran-fixed.cc
rm -rf ${ROSE_DIST_DIR}/src/frontend/SageIII/preproc.cc
# dot2gml is not yet in distribution, added them here as a precaution 
rm -rf ${ROSE_DIST_DIR}/src/roseIndependentSupport/dot2gml/parseDotGrammar.cc
rm -rf ${ROSE_DIST_DIR}/src/roseIndependentSupport/dot2gml/parseDotGrammar.h
rm -rf ${ROSE_DIST_DIR}/src/roseIndependentSupport/dot2gml/parseDot.cc
rm -rf ${ROSE_DIST_DIR}/projects/DocumentationGenerator/doxygenComment.C
rm -rf ${ROSE_DIST_DIR}/src/midend/programAnalysis/annotationLanguageParser/language-lexer.cc
rm -rf ${ROSE_DIST_DIR}/src/midend/programAnalysis/annotationLanguageParser/language-parser.cc
rm -rf ${ROSE_DIST_DIR}/src/midend/programAnalysis/annotationLanguageParser/language.tab.h

# add ChangeLog2 into the distribution
# Liao, 11/17/2009
if test -f ${ROSE_SOURCE_PATH}/ChangeLog2; then
  :
else
  echo "Fatal Error: ${ROSE_SOURCE_PATH}/ChangeLog2 does not exist as expected!" 
  exit 1
fi
cp ${ROSE_SOURCE_PATH}/ChangeLog2 ${ROSE_DIST_DIR}/.
rm -rf .*.swp

# restore a few third party Makefile.in which cannot yet been automatically built
cp ${ROSE_SRC}/src/3rdPartyLibraries/libharu-2.1.0/script/Makefile.in ${ROSE_DIST_DIR}/src/3rdPartyLibraries/libharu-2.1.0/script/Makefile.in
cp ${ROSE_SRC}/src/3rdPartyLibraries/libharu-2.1.0/Makefile.in ${ROSE_DIST_DIR}/src/3rdPartyLibraries/libharu-2.1.0/Makefile.in 
cp ${ROSE_SRC}/src/3rdPartyLibraries/libharu-2.1.0/src/Makefile.in ${ROSE_DIST_DIR}/src/3rdPartyLibraries/libharu-2.1.0/src/Makefile.in
cp ${ROSE_SRC}/src/3rdPartyLibraries/libharu-2.1.0/include/Makefile.in ${ROSE_DIST_DIR}/src/3rdPartyLibraries/libharu-2.1.0/include/Makefile.in

# Make sure no EDG copyrighted files exist
#----------------------------------------------------

# We search for some representative source files of EDG
EDG_FILES=($(find . -name il_def.h -or -name cp_gen_be.c -or -name lower_il.h))
#EDG_FILES=($(find . -name Makefile.am))
if [ ${EDG_FILES[0]} ]; then
  echo Fatal Error: Found copyrighted EDG source files:${EDG_FILES[@]}
  exit 1
fi

# and the copyright string of EDG: "Proprietary information of Edison Design Group Inc."
EDG_COPYRIGHT_STRINGS=($(find . -name \*.C -or -name \*.h -or -name \*.c -or -name \*.cpp| xargs grep 'Proprietary information of Edison Design Group Inc.'))

if [ ${EDG_COPYRIGHT_STRINGS[0]} ]; then
  echo Fatal Error: Found copyrighted EDG text in source files:${EDG_COPYRIGHT_STRINGS[@]}
  exit 2
fi

# -------------------- we no longer put EDG binaries into the external svn repository--------
rm -rf ${ROSE_DIST_DIR}/src/frontend/CxxFrontend/roseBinaryEDG-*.tar.gz

#if true; then
## Make sure all binary EDG versions for three platforms exist:i686-apple, i686-redhat, 
## and x86_64-redhat, using a bash array (index starting from 0) to test it.
#EDG_BINARIES1=($(ls ${ROSE_DIST_DIR}/src/frontend/CxxFrontend/roseBinaryEDG-i686-pc-linux-gnu*.tar.gz))
#if [ ${EDG_BINARIES1[0]} ]; then
#   echo Found the EDG binaries for i686-pc-linux as expected. It is ${EDG_BINARIES1[@]}. 
#else 
#  echo Fatal Error: No EDG binaries for i686-pc-linux are available. 
#  exit 3
#fi
#fi
#
#EDG_BINARIES2=($(ls ${ROSE_DIST_DIR}/src/frontend/CxxFrontend/roseBinaryEDG-x86_64-pc-linux-gnu*.tar.gz))
#if [ ${EDG_BINARIES2[0]} ]; then
#   echo Found the EDG binaries for x86_64-pc-linux-gnu as expected. It is ${EDG_BINARIES2[@]}. 
#else 
#  echo Fatal Error: No EDG binaries for x86_64-pc-linux-gnu are available. 
#  exit 3
#fi
#
#if true; then
# EDG_BINARIES3=($(ls ${ROSE_DIST_DIR}/src/frontend/CxxFrontend/roseBinaryEDG-i686-apple-darwin*.tar.gz))
# if [ ${EDG_BINARIES3[0]} ]; then
#    echo Found the EDG binaries for i686-apple-darwin as expected. It is ${EDG_BINARIES3[@]}. 
# else 
#   echo Fatal Error: No EDG binaries for i686-apple-darwin are available. 
#   exit 3
# fi
#fi

# Now we can upload the package
#--------------------------------------------------------
${ROSE_SRC}/scripts/svn_load_dirs.1.4.5.pl -svn_username liaoch -no_user_input ${REMOTE_SVN_REPOS} trunk ${ROSE_DIST_DIR}

if [ $? -ne 0 ]; then
   echo "Error: svn_load_dirs finishes abnormally!"
   exit 3
else
  echo "svn_load_dirs.pl finishes normally."
fi
#rm -rf ${UPLOAD_DIR}
