#! /bin/sh
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-08-01 23:10:01 +0300 (Sun, 01 Aug 2021) $
#$Revision: 8845 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.8.1/scripts/codcif2sdf $
#------------------------------------------------------------------------------
#*
#* Convert CIFs with crystal description (similar to those obtained from
#* the COD) to an SDF file with a molecule description.
#* Intended to be suitable for upload to PubChem.
#*
#* USAGE:
#*   $0 --options < file1.cif
#*   $0 --options file1.cif
#*   $0 --options file1.cif file2*.cif
#**

TMP_DIR="${TMPDIR}"

set -ue
## set -x

script() { echo "# $*"; cat; }
setvar() { eval $1="'$3'"; }

setvar FILES = ""

setvar BASENAME = "`basename $0`"

setvar IGNORE_BUMPS_OPT = ""

#* OPTIONS:
#*   -i, --ignore-bumps
#*                     Ignore atomic bumps while running cif_molecule.
#*
#*   --tmp-dir /tmp
#*                     Use the specified temporary directory (default: '/tmp').
#*
#*   --help, --usage
#*                     Output a short help message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
while [ $# -gt 0 ]
do
  case $1 in
      -i|--ignore-bumps|--ignore-bump|--ignore-bum|--ignore-bu|--ignore-b|\
      --ignore|--ignor|--igno|--ign|--ig|--i)
          IGNORE_BUMPS_OPT="-i"
          ;;
      --tmp-dir|--tmp-di|--tmp-d|--tmp|--tm|--t)
          TMP_DIR="$2"
          shift
          ;;
      --options|--option|--optio|--opti|--opt|--op|--o)
          echo "$(basename "$0"):: The '--options' option is a placeholder."
          echo "$(basename "$0"):: It should be replaced by one of the following options:"
          awk '/#\* OPTIONS:/,/#\*\*/ {
                  sub("OPTIONS:", "");
                  sub("^ *#[*]?[*]?", "");
                  gsub("\\$0","'"$0"'");
                  print $0
              }' "$0"
          exit
          ;;
      --help|--hel|--he|--h|--usage)
          awk '/#\*/,/#\*\*/ {
                  sub("^ *#[*]?[*]?", "");
                  gsub("\\$0","'"$0"'");
                  print $0
              }' "$0"
          exit
          ;;
      --version)
          $(dirname $0)/cod-tools-version
          exit
          ;;
      -*) echo "`basename $0`:: ERROR, unknown option '$1'." >&2 ; exit 1 ;;
      *)  FILES="$FILES '$1'" ;;
    esac
    shift
done

## echo ${FILES}
eval set -- "${FILES}"

test -n "${FILES}" && FILES="-"

test -z "${TMP_DIR}" && TMP_DIR="/tmp"
TMP_DIR="${TMP_DIR}/tmp-${BASENAME}-$$"
mkdir "${TMP_DIR}"

TMP_SPLIT_DIR="${TMP_DIR}/split"
mkdir ${TMP_SPLIT_DIR}

TMP_CIF="${TMP_DIR}/-"

## set -x

for CIF in ${1+"$@"}
do
    if [ ${CIF} = "-" ]
    then
        echo $0: $CIF: reading CIF from STDIN... >&2
        cat > ${TMP_CIF}
        CIF=${TMP_CIF}
    else
        if [ ! -e ${CIF} ]
        then
            echo $0: $CIF: file does not exist. >&2
            continue
        fi
    fi

    cif_molecule \
        --preserve-stoichiometry \
        --one-datablock-output \
        --split-disorder-groups \
        --largest-molecule-only \
        ${IGNORE_BUMPS_OPT} \
        "${CIF}" \
    | molcif2sdf --cod-cif "${CIF}" --tmp-dir "${TMP_DIR}"

done

## set -x
rm -rf "${TMP_DIR}"
