#!/bin/bash
#
#
#                                  _JNJ`                        
#                              .JNMH`                           
#                             JMMF`       `;.                   
#                           .NMM)          `MN.                 
#                           MMM)            (MML                
#                          (MMM`             MMML               
#                    M  I  D  N  I  G  H  T    C  o  D  E       
#                          (NMMF             MHNH               
#                           NMML            .MMM                
#                            NMML          .NMH                 
#                             4MMNL       .#F                   
#                              `4HNNL_   `                      
#                                  `"""`                        
#
#                         Copyright (C) 2004-2005
#      "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>
#
#   Midnight Code is a registered trademark of Ian Latter.
#
#   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, and mirrored at the Midnight Code web
#   site; as at version 2 of the License only.
# 
#   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
#   (version 2) along with this program; if not, write to the Free
#   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#   02111-1307 USA, or see http://midnightcode.org/gplv2.txt
#
#


## Script global variables
do_download="0"
struct_head="struct midnightcode_interface_vendor_t "
struct_more="midnightcode_interface_vendors[] = {"
struct_last="  { 0, NULL }"
struct_tail="};"
max_entries="#define MIDNIGHTCODE_INTERFACE_VENDORS"


## Write header
echo "/*"
echo
echo "                                   _JNJ\`                        "
echo "                               .JNMH\`                           "
echo "                              JMMF\`       \`;.                   "
echo "                            .NMM)          \`MN.                 "
echo "                            MMM)            (MML                "
echo "                           (MMM\`             MMML               "
echo "                     M  I  D  N  I  G  H  T    C  o  D  E       "
echo "                           (NMMF             MHNH               "
echo "                            NMML            .MMM                "
echo "                             NMML          .NMH                 "
echo "                              4MMNL       .#F                   "
echo "                               \`4HNNL_   \`                      "
echo "                                   \`\"\"\"\`                        "
echo
echo "                          Copyright (C) 2004-2006"
echo "       \"Ian (Larry) Latter\" <ian dot latter at midnightcode dot org>"
echo
echo "    Midnight Code is a registered trademark of Ian Latter."
echo
echo "    This program is free software; you can redistribute it and/or modify"
echo "    it under the terms of the GNU General Public License as published by"
echo "    the Free Software Foundation, and mirrored at the Midnight Code web"
echo "    site; as at version 2 of the License only."
echo
echo "    This program is distributed in the hope that it will be useful,"
echo "    but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU"
echo "    General Public License for more details."
echo
echo "    You should have received a copy of the GNU General Public License"
echo "    (version 2) along with this program; if not, write to the Free"
echo "    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA"
echo "    02111-1307 USA, or see http://midnightcode.org/gplv2.txt"
echo
echo "*/"
echo
echo
echo "/* midnightcode-interface-vendor.h :: Midnight Code eth vendor library	*/"
echo
echo
echo "#ifdef MIDNIGHTCODE_INTERFACE_NEED_VENDOR"
echo "# ifndef __MIDNIGHTCODE_INTERFACE_VENDOR_H"
echo "# define __MIDNIGHTCODE_INTERFACE_VENDOR_H 1"
echo
echo
echo "/* NOTE: Auto-generated at `date` */"
echo


## Get master list
if [ "${do_download}x" == "1x" ]
then
  if [ -f "oui.txt" ]
  then
    rm -f "oui.txt"
  fi
  wget -c -r http://standards.ieee.org/regauth/oui/oui.txt
fi
if [ ! -f "oui.txt" ]
then
  echo "Error: oui file not found (enable do_download)!" >&2
  exit 1
fi


## Calculate entries
entries=0
entries=`cat oui.txt | \
  grep "^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F] " | \
  wc -l`
entries=$(( ${entries} + 0 ))
echo "${max_entries} ${entries}"
echo


## Generate table
echo -n "${struct_head}"
echo "${struct_more}"
cat oui.txt | \
  grep "^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F] " | \
  tr -cd '\012\015\040-\043\050-\051\053-\073\101-\132\141-\172' | \
  cut -c1-7,21- | \
  while read entry
  do
    entry_index=`echo ${entry} | cut -f1 -d" "`
    entry_name=`echo ${entry} | cut -f2- -d" " | sed s#'\"'#'\\\"'#g`
    if [ "${entry_index}x" == "${entry_name}x" ]
    then
      entry_name="Unknown / Private"
    fi
    echo "  { 0x${entry_index}, \"${entry_name}\" },"
  done
echo "${struct_last}"
echo "${struct_tail}"


## Write footer
echo
echo
echo "# endif"
echo "#endif"
