#!/bin/sh
#
# Restores miner configs from factory defaults
#

FACTORY=/etc/factory
CONF=/nvdata/anthillos/config

restore_from_dir() {
	dir=$1

	if [ ! -s "$CONF/system.conf" ] && [ -f "$dir/system.conf" ]; then
		cp "$dir/system.conf" "$CONF"
	fi

	if [ ! -s "$CONF/network.conf" ] && [ -f "$dir/network.conf" ]; then
		cp "$dir/network.conf" "$CONF"
	fi

	if [ ! -f "$CONF/mac" ] && [ -f "$dir/mac" ]; then
		cp "$dir/mac" "$CONF"
	fi

	if [ ! -s "$CONF/ssh.conf" ] && [ -f "$dir/ssh.conf" ]; then
		cp "$dir/ssh.conf" "$CONF"
	fi

	if [ ! -s "$CONF/dashd.conf" ] && [ -f "$dir/dashd.conf" ]; then
		cp "$dir/dashd.conf" "$CONF"
	fi

	if [ ! -s "$CONF/password" ] && [ -f "$dir/password" ]; then
		cp "$dir/password" "$CONF"
	fi

	if [ ! -s "$CONF/hotelfee.json" ] && [ -f "$dir/hotelfee.json" ]; then
		cp "$dir/hotelfee.json" $CONF
	fi
}

restore() {
	restore_from_dir "$FACTORY"

	# Grant ownership to miner user
	# and set proper file permissions
	chown -R miner:miner "$CONF"
	chmod -R u+rw,g+r,a-x "$CONF"/*
	sync
}

case "$1" in
	start)
		restore;;
	stop)
		:;;
	*)
		echo "Usage: $0 {start|stop}"
		exit 1
esac

exit $?
