#!/bin/sh

myname=`basename $0`
workdir=`pwd`
USAGE="Usage: $myname <no_of_processes> <pdb_list_file> <db_level> [imembrane_options]"
#DBDIR="/data/pegasus/DATABASES/imemdb_projected"

processes=$1
if ! [ $1 ] || [[ ${processes:0:1} == "-" ]]; then
  echo $USAGE 1>&2
  exit 1
fi
shift


pdblist=$1
if ! [ $pdblist ] || ! [ -f $pdblist ]; then
  echo $USAGE 1>&2
  exit 1
fi
shift


dblevel=$1
if ! [ $dblevel ]; then
  echo $USAGE 1>&2
  exit 1
fi
shift

let nextlevel=$dblevel+1


echo "Number of sub-processes: $processes"
echo "PDB list file: $pdblist"
echo "db_level to search: $dblevel"
echo "db_level to build: $nextlevel"
echo "iMembrane options:" $@


a=(`cat $pdblist`)


if ! [ -d db ]; then
  echo "Cannot find subdirectory 'db' (containing all the database levels) in working directory." 1>&2
  exit 1
fi

mkdir -p input

cd input

THREADLIST=""
for (( threadid=0; threadid < processes; threadid++ )); do
  # Start sub-process
  #
  (
      echo "Starting thread: $threadid"
      
      # Resume information
      if [ -f $threadid.status ]; then
        savedstate=`cat $threadid.status`
      else
        savedstate=0
      fi
      
      i=0
      while (( i < ${#a[@]} )); do
        pdb=${a[$i]}         # 1A91
        let i=i+1
        
        # Multi-threading: Only run this thread's share of jobs
        #
        if (( $i % $processes != $threadid )); then
          continue
        fi
        
        # Resume support: Skip jobs that we've already done
        #
        if (( $savedstate >= $i )); then
          continue
        fi
        
        if ! [ -f $pdb.pdb ]; then
          getpdbchain $pdb || continue
        fi
        
        #~ c=($pdb.pdb)
        #~ if ! [ -f $c ]; then
          #~ c=(`splitchain -p $pdb.pdb`)
        #~ fi
        
        #for d in ${c[@]}; do
          #chainname=`echo $d | sed 's/.pdb//g'`
          chainname=$pdb
          chaincode=${chainname:4}
          pdb=${chainname::4}
          
          path=($workdir/db/*/entries/$pdb.*.$chaincode.*.*)
          if ! [ -d $path ]; then
            newname=$pdb.0.$chaincode.0.$nextlevel
            echo "Thread $threadid : $chainname : $newname"
            mkdir -p $chainname
            
            imembrane --dbdir $workdir/db/$dblevel --struc $chainname.pdb --inputid $newname --outdir $chainname --maxhits 1 --write-input-sequence $@ &
            procid=${!}
            trap "kill -TERM $procid 2>/dev/null; exit 1" INT TERM KILL EXIT
            wait $procid
            
            if [ -e $chainname/hits.table ]; then
              besthit=`egrep -v '^#' $chainname/hits.table | head -n1`
              subdir=`echo $besthit | cut -d' ' -f1`
              inputid=`echo $besthit | cut -d' ' -f2`
              dbid=`echo $besthit | cut -d' ' -f 3`
              #dblevel=`echo $dbid | cut -d. -f5`
              #let dblevel+=1
              mkdir -p $workdir/db/$nextlevel
              mv $chainname/$subdir $workdir/db/$nextlevel/$newname
            fi
          fi
          rm -rf $chainname
        #done
        
        echo $i > $threadid.status
      done
  ) &
  thread_pid=$!
  THREADLIST="$THREADLIST $thread_pid"
done

# If the main process is terminated, kill all sub-processes too
#
trap "kill -TERM $THREADLIST 2>/dev/null; exit 1" INT TERM KILL EXIT
wait

rm -f *.status
