#!/bin/bash

TMPFILE=".t.dts"
ECHO="true"

if [ `whoami` != "root" ]; then
	echo "使用 root 用户或者 sudo 执行本程序"
	exit -1
fi

if [ $# != 1 ] || ([ $1 != "on" ] && [ $1 != "off" ]); then
	echo "参数错误，请使用一个 on 或者 off 作为参数传递"
	exit 0;
fi

if [ -f '/lib/modules/`uname -r`/build/scripts/dtc/dtc' ]; then
	echo "设置安全模式需要使用到内核开发包，请安装内核开发环境"
	exit -2
fi

for file in `ls /boot/dtb/*.dtb`
do
	/lib/modules/`uname -r`/build/scripts/dtc/dtc -I dtb -O dts $file > $TMPFILE
	sed -i '/security-button/d' $TMPFILE
	sed -i "/security,ft-1500a/a\\\tsecurity-button = \"$1\";" $TMPFILE
	grep -rn "security {" $TMPFILE &> /dev/null
	if [ $? == 0 ]; then
		ECHO="false"
	fi
	/lib/modules/`uname -r`/build/scripts/dtc/dtc -I dts -O dtb $TMPFILE > $file.1
done

if [ $ECHO == "true" ]; then
	echo "当前 /boot 分区的所有 dtb 文件都不支持安全开关"
elif [ $1 == "on" ]; then
	echo "安全开关打开成功"
elif [ $1 == "off" ]; then
	echo "安全开关关闭成功"
fi

rm -rf $TMPFILE
