#!/bin/sh

# pc_cleansrc
# -----------
# Description:
#   Removes all symbolic links created by 'pc_setupsrc'.
#   Removes all compiled executables and other build objects.
#   Localized source files (see pc_localize) are maintained.
#   Data and other files in the run directory are maintained.

delete_links()
{
  links=$1
  # delete symlinks to scripts in $PENCIL_HOME
  for file in $links
  do
    # echo "$file"
    if [ -L $file ]; then
      rm -f $file
    fi
  done
}

delete_files()
{
  files=$1
  # delete regular files from compilation and running
  for file in $files
  do
    if [ -f $file ]; then
      rm -f $file
    fi
  done
}

clean_dir()
{
  dir=$1
  objects=$2
  # delete object files from compilation
  for object in $objects
  do
    for file in $dir/$object
    do
      if [ -f $file ]; then
        rm -f $file
      fi
    done
  done
  # delete symlinks in the source directory
  for file in $dir/*
  do
    if [ -L $file ]; then
      rm -f $file
    fi
  done
  # enter sub-directories
  for test in $dir/*
  do
    if [ -d $test ]; then
      clean_dir "$test" "$objects"
    fi
  done
  rmdir $dir >/dev/null 2>&1
}

clean_objects="*.o *.a *.mod *.x *.inc .build-history .buildinfo .buildtime"
clean_links="start.csh start_run.csh run.csh getconf.csh Makefile src/.cvsignore"
clean_files=".pc_build_history run_command.log COMPLETED ERROR LOCK STOP FULLSTOP src/Makefile src/mpicomm_double.f90 src/revision.txt src/.current-precision"
src_dir="src"

echo "Cleaning up run directory..."
delete_links "$clean_links"
delete_files "$clean_files"
clean_dir "$src_dir" "$clean_objects"
echo "...done."


