#!/bin/bash

if [ -z "$1" ]; then
  echo "Usage: $0 file.sym"
  exit
fi

KVERSION=2
KPATCHLEVEL=0
KSUBLEVEL=33
SRC="$1"
BASE=`basename $SRC .sym`
BASE1=`echo $BASE | sed -e 's/-/_/g'`
DEST="$BASE.c"
XSYM1="unknown"
XSYM2="unknown"
if [ $KVERSION -eq 2 ]; then
  if [ $KPATCHLEVEL -eq 1 ]; then
    XSYM1="EXPORT_SYMBOL("
    XSYM2=");"
  else
    XSYM1="X("
    XSYM2="),"
  fi
fi
if [ "$SRC" = "$BASE" ]; then
  echo "Error (1)"
  exit
fi

echo -n "generating $DEST from $SRC... "

cat > $DEST << EOF
/*
 *  File generated by script export-symbols from file $SRC...
 *  Don't edit!!!
 */

#define EXPORT_SYMTAB
#include "driver.h"
EOF

awk '{ XLINE=$$1; if ( substr( XLINE, 0, 1 ) == "+" ) { print substr( XLINE, 3, length( XLINE ) - 1 ) } }' $SRC >> $DEST

cat >> $DEST << EOF

#ifndef LINUX_2_1
struct symbol_table snd_symbol_table_$BASE1 = {
#include <linux/symtab_begin.h>
#endif
EOF

awk -v XSYM1=$XSYM1 -v XSYM2=$XSYM2 '{ XLINE=$$1; if ( substr( XLINE, 0, 1 ) == "#" ) { print "  /* " substr( XLINE, 3, length( XLINE ) - 1 ) " */" } else if ( substr( XLINE, 0, 1 ) == "+" ) { ; } else { print  "  " XSYM1 XLINE XSYM2 } }' $SRC >> $DEST

cat >> $DEST << EOF
#ifndef LINUX_2_1
#include <linux/symtab_end.h>
};
#endif
EOF

echo "done"
