#!/bin/sh

echo ""
echo "start import tbPort dat>>"

echo "CurDir=`pwd`"

source /etc/omdio.conf

DOCnt=`echo $om_do|tr -cd ','|wc -c`
DOCnt=`expr $DOCnt + 1`
DICnt=`echo $om_di|tr -cd ','|wc -c`
DICnt=`expr $DICnt + 1`
AICnt=2
RS232Cnt=`cat /etc/rc.local | grep -c RS232`
RS485Cnt=`cat /etc/rc.local | grep -c RS485`

echo "om_do=$om_do"
echo "om_di=$om_di"
echo "DOCnt=$DOCnt"
echo "DICnt=$DICnt"
echo "AICnt=$AICnt"
echo "RS232Cnt=$RS232Cnt"
echo "RS485Cnt=$RS485Cnt"

cd /NandFlash
echo "CurDir=`pwd`"

sqlite3 dbCfg.db3 "delete from tbPort"
# DI
for((i=1;i<=$DICnt;i++));  
do     
	sqlite3 dbCfg.db3 "insert into tbPort values('DI$i','',1,$(expr $i + 8388608),1)"
	echo "DI$i:$(expr $i + 8388608)"
done 


# DO
for((i=1;i<=$DOCnt;i++));  
do     
	sqlite3 dbCfg.db3 "insert into tbPort values('DO$i','',2,$(expr $i + 8388608),1)"
	echo "DO$i:$(expr $i + 8388608)"
done 

# AI
for((i=1;i<=$AICnt;i++));  
do
	sqlite3 dbCfg.db3 "insert into tbPort values('AI$i','',1,$(expr $i + 512),2)"
	echo "AI$i:$(expr $i + 512)"
done 

#232
addr=0
for((i=1;i<=$RS232Cnt;i++));  
do
	sqlite3 dbCfg.db3 "insert into tbPort values('RS232-$i:','',3,$(expr 1048576 + $addr + $i),0)"
	echo "RS232-$i:$(expr 1048576 + $addr + $i)"
done 

addr=`expr $addr + $RS232Cnt`
#485
for((i=1;i<=$RS485Cnt;i++));  
do
	sqlite3 dbCfg.db3 "insert into tbPort values('RS485-$i:','',4,$(expr 1048576 + $addr + $i),0)"
	echo "RS485-$i:$(expr 1048576 + $addr + $i)"
done 

echo ">>end import tbPort dat"
echo ""
