#!/bin/ash

[ -z "$ROOT" ] && ROOT=/www
CONFIG=$ROOT/config
SCRIPT=$ROOT/script
TYPE=$ROOT/models

cp -rf $TYPE/init/etc/* /etc/

# Init model
suportedModels=`ls $TYPE 2>/dev/null | grep -v init`
model=`$SCRIPT/model.sh 2>/dev/null`

if [ -z "$model" ] ; then
	echo "Cannot detect hardware model."
	echo "Suported models: $suportedModels"
  exit 1
fi

echo "Init model $model"

[ -d "$TYPE/$model/etc" ] && cp -rf $TYPE/$model/etc/* /etc/
[ -d "$TYPE/$model/root" ] && cp -rf $TYPE/$model/root/* $ROOT

if [ -f "$TYPE/$model/init.sh" ] ; then
	sh "$TYPE/$model/init.sh"
fi

chmod -R +x /etc/hotplug.d/*
chmod -R +x /etc/rc.button/*
chmod -R +x $SCRIPT

sh $TYPE/init-dependencies.sh