#!/bin/sh

SUB_DIR=$1
EXEC="/srv/IPv6Enable/isp/sa/global_route_grt/libbgpdump-new/bgpdump"
PARENT_DIR="./result"

if [ ! -e $PARENT_DIR/$SUB_DIR/CHECK_STAMP ]; then
  touch -t "200001010000.00" $PARENT_DIR/$SUB_DIR/CHECK_STAMP
fi

#find $PARENT_DIR/$SUB_DIR -newer $PARENT_DIR/$SUB_DIR/CHECK_STAMP -name "*.bz2" -print | xargs -r ls -rt | xargs $EXEC
find $PARENT_DIR/$SUB_DIR -newer $PARENT_DIR/$SUB_DIR/CHECK_STAMP -name "*.bz2" -print | xargs -r ls -rt | xargs --replace $EXEC {} >> ./sum
find $PARENT_DIR/$SUB_DIR -newer $PARENT_DIR/$SUB_DIR/CHECK_STAMP -name "*.bz2" -print | xargs -r ls -rt | xargs --replace echo "{}" >> ./log_file_list

target_file=$(find $PARENT_DIR/$SUB_DIR -newer $PARENT_DIR/$SUB_DIR/CHECK_STAMP -name "*.bz2" -print | xargs -r ls -t | head -n 1)

if [ -e "$target_file" ]; then
	touch -t $(date -r $target_file +%Y%m%d%H%M.%S) $PARENT_DIR/$SUB_DIR/CHECK_STAMP
	find $PARENT_DIR/$SUB_DIR -type f -name "update*" -mtime +2 -print | xargs rm -rf
else
    echo "no-update-found"
fi
