#!/bin/bash
 #   # mysterious_b0x3n_configurator.sh
 #   # Written by yom <yom@nainwak.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 2
 # 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.
 #
 # Visit http://www.gnu.org/copyleft/gpl.html for details.
   hextomac() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
    zob=`echo $pouet |awk '{print substr($0,1,2)"."substr($0,3,2)"."substr($0,5,2)"."substr($0,7,2)"."substr($0,9,2)"."substr($0,11,2)}'`
  eval "$1=$zob";
 }
   mactohex() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
  
  zob=`echo $pouet |awk '{print substr($0,1,2)""substr($0,4,2)""substr($0,7,2)""substr($0,10,2)""substr($0,13,2)""substr($0,16,2)}'`
  eval "$1=$zob";
 }
   hextodec() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
  
  zob=`echo "16i $pouet p" |dc`
  eval "$1=$zob";
 }
   dectohex() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
  
  zob=`echo "$pouet 16o p" |dc`
  eval "$1=$zob";
 }
   pad() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
  
  zob=`echo $pouet |awk '{while(length($0)<12){$0="0"$0} print $0}'`
  eval "$1=$zob";
 }
   inc() {
  brol=\$"$1"
  
  pouet=`eval "expr \"$brol\" "`
  
  mactohex pouet
  hextodec pouet
  pouet=`echo "$pouet 1 + p" |dc`
  dectohex pouet
  pad pouet
  hextomac pouet
  eval "$1=$pouet";
 }
  
 ADDRFILE=/tmp/mac.txt
   # 1 - Ouvrir un fichier (mac.txt) et aller a la derniere ligne pour recuperer la derniere adresse MAC utilisée
 MAC=`tail -1 $ADDRFILE`
   # 2 - Incrementer cette adresse MAC
 inc MAC
   for i in `seq 1 100000` ; do
  # 3 - Demander a l'utilisateur de brancher un boitier sur le reseau puis de taper ENTER
  echo "Veuillez brancher un boitier sur le reseau et appuyer sur ENTER, ou Ctrl-C pour quitter..."
  read BASHSUX
    # 4 - Envoyer la requete de configuration a la carte avec wget
  wget -q -t 1 -O /tmp/result.txt 192.168.1.250/cgi/setparameters.cgi?mac=$MAC
    # 5 - On verifie dans le fichier recuperé par wget (reponse du boitier) si la sauvegarde s'est bien passé.
  if grep -q "sauvegardeok" /tmp/result.txt ; then
  	echo "[OK] Sauvegarde effectuee - Adresse MAC : $MAC"
  	echo $MAC >> $ADDRFILE
  	inc MAC
  else
  	echo "[ERREUR] La sauvegarde a echoue"
  fi
  
 done  |