#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#set -x
cfg=
version=
log=/var/log/cloud.log
log_it() {
    logger -t cloud "$*"
    echo "$(date) : $*" >> $log
}
while getopts 'c:' OPTION
do
  case $OPTION in
      c) cfg="$OPTARG"
          ;;
  esac
done
while read line
do
    #comment
    if [[ $line == \#* ]]
    then
	    continue
    fi
    if [ "$line" == "" ]
    then
        read line
        version=$line
        log_it "VR config: configuation format version $version"
        #skip 
        read line
        continue
    fi
    if [ "$line" == "
        read line
        log_it "VR config: execution success "
        continue
    fi
    if [ "$line" == "" ]
    then
        read line
        file=$line
        log_it "VR config: creating file: $file"
        rm -f $file
        while read -r line
        do
            if [ "$line" == "" ]
            then
                break
            fi
            echo $line >> $file
        done
        log_it "VR config: create file success"
        continue
    fi
done < $cfg
#remove the configuration file, log file should have all the records as well
rm -f $cfg
# Flush kernel conntrack table
log_it "VR config: Flushing conntrack table"
conntrackd -d 2> /dev/null
if [ $? -eq 0 ]; then
    conntrackd -F
    conntrackd -k
else
    conntrackd -F
fi
log_it "VR config: Flushing conntrack table completed"
exit 0