#!/bin/sh
#
# Copyright (c) 2005-2007 The ABINIT Group (Yann Pouillon)
# All rights reserved.
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

#
# Setup
#

# Internal parameters
debug="no"

# Get command-line arguments
my_name=`basename $0`
if [ ${#} -lt 2 ]; then
 echo "Usage: ${my_name} origin destination [shell_bin] [perl_bin] [python_bin]"
 exit 10
fi

s_orig=${1}
s_dest=${2}
if [ ${#} -ge 2 ]; then
 shell_bin=${3}
fi
if [ ${#} -ge 3 ]; then
perl_bin=${4}
fi
if [ ${#} -ge 4 ]; then
python_bin=${5}
fi

# Look for SH
if test "${shell_bin}" = ""; then
 if test "${debug}" = "yes"; then
  echo -n "Looking for BOURNE-AGAIN SHELL ... "
 fi
 shell_bin=`which bash`

 if test "${shell_bin}" != ""; then
  if test "${debug}" = "yes"; then
   echo "found."
  fi
 else
  if test "${debug}" = "yes"; then
   echo "not found."
   echo -n "Looking for BOURNE SHELL ... "
  fi
  shell_bin=`which sh`
 fi

 if test "${shell_bin}" != ""; then
  if test "${debug}" = "yes"; then
   echo "found."
  fi
 else
  if test "${debug}" = "yes"; then
   echo "not found => aborting."
   echo ""
   echo "Your system does not provide the minimal Bourne shell."
   echo "This is a severe violation of Unix standards."
   echo "You should probably check the sanity of this host."
   echo ""
  fi
  exit 1
 fi
fi

# PERL
if test "${perl_bin}" = ""; then
 if test "${debug}" = "yes"; then
  echo -n "Looking for PERL executable ... "
 fi
 perl_bin=`which perl`

 if test "${perl_bin}" != ""; then
  if test "${debug}" = "yes"; then
   echo "found."
  fi
 else
  if test "${debug}" = "yes"; then
   echo "not found => aborting."
  fi
  exit 2
 fi
fi

# Python
if test "${python_bin}" = ""; then
 if test "${debug}" = "yes"; then
  echo -n "Looking for PYTHON executable ... "
 fi
 python_bin=`which python`

 if test "${python_bin}" != ""; then
  if test "${debug}" = "yes"; then
   echo "found."
  fi
 else
  if test "${debug}" = "yes"; then
   echo "not found => aborting."
  fi
  exit 3
 fi
fi

# Write report
if test "${debug}" = "yes"; then
cat <<EOF
Executables
-----------

SHELL  : ${shell_bin}
PERL   : ${perl_bin}
PYTHON : ${python_bin}

EOF
fi

#
# Search for scripts
#

if test "${debug}" = "yes"; then
 echo -n "Looking for scripts ... "
fi

scripts=`find ${s_orig} -name '*.sh' -o -name '*.pl' -o -name '*.py'`
count=`echo ${scripts} | awk '{print NF}'`

if test "${debug}" = "yes"; then
 echo "${count} found."
 echo ""
fi

#
# Process scripts
#

if test "${debug}" = "yes"; then
cat <<EOF
Script                           Type Destination
-------------------------------- ---- ----------------
EOF
fi

for s in ${scripts}
do
 s_name=`basename ${s}`
 s_type=`echo ${s_name} | sed -e 's/.*\.//'`
 s_name=`echo ${s_name} | sed -e 's/\..*//'`

 case ${s_type} in

  sh)
   echo "#!${shell_bin}" > ${s_dest}/${s_name}
   ;;

  pl)
   echo "#!${perl_bin}" > ${s_dest}/${s_name}
   ;;

  py)
   echo "#!${python_bin}" > ${s_dest}/${s_name}
   ;;

  *)
   if test "${debug}" = "yes"; then
    echo "Unsupported script type: ${s_type} => aborting."
   fi
   exit 10
   ;;

 esac

 cat ${s} >> ${s_dest}/${s_name}
 chmod 755 ${s_dest}/${s_name}

 if test "${debug}" = "yes"; then
  printf "%32s  %2s  %s\n" ${s_name} ${s_type} ${s_dest}
 fi
done

if test "${debug}" = "yes"; then
 echo ""
fi

exit 0
