#!/bin/bash
. incl_var
echo "--WARNING: This script will clear environment variables on older Speedports without EVA Bootloader!!--"
echo "If this is the case you need to add some individual variables into this script at the marked position."
echo "Make sure only one netcard is installed in your PC, or set an interface via Speed-to-fritz menu.------"
echo "You must run ./install-start first, to set up OEM, IPADRESS and LAN! ---------------------------------"
echo "Or you may edit in ./incl_var the following three variables:"
echo 'export OEM="avm"'
echo 'export ETH_IF="eth0"'
echo 'export IPADDRESS="192.168.178.1"'
echo "------------------------------------------------------------------------------------------------------"
echo "--------------------------------------- ${IPADDRESS} ------------------------------------------------"
echo "--------------------------------------- BRANDIG:  $OEM ------------------------------------------------"
CNET=${IPADDRESS%.*}
NET=${CNET%.*}
ETHLIST=" eth0 eth1 eth2 eth3"
if [ $ETH_IF ]; then 
 ETHLIST="$ETH_IF"
 echo "Net cards set up for search active netconnections: $ETHLIST"
 echo "This was set via command line parameter of sp-to-fritz.sh."
else 
 echo "Net cards set up for search active netconnections: $ETHLIST"
 echo "This list was set default, but can be set in advance." 
fi
for ETH in ${ETHLIST} ; do
  if  ifconfig $ETH > /dev/null ; then
  echo "Ethernet card found on: $ETH"
   break
  fi  
done  
ifconfig $ETH add ${CNET}.10 &> /dev/null
ifconfig $ETH | grep 'inet addr' | sed -e "s/inet addr/PC Netcardaddr: /g"

PTEST=" finalize=1 subnet=2.10"
touch empty
for i in ${PTEST} ; do
	case $i in
	 subnet=*)
	    PTEST_SERVER=${NET}.${i##subnet=}
	    export PTEST_SERVER
	 echo "PTEST_SERVER=$PTEST_SERVER"
	esac
done
echo "You should now reboot your box. Waiting for box to shut down for restart ..."
while [ `ping -c1 -w1 ${IPADDRESS} | grep 'receive' | awk '{ print $4 }'` == "1" ]; do
echo -n "."
sleep 1
done 
while [ `ping -c1 -w1 ${IPADDRESS} 2>&1 | grep -c 'Unreachable'` != "0" ] ||\
[ `ping -c1 -w1 ${IPADDRESS} | grep 'receive' | awk '{ print $4 }'` == "0" ]; do
 echo -n "."
done
echo  "."
echo "Reset Environment..."
ftp -n -p <<EOT
open ${IPADDRESS}
user adam2 adam2
binary
debug
hash
quote MEDIA FLSH
put ./empty mtd3
put ./empty mtd4
quote SETENV my_ipaddress 192.168.178.1
quote SETENV jffs2_size 32
quote SETENV firmware_version $OEM
quote SETENV kernel_args annex=$ANNEX
quote REBOOT
quit
EOT
echo "All done .... Branding set to '$OEM', Press 'ENTER' to finish"
while !(read -s);do
	sleep 1
done

exit 0

Comment:
if you use this script to downgrade to tcom firmware you must use: firmware_version tcom
quote SETENV wlan_key key
replace "key" with the key you like to use (factory key can be found on the back of your box)