#!/bin/bash
#
# You must have passwordless access to SERVER for deploy-user.
#
#

ARGC=$#
SCRIPTNAME=`basename $0`

if [ $ARGC -ne 1 ]; then
  echo "Usage: $SCRIPTNAME <month>"
fi


SERV_IP="192.168.1.208"
SERV_PORT="11112"
SAET=DCM4CHEE
# Path to source archive and year
YEAR=2021
PA2AR=/mnt/1/lib/vservers/lin01/var/lib/dcm4chee/server/default/archive/$YEAR


list="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
# You can limit days in month
#list="1 2 3 4 5"
#list="25 26 27"

#echo $(dirname $PA2AR)

# Disable time synchronization
ssh deploy@$SERV_IP "sudo timedatectl set-ntp no"

# Find directories under the month (i.e. days for month)
for A in `find $PA2AR/$1 -maxdepth 1 -type d `
do
  for B in $list
  do
# Set limit for days by list-array  
    if [ "${A##*/}" == "$B" ]; then
# Pass the month-number directory (i.e. parent dir is year)
      if [ "$(dirname $A)" != "$PA2AR" ]; then

#        echo ==${A##*/}==
#        echo _${A}_$B
        echo ========day===========$B
        echo "$YEAR-$1-$B 11:11:11"
#       Set date on SERVER       
        ssh deploy@$SERV_IP "sudo timedatectl set-time \"$YEAR-$1-$B 11:11:11\""        
        sleep 2
#        find $A -not -type d
#       Send dcm-file to SERVER        
        find $A -not -type d -exec storescu -aet $SAET -aec $SAET $SERV_IP $SERV_PORT {} \;
#        find $A -not -type d -exec storescu -aet BNK_INTEK -aec DCM4CHEE 192.168.1.203 11112 {} \;
#        find $A -not -type d -exec echo {} \;
      fi  
    fi  
  done  
done

# Enable time synchronization (remote command)
ssh deploy@$SERV_IP "sudo timedatectl set-ntp yes"

  