#!/system/bin/sh
#
# Copyright (C) 2008 The Android Open-Source Project
# Copyright (C) 2013 OmniROM Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# NOTE: this script depends on samsung rild
#
#       ril.wifi_macaddr and ril.bt_macaddr are set by samsung rild
#
#       Both can only be read after the initial boot has completed.
#       This is because init does update the property db in a way
#       that getprop can NOT read the updates until the boot
#       sequence has finished.
#

WLMAC=$(getprop ril.wifi_macaddr)
if [ -n "$WLMAC" ]; then

  # write updated wifi driver config
  cat /system/vendor/firmware/nvram_net.txt | sed "s/macaddr=\([0-9a-fA-F]\{2\}\:\?\)\+/macaddr=$WLMAC/" > /data/.nvram_net.txt
  chown radio.radio /data/.nvram_net.txt
  chmod 0664 /data/.nvram_net.txt

fi

# /data/.nvram_net.txt *needs* to exist, even if the MAC is incorrect.
# $WLMAC is set too late in the boot process for this script to work on the first boot.
# Go ahead and copy stock nvram_net.txt just in case so that wifi doesn't crash the
# system if the user tries to start it on first boot.
if [ ! -f /data/.nvram_net.txt ]; then

  cp /system/vendor/firmware/nvram_net.txt /data/.nvram_net.txt
  chown radio.radio /data/.nvram_net.txt
  chmod 0664 /data/.nvram_net.txt

fi

BTMAC=$(getprop ril.bt_macaddr)
if [ -n "$BTMAC" ]; then

  # write bluetooth mac
  echo -n $(echo $BTMAC | cut -f2 -d: | sed 's/\(..\)/\1:/g;s/:$//') > /data/.nvram_btmac
  chown bluetooth.net_bt_stack /data/.nvram_btmac
  chmod 0664 /data/.nvram_btmac

fi

# set path to bluetooth mac file
setprop ro.bt.bdaddr_path /data/.nvram_btmac
