#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#   use lspci and NIC to detect etherboot rom
#

backtitle="DRBL Wizard"
title="etherboot_detect"

# locale
if [ "$1" != "" ]; then
  LOCALE="$1"
  LC_ALL="$1"
  export LC_ALL
else
  LOCALE="zh_TW.Big5"
  LC_ALL="zh_TW.Big5"
  export LC_ALL
fi

# text or x-window
case "`tty`" in
  /ram1/dev/tty[1-8])
    MODE=text
    DIA=dialog
    ;;
  /ram1/dev/pts/*|/ram1/dev/ttyp*)
    MODE=x
    XDIALOG_HIGH_DIALOG_COMPAT=1
    export XDIALOG_HIGH_DIALOG_COMPAT
    DIA=Xdialog
    ;;
  *)
    MODE=text
    DIA=dialog
    ;;
esac

#if [ -z "$DISPLAY" ] ; then
  MODE=text
  DIA=dialog
#fi

### main
etherboot_version=$(ls /etc/hwdata/etherboot-*)
etherboot_version=${etherboot_version/\/etc\/hwdata\/etherboot-/}

MSG_BIG5="oӵ{|UzӾdһݭn Etherboot ($etherboot_version) ROM"
MSG_EN="This program will help you to detect Etherboot ($etherboot_version) ROM for your NICs"
if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" \
  --msgbox "$MSG" 15 60

## detect
total=0
roms=""
for pciid in `lspci -n | cut -d' ' -f4`
do
  vendorid=$(echo $pciid | cut -d: -f1)
  deviceid=$(echo $pciid | cut -d: -f2)
  rom=$(grep "0x$vendorid,0x$deviceid" /etc/hwdata/etherboot-$etherboot_version | awk '{ print $1".lzdsk" }')
  if [ "$rom" != "" ]; then
    total=`expr $total + 1`
    roms="$roms $rom"
  fi
done

## report
MSG_BIG5="즳 $total id, UOnϥ\n"
MSG_EN="There are $total NICs detected, use \n"
for rom in $roms
do
  MSG_BIG5="$MSG_BIG5    $rom\n"
  MSG_EN="$MSG_EN    $rom\n"
done
MSG_EN="$MSG_EN""respectively\n"
MSG_BIG5="$MSG_BIG5\n\
Цb DRBL AΥHUOͺϤ\n\
 # cat /tftpboot/etherboot-$etherboot_version/[ROM] > /dev/fd0\n\n\
Ϊ̱zƱγoi CD zs@}?"
MSG_EN="$MSG_EN\n\
Use the followring command to create diskette in the DRBL server\n\
 # cat /tftpboot/etherboot-$etherboot_version/[ROM] > /dev/fd0\n\n\
Or would you like to create boot disk here?"

if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
else MSG=$MSG_EN; fi
$DIA --backtitle "$backtitle" --title "$title" \
  --yesno "$MSG" 15 60

if [ $? -eq 0 ]; then 

  num=1
  for rom in $roms
  do
    MSG_BIG5="дJ $num iϤnо"
    MSG_EN="Please insert $num diskette in the floppy"
    if [ "$LOCALE" = "zh_TW.Big5" ]; then MSG=$MSG_BIG5
    else MSG=$MSG_EN; fi
    $DIA --backtitle "$backtitle" --title "$title" --msgbox "$MSG" 15 60
    cat /usr/local/etherboot-$etherboot_version/$rom > /dev/fd0
    num=`expr $num + 1`
  done

fi
