#!/bin/bash

#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Jakub Mazanek jmazanek@redhat.com
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_applies_to "dhcpv6"
#END GENERATED SECTION
SOLUTION_TEXT=""
if [ -z $SOLUTION_FILE ]; then
  SOLUTION_FILE="./solution.txt"
fi
cat /dev/null > $SOLUTION_FILE

sysconfig_dir="/etc/sysconfig"
dhcpconf="/etc/dhcp6s.conf"
new_conf_add="/root/new_conf_add"
aidfile="/root/aidfile"
rm_comment () {
              egrep -v "^#|^ *#" $1
              }
entry () {
        field=$1
         awk -v field="$field" '{
           for (i = field ; i <= NF; i++) {
            printf("%s ", $i);
           }
         printf("\n") }'
         }
prefix=$(rm_comment "$dhcpconf" | grep -w 'prefix' | entry 2 )


#set -x
  if ! [[ -z "$dhcpconf" ]];then
   rm_comment "$dhcpconf" | grep "link *" > /dev/null 2>/dev/null
    if [ $? -eq 0 ];then

      rm_comment "$dhcpconf" | grep 'option dns_servers' | entry 3 > $aidfile
                  while read line; do  echo "option dhcp6.name-servers $line" >> $new_conf_add ; done < "$aidfile" 
      rm_comment "$dhcpconf" | grep 'option domain-name' | entry 3 > $aidfile
                  while read line; do  echo "option dhcp6.domain-search $line" >> $new_conf_add ; done < "$aidfile" 
      echo "subnet6 $prefix {" > $new_conf_add
      rm_comment "$dhcpconf" | grep 'range' | entry 2 > $aidfile
                  while read line; do  echo "                                  range6 $line" >> $new_conf_add ; done < "$aidfile" 
      echo "}" >> $new_conf_add
#set +x
    fi
  fi

echo "For the dhcp server with IPv6 support equivalent to the original system, add the following entries to the /etc/dhcp/dhcpd6.conf file:

$(cat $new_conf_add)

" >> $SOLUTION_FILE
rm -rf "$aidfile"
rm -rf "$new_conf_add" 


exit $RESULT_INFORMATIONAL
