#!/bin/sh
  #         
  # license: Standard BSD2CLAUSE (BSD 2-clause Simplified License),
  # Please read from the web.
  #         
            
  #         
  # dynip is a daemon that monitors your host system ISP assigned dynamic IP 
  # address. If it changes, dynip automatically updates the dynamic DNS IP
  # address at your dynamic DNS hosting provider then posts an informational
  # message to the host system log followed by sending an informational email
  # to user root.
  #         
  # NOTE: Most DNS hosting providers that provide dynamic DNS services use a
  # variation of the default browser URL format used here. A simple user 
  # substitution to the "fetch" command for the browser URL format used by your
  # dynamic DNS hosting provider will allow this script to work for you.   
  #
  # See man dynip for details.
  #
  # On purpose the new IP address is not included in the standard browser
  # style URL format because the dynamic DNS hosting providers update process
  # will use the IP address of the sending update request as the value to
  # update their records to.
            
  prev_ip_file="/usr/local/etc/dynip"
            
  # Cycle time to wait between ip checks.
  #         
  #elapse_time="7200" # 7200 seconds = 2 hours
  #elapse_time="3600" # 3600 seconds = 1 hour
  #elapse_time="1800" # 1800 seconds = 30 minutes
  elapse_time="900"  # 1800 seconds = 15 minutes
  #elapse_time="600"  # 600  seconds = 10 minutes
  #elapse_time="300"  # 300  seconds =  5 minutes
  #elapse_time="10"   # 10   seconds for testing  
            
            
  # On start up check to see if file contaning previous IP address exists.
  # If not populate with default value.  
  #         
  if [ ! -f "${prev_ip_file}" ]; then
     prev_ip="0.0.0.0"
     echo "${prev_ip}" > ${prev_ip_file}
  fi        
            
            
  # Find the NIC device name of the NIC connected to the public internet.
  # The default interface obtained from route command is the one connected
  # to the public internet.
  #         
  nic_devicename=$(route get -inet default 2> /dev/null | \
    grep -o "interface.*" | cut -d ' ' -f 2)
            
            
  # This is the continuous loop checking the current ip address to
  # determin if it changed.
  #         
  while true; do
            
    # Get the IP address assigned to that NIC device name.
    #        
    current_ip=$(ifconfig $nic_devicename inet | \
      grep -m 1 -o inet.* | cut -d ' ' -f 2)
            
    prev_ip="$(cat ${prev_ip_file})"
            
    if [ "${current_ip}" != "${prev_ip}" ]; then
            

      # Update dynamic DNS hosting provider with new IP address.
      #     
      #   Insert best match example from man dynip here.

      #################### start insert here ##########################

      echo "/usr/local/sbin/dynip"
      echo "Has not been customized yet, terminated"
      exit 99

      ####################  end  insert here ##########################


      # For testing, all messages go to screen
      #fetch -q -o - ${url}

      # For Production usage
      fetch --user-agent="dynip Freebsd" -q -o /var/log/dynip.log ${url}
            
            
      if [ $? -ne 0 ]; then
        log_msg="Error: /usr/bin/fetch command failed."
        echo "$log_msg"
            
        # Send email to host root to inform about update error.
        #   
        subject="DYNIP Status report: Update error."
        echo "$log_msg" | /usr/bin/mail -s "${subject}" root@$(hostname)
            
        exit 3
      fi    
            
      # Good Update.
      #     
      echo "${current_ip}" > ${prev_ip_file}
      prev_ip="${current_ip}"        
      log_msg="Success: Dynamic DNS service updated:"
      log_msg="${log_msg} IP address set to ${current_ip}"
      echo "$log_msg"
            
      # Send email to host root to inform about ip address change.
      subject="DYNIP Status report: Dynamic IP address changed."
      echo "$log_msg" | /usr/bin/mail -s "${subject}" root@$(hostname)
    fi      
            
    sleep ${elapse_time}
  done  
# done &   # uncomment to run from the command line for testing.
  exit 0


