#!/usr/bin/wksh -openlook
OPTIONS=$1 $2 $3 $4 $5 $6 $7 $8 $9
TFOPTS="-r -b -w -c -u"
#FONT="-adobe-helvetica-medium-r-normal--14-140-75-75-p-78-iso8859-1"

assumptions(){
cps BOX1 About noticeShell $TOPLEVEL \
	noticeType:information
sv $BOX1_TA string:"This is a Domain Name Server Setup Utility\nCreated by Steven Wootton\n\nAssumptions:\na. TCP/IP is installed and functioning\nb. The name server will handle a Class B or Class C network."
cmw SW scrolledWindow scrolledWindow $BOX1_CA width:620 height:450 vStepSize:36 vRepeatRate:1
        cmw SW1 staticText staticText $SW \
        string:"`cat help.txt`"
cmw NAMEBUTT buttons flatButtons $BOX1_CA  \
	itemFields:{label} items:{"OK"}
XtPopup $BOX1
}

createboot(){
temp1="; ns setup"
echo $temp1>/etc/inet/named.boot
}

checkroot(){
ID=`id | cut -d"(" -f2 | cut -d ")" -f1`
if [ $ID != "root" ]
then
        cmw CONTROL Warning controlArea $TOPLEVEL \
	layoutType:fixedcols \
	measure:1
	cmw msg2 message staticText $CONTROL string:"You must be root to use Name Server Setup"
        cmw NAMEBUTT buttons flatButtons $CONTROL \
        itemFields:{label} items:{Exit}
	sv $NAMEBUTT selectProc:exit
	checkrt=no
fi
}

main(){
DTE=`date +%e%m%y%H%M%S`
if [[ ! -d /var/named ]]
then
	mkdir /var/named
fi
me=`uname -n`
awk '{if ($1=="domain") printf "%s",$2}' /etc/resolv.conf > /tmp/check
dmn=`cat /tmp/check`
rm -f /tmp/check
#ifconfig -a > /tmp/test.dat
#awk '{if ($0 ~ /inet/ && $0 !~ /127.0.0.1/) printf "%s\n",$2}' /tmp/test.dat >/tmp/check
#rm -f /tmp/test/dat
#ipadd=`cat /tmp/check`
#ip1=`cut -d. -f1 /tmp/check`
#ip2=`cut -d. -f2 /tmp/check`
#ip3=`cut -d. -f3 /tmp/check`
#ip4=`cut -d. -f4 /tmp/check`
if [ ! -f /etc/inet/named.boot ]
then
	createboot
fi
awk '{if ($1=="directory") printf "%s",$2}' /etc/inet/named.boot > /tmp/check
dirn=`cat /tmp/check`
rm -f /tmp/check
if [ ! -f $dirn/named.local ]
then
	post=root@$me.$dmn.
else
	awk '{if ($1=="@") printf "%s",$5}' $dirn/named.local > /tmp/check
	post=`cat /tmp/check`
	rm -f /tmp/check
fi
if [[ $post == "" ]]
then
	post=root@$me.$dmn.	
fi
awk '{if ($3=="setup") printf "y" }' /etc/inet/named.boot >/tmp/check
soa=`cat /tmp/check`
rm  -f /tmp/check
########## control header #####
cmw CONTROL control controlArea $TOPLEVEL \
	layoutType:fixedrows measure:10

if [[ $soa != "y" || $soa == "" ]]
then
	cp /etc/inet/named.boot /etc/inet/named.boot.old
	sv $WBOX_TA string:"Copied /etc/inet/named.boot to /etc/inet/named.boot.old"
	pu $WBOX
fi
cmw msg1 message staticText $CONTROL string:"Name Server's Domain"
cmw DATA1 TextField textField $CONTROL string:$dmn charsVisible:30
if [[ $dirn == "" ]]
then
	dirn=/var/named
fi
cmw msg2 message staticText $CONTROL string:"Name Server Database Directory"
cmw DATA2 TextField textField $CONTROL string:$dirn charsVisible:30
cmw msg3 message staticText $CONTROL string:"Domain Administrator"
cmw DATA3 TextField textField $CONTROL string:$post charsVisible:30
awk '{if (($1=="primary" || $1=="secondary") && $2 !~ /in-addr.arpa/) printf "%s %s %s %s\n",$1,$2,$3,$4}' /etc/inet/named.boot > /tmp/list
cmw msg1 message staticText $CONTROL string:"Type        Controlled Domain        Database"
cmw DATAL TextField scrollingList $CONTROL viewHeight:5
sladd $DATAL "                                                                             "
sldel $DATAL all
sladd $DATAL -f /tmp/list 
cmw BAR4 controlArea controlArea $CONTROL \
	layoutType:fixedcols \
	measure:4
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Add Primary"}
sv $NAMEBUTT selectProc:newdom
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Add Secondary"}
sv $NAMEBUTT selectProc:newsec
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Modify"}
sv $NAMEBUTT selectProc:moddom
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Delete"}
sv $NAMEBUTT selectProc:deldom
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Accept/Exit"}
sv $NAMEBUTT selectProc:acceptit
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Providers"}
sv $NAMEBUTT selectProc:cachit
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Cancel"}
sv $NAMEBUTT selectProc:cancelit3
cmw NAMEBUTT buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Help"}
sv $NAMEBUTT selectProc:assumptions
}

poptemplate(){
rbutton=A
cps BOX3 input popupWindowShell $TOPLEVEL
sv $BOX3_UCA layoutType:fixedrows measure:15
cmw msg5 message staticText $BOX3_UCA string:"Enter Information for controlled domain."
cmw mess textField staticText $BOX3_UCA string:"Enter/Modify Controlled Domain Name"
cmw DOMNAME textField textField $BOX3_UCA charsVisible:26
cmw mess textField staticText $BOX3_UCA string:"Resource Records"
cmw DATAG TextField scrollingList $BOX3_UCA viewHeight:4
cmw BAR controlArea controlArea $BOX3_UCA \
	layoutType:fixedcols \
	measure:2
cmw NAMEBUTT1 buttons flatButtons $BAR \
	itemFields:{label} items:{"Add Record"}
sv $NAMEBUTT1 selectProc:pop7
cmw NAMEBUTT2 buttons flatButtons $BAR \
	itemFields:{label} items:{"Delete Record"}
sv $NAMEBUTT2 selectProc:delitm
cmw NAMEBUTT3 buttons flatButtons $BAR \
	itemFields:{label} items:{"Accept/Exit"}
sv $NAMEBUTT3 selectProc:allexit
cmw NAMEBUTT3 buttons flatButtons $BAR \
	itemFields:{label} items:{"Cancel"}
sv $NAMEBUTT3 selectProc:cancelit2
}

pop7(){
ofsv $G $CB_INDEX set:TRUE 
pu $BOX7
setvarg $G
}

poptemplate1(){
rbutton=A
cps BOX7 input popupWindowShell $TOPLEVEL x:280
sv $BOX7_UCA layoutType:fixedrows measure:15
cmw msg5 message staticText $BOX7_UCA string:"Enter the names and IP addresses\nfor all systems in this domain."
cmw mess1 message staticText $BOX7_UCA string:"Choose the Resource Record Type"
cmw G g flatExclusives $BOX7_UCA \
	itemFields:'{label}' \
	layoutType:fixedrows \
	measure:2 \
	items:'{A}, {NS}, {HINFO},{CNAME}, {MX}, {WKS}'
sv $G selectProc:"setvarg $G"
cmw mess3 textField staticText $BOX7_UCA string:"(A)Enter a TCP/IP system name"
cmw DATANAM textField textField $BOX7_UCA charsVisible:26
cmw mess2 message staticText $BOX7_UCA string:"(B)Enter the corresponding IP address"
cmw BAR7 controlArea controlArea $BOX7_UCA
cmw DATAIP1 textField textField $BAR7 string:$ip1 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIP1 
cmw DATAIP2 textField textField $BAR7 string:$ip2 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIP2
cmw DATAIP3 textField textField $BAR7 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIP3
cmw DATAIP4 textField textField $BAR7 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIP4
cmw mess5 message staticText $BOX7_UCA string:"(C)Enter Resource Record Information"
cmw RR textField textField $BOX7_UCA charsVisible:26
cmw mess4 message staticText $BOX7_UCA string:"(D)MX Priority"
cmw MXP textField textField $BOX7_UCA charsVisible:3
TextFieldOp $TFOPTS -v '[0-9]' $MXP
cmw BAR controlArea controlArea $BOX7_UCA
cmw NAMEBUTT1 buttons flatButtons $BAR \
	itemFields:{label} items:{"OK"}
sv $NAMEBUTT1 selectProc:accitm
cmw NAMEBUTT3 buttons flatButtons $BAR \
	itemFields:{label} items:{"Cancel"}
sv $NAMEBUTT3 selectProc:cancelit7
}

poptemplate2(){
cps BOX4 input popupWindowShell $TOPLEVEL
sv $BOX4_UCA layoutType:fixedrows measure:15 sameSize:none
cmw msg5 message staticText $BOX4_UCA string:"Secondary Name Server Setup"
cmw mess textField staticText $BOX4_UCA string:"Enter/Modify Controlled Domain Name"
cmw DOMNAME2 textField textField $BOX4_UCA charsVisible:26
cmw messg2 message staticText $BOX4_UCA string:"Enter the IP address of the\nPrimary Name Server for this domain"
cmw BAR4 controlArea controlArea $BOX4_UCA
cmw DATAIPB1 textField textField $BAR4 string:$bip1 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPB1
cmw DATAIPB2 textField textField $BAR4 string:$bip2 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPB2
cmw DATAIPB3 textField textField $BAR4 string:$bip3 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPB3
cmw DATAIPB4 textField textField $BAR4 string:$bip4 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPB4
cmw mess textField staticText $BOX4_UCA string:"Resource Records (Read-Only)"
cmw DATAS TextField scrollingList $BOX4_UCA viewHeight:4
cmw BAR5 controlArea controlArea $BOX4_UCA
cmw NAMEBUTT3 buttons flatButtons $BAR5 \
	itemFields:{label} items:{"Accept/Exit"}
sv $NAMEBUTT3 selectProc:allexitb
cmw NAMEBUTT3 buttons flatButtons $BAR5 \
	itemFields:{label} items:{"Cancel"}
sv $NAMEBUTT3 selectProc:cancelit
}

poptemplate3(){
cps BOX5 input popupWindowShell $TOPLEVEL
sv $BOX5_UCA layoutType:fixedrows measure:15
cmw msg5 message staticText $BOX5_UCA string:"Enter the names and IP addresses\nof your Internet Providers."
cmw mess textField staticText $BOX5_UCA string:"Current Internet Providers"
cmw DATAC TextField scrollingList $BOX5_UCA viewHeight:4
sladd $DATAC "                                           "
sldel $DATAC all
cmw mess textField staticText $BOX5_UCA string:"Enter provider's qualified domain name"
cmw DATANAMC textField textField $BOX5_UCA charsVisible:30 maximumSize:35
TextFieldOp $TFOPTS -v '[a-zA-Z.]' $DATANAMC
cmw messg2 message staticText $BOX5_UCA string:"Enter the corresponding IP address"
cmw BAR8 controlArea controlArea $BOX5_UCA
cmw DATAIPC1 textField textField $BAR8 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPC1
cmw DATAIPC2 textField textField $BAR8 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPC2
cmw DATAIPC3 textField textField $BAR8 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPC3
cmw DATAIPC4 textField textField $BAR8 charsVisible:3 maximumSize:3
TextFieldOp $TFOPTS -v '[0-9]' $DATAIPC4
cmw BAR4 controlArea controlArea $BOX5_UCA
cmw NAMEBUTT1 buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Add Provider"}
sv $NAMEBUTT1 selectProc:accitmc
cmw NAMEBUTT2 buttons flatButtons $BAR4 \
	itemFields:{label} items:{"Delete Provider"}
sv $NAMEBUTT2 selectProc:delitmc
cmw BAR9 controlArea controlArea $BOX5_UCA
cmw NAMEBUTT3 buttons flatButtons $BAR9 \
	itemFields:{label} items:{"Accept/Exit"}
sv $NAMEBUTT3 selectProc:allexitc
cmw NAMEBUTT4 buttons flatButtons $BAR9 \
	itemFields:{label} items:{"Cancel"}
sv $NAMEBUTT4 selectProc:cancelitc
}

cancelit3(){
if [[ $SAFTEY != "ON" ]]
then
	exit
else
	sv $WBOX_TA string:"Cannot Cancel\nA file modification has already been made."
	pu $WBOX
fi
}

cancelit(){
pd $BOX4
}

cachit(){
rm -f /tmp/p1
rm -f /tmp/p2
if [[ -f $dirn/named.ca ]]
then
	awk '{if ($1 ~ /99999/ ) printf "%s\n",$0}' $dirn/named.ca >/tmp/p1
	awk '{if ($1 !~ /99999/ ) printf "%s\n",$0}' $dirn/named.ca >/tmp/p2
	slnoupdate $DATAC
	sldel $DATAC all
	sladd $DATAC -f /tmp/p1
	sladd $DATAC -f /tmp/p2
	slupdate $DATAC
fi
pu $BOX5
}

cancelitc(){
sldel $DATAC all
pd $BOX5
}

cancelit2(){
pd $BOX3
pd $BOX7
}

cancelit7(){
pd $BOX7
}

setvarg(){
umw $mess5
umw $RR
umw $mess4
umw $MXP
umw $mess2
umw $BAR7
umw $DATANAM
umw $mess3
ofgv $1 $CB_INDEX label:rbutton
if [[ $rbutton == "A" ]]
then
	mw $mess3
	mw $DATANAM
	mw $mess2
	mw $BAR7
	sv $WBOX_TA string:"This Resource Record Type requires\n a system name (A) and an IP address. (B)\nExample: woody and 137.65.46.46"
	pu $WBOX
fi
if [[ $rbutton == "NS" ]]
then
	mw $mess5
	mw $RR
	sv $WBOX_TA string:"This Resource Record Type requires a \nsystem and domain name in the Resource Record Information Box. (C)Example: woody.novell.com"
	pu $WBOX
fi
if [[ $rbutton == "HINFO" ]]
then
	mw $mess3
	mw $DATANAM
	mw $mess5
	mw $RR
	sv $WBOX_TA string:"This Resource Record Type requires\n a system name (A) and Hardware Information\nin the Resource Record Information Box. (C)\nExample: woody and PC-UNIX-INTEL"
	pu $WBOX
fi
if [[ $rbutton == "CNAME" ]]
then
	mw $mess3
	mw $DATANAM
	mw $mess5
	mw $RR
	sv $WBOX_TA string:"This Resource Record Type requires\n a system name (A) and an alias system name.\nin the Resource Record Information Box. (C)\nExample: woody and roadrunner"
	pu $WBOX
fi
if [[ $rbutton == "WKS" ]]
then
	mw $mess3
	mw $DATANAM
	mw $mess2
	mw $BAR7
	mw $mess5
	mw $RR
	sv $WBOX_TA string:"This Resource Record Type requires\n a system name (A), an IP address (B), and general information in the Resource Record Information Box. (C)\nExample: woody and 137.65.46.46 and (TCP, NFS, PPPP, etc)"
	pu $WBOX
fi
if [[ $rbutton == "MX" ]]
then
	mw $mess3
	mw $DATANAM
	mw $mess5
	mw $RR
	mw $mess4
	mw $MXP
	sv $WBOX_TA string:"This Resource Record Type requires a\n (system name.domain name) in the Resource\nRecord Information Box. (C) Example: woody.novell.com\nA MX Priority number (D) is also required. Example: 10\nOptionally, a subdomain entry can be inserted\n in box (A) to prepend the entry\nbefore the domain name. Example: junk."
	pu $WBOX
fi
}


allexit(){
gv $DOMNAME string:dname
if [[ $dname == "" ]]
then
	sv $WBOX_TA string:"Please Enter a Domain name\nand try again."
	pu $WBOX
else
	if [[ $DATAL_CURITEM == "" ]]
	then
		DATAL_CURITEM=NULL
	fi
	slnoupdate $DATAG
	sldel $DATAG all
	slupdate $DATAG
	sv $DOMNAME string:""
	pd $BOX7
	pd $BOX3
	gv $DATA3 string:postf
	gv $DATA1 string:domainf
	echo '@		IN SOA '$domainf.'	'$postf '(' > $dirn/$dname.hosts
	echo '		'$DTE' ;Serial' >>/$dirn/$dname.hosts
	echo '		10800 ;Refresh' >>/$dirn/$dname.hosts
	echo '		3600 ;Retry' >>/$dirn/$dname.hosts
	echo '		432000 ; Expire' >>/$dirn/$dname.hosts
	echo '		86400) ;Minimum' >>/$dirn/$dname.hosts
	cat /tmp/newmod >>$dirn/$dname.hosts
	deldom
	echo primary $dname $dname.hosts >> /tmp/list
	slnoupdate $DATAL
	sldel $DATAL all
	sladd $DATAL -f /tmp/list
	slupdate $DATAL
	DATAL_CURITEM=""
	SAFTEY=ON
	rm -f /tmp/newmod
	rm -f /tmp/check
	rm -f /tmp/check2
	sv $DATANAM string:""
	sv $DOMNAME string:""
	sv $DATAIP3 string:""
	sv $DATAIP4 string:""
	sv $RR string:""
fi
}

allexitb(){
sst1=
sst2=
sst3=
sst4=
gv $DOMNAME2 string:sdname
gv $DATAIPB1 string:sst1
gv $DATAIPB2 string:sst2
gv $DATAIPB3 string:sst3
gv $DATAIPB4 string:sst4
check8=
if [[ $sst1 == "" || $sst2 == "" || $sst3 == "" || $sst4 == "" ]]
then
	sv $WBOX_TA string:"Please Input the IP address of the Primary\nName Server for this Domain."
	pu $WBOX
	check8=y
else
	if [[ $sdname == "" ]]
	then
	sv $WBOX_TA string:"Please Enter a valid, fully qualified Domain name."
	pu $WBOX
	check8=y
	fi
fi
if [[ $check8 != "y" ]]
then
	if [[ $DATAL_CURITEM == "" ]]
	then
		DATAL_CURITEM=NULL
	fi
	sv $DOMNAME2 string:""
	pd $BOX4
	deldom
	echo secondary $sdname $sst1.$sst2.$sst3.$sst4 s$sdname.hosts >> /tmp/list
	slnoupdate $DATAS
	sldel $DATAS all
	slupdate $DATAS
	slnoupdate $DATAL
	sldel $DATAL all
	sladd $DATAL -f /tmp/list
	slupdate $DATAL
	DATAL_CURITEM=
	SAFTEY=ON
	sv $DOMNAME2 string:""
	sv $DATAIPB1 string:""
	sv $DATAIPB2 string:""
	sv $DATAIPB3 string:""
	sv $DATAIPB4 string:""
fi
}

allexitc(){
slnoupdate $DATAC
sldel $DATAC all
slupdate $DATAC
pd $BOX5
rm -f $dirn/named.ca
cat /tmp/p1 >$dirn/named.ca
cat /tmp/p2 >>$dirn/named.ca
rm -f /tmp/p1
rm -f /tmp/p2
SAFTEY=ON
}

newdom(){
DATAL_CURITEM=""
modfile="junker"
ttype=""
edom=""
sv $DOMNAME string:""
sv $DATAIP1 string:""
sv $DATAIP2 string:""
sv $DATAIP3 string:""
sv $DATAIP4 string:""
rm -f /tmp/newmod
touch /tmp/newmod
sladd $DATAG "                              "
sldel $DATAG all
pu $BOX3 GrabNone
}

newsec(){
DATAL_CURITEM=""
sv $DOMNAME2 string:""
sv $DATAIPB1 string:""
sv $DATAIPB2 string:""
sv $DATAIPB3 string:""
sv $DATAIPB4 string:""
sladd $DATAS "                              "
sldel $DATAS all
pu $BOX4 GrabNone
}

deldom(){
if [[ $DATAL_CURITEM == "" ]]
then
	sv $WBOX_TA string:"Nothing Selected.\nPlease select a domain record\nand try again."
	pu $WBOX
else
	echo $DATAL_CURITEM > /tmp/check
	first=`cut -d' ' -f1 /tmp/check`
	last=`cut -d' ' -f2 /tmp/check`
	vlast=`cut -d' ' -f3 /tmp/check`
	awk '{if ($1!=FST && $2!=FST && $3!=FST) printf "%s\n",$0}' FST=$first FST=$last FST=$vlast /tmp/list >/tmp/list2
	mv /tmp/list2 /tmp/list
	slnoupdate $DATAL
	sldel $DATAL all
	sladd $DATAL -f /tmp/list
	slupdate $DATAL
fi
}

delitm(){
if [[ $DATAG_CURITEM == "" ]]
then
	sv $WBOX_TA string:"Nothing Selected.\nPlease select a record\nand try again."
	pu $WBOX
else
	echo $DATAG_CURITEM > /tmp/check
	first=`cut -d' ' -f1 /tmp/check`
	second=`cut -d' ' -f2 /tmp/check`
	last=`cut -d' ' -f3 /tmp/check`
	vlast=`cut -d' ' -f4 /tmp/check`
	awk '{if ($1!=FST && $2!=FST && $3!=FST && $4!=FST ) printf "%s\n",$0}' FST=$first FST=$second FST=$last FST=$vlast /tmp/newmod >/tmp/newmod2
	mv /tmp/newmod2 /tmp/newmod
	slnoupdate $DATAG
	sldel $DATAG all
	sladd $DATAG -f /tmp/newmod
	slupdate $DATAG
fi
}

delitmc(){
if [[ $DATAC_CURITEM == "" ]]
then
	sv $WBOX_TA string:"Nothing Selected.\nPlease select a record\nand try again."
	pu $WBOX
else
	echo $DATAC_CURITEM > /tmp/check
	first=`cut -d' ' -f1 /tmp/check`
	final=`cut -d' ' -f4 /tmp/check`
	awk '{if ($4!=FST) printf "%s\n",$0}' FST=$final /tmp/p1 >/tmp/p1t
	mv /tmp/p1t /tmp/p1
	awk '{if ($1!=FST) printf "%s\n",$0}' FST=$first /tmp/p2 >/tmp/p2t
	mv /tmp/p2t /tmp/p2
	slnoupdate $DATAC
	sldel $DATAC all
	sladd $DATAC -f /tmp/p1
	sladd $DATAC -f /tmp/p2
	slupdate $DATAC
fi
}

moddom(){
echo $DATAL_CURITEM > /tmp/check
GF=`cut -d' ' -f1 /tmp/check`
modfile=`cut -d' ' -f3 /tmp/check`
sip=`cut -d' ' -f4 /tmp/check`
edom=`cut -d' ' -f2 /tmp/check`
rm -f /tmp/newmod
if [[ $DATAL_CURITEM == "" ]]
then
	sv $WBOX_TA string:"Modify only pertains to domain records\nPlease select a record and try again."
	pu $WBOX
else
	if [[ $GF == "primary" ]]
	then
		sldel $DATAG all
		sv $DOMNAME string:$edom
		awk '{if ($0 !~ /SO/) printf "%s	 %s %s	 %s %s\n",$1,$2,$3,$4,$5}' $dirn/$modfile > /tmp/unk
		awk '{if ($0 ~ /NS/) printf "	 %s %s	 %s %s\n",$1,$2,$3,$4,$5}' /tmp/unk > /tmp/newmod
		awk '{if ($0 ~ /A/ || $0 ~ /MX/ || $0 ~ /HINFO/ || $0 ~ /CNAME/ || $0 ~ /WKS/) printf "%s	 %s %s	 %s %s\n",$1,$2,$3,$4,$5}' /tmp/unk >> /tmp/newmod
		sladd $DATAG -f /tmp/newmod
		pu $BOX3 GrabNone
	else
		sldel $DATAS all
		sv $DOMNAME2 string:$edom
		awk '{if ($0 !~ /SO/) printf "%s	 %s %s	 %s %s\n",$1,$2,$3,$4,$5}' $dirn/$sip > /tmp/unk
		awk '{if ($0 ~ /A/ || $0 ~ /NS/ || $0 ~ /MX/ || $0 ~ /HINFO/ || $0 ~ /CNAME/ || $0 ~ /WKS/) printf "%s	 %s %s	 %s %s\n",$1,$2,$3,$4,$5}' /tmp/unk > /tmp/snewmod
		sladd $DATAS -f /tmp/snewmod
		echo $modfile >/tmp/check
		bip1=`cut -d. -f1 /tmp/check`
		bip2=`cut -d. -f2 /tmp/check`
		bip3=`cut -d. -f3 /tmp/check`
		bip4=`cut -d. -f4 /tmp/check`
		er=`cut -c1 /tmp/check`
		if [[ $er == 1 || $er == 2 || $er == 3 || $er == 4 || $er == 5 || $er == 6 || $er == 7 || $er == 8 || $er == 9 ]]
		then
			sv $DATAIPB1 string:$bip1
			sv $DATAIPB2 string:$bip2
			sv $DATAIPB3 string:$bip3
			sv $DATAIPB4 string:$bip4
		fi
		pu $BOX4 GrabExclusive
	fi
fi
}

accitm(){
nopass=
gv $DATANAM string:snam
gv $DATAIP1 string:st1
gv $DATAIP2 string:st2
gv $DATAIP3 string:st3
gv $DATAIP4 string:st4
gv $RR string:rr
gv $MXP string:mxp
if [[ $rbutton == "A" || $rbutton == "WKS" ]]
then
	if [[ $st1 == "" || $st2 == "" || $st3 == "" || $st4 == "" ]]
	then
		sv $WBOX_TA string:"IP address is not complete!\nPlease enter a valid IP address\nand try again."
		pu $WBOX
	else
		if [[ $rbutton == "A" ]]
		then
			echo $snam'	 IN A	 '$st1.$st2.$st3.$st4 >> /tmp/newmod
			nopass=y
		fi
		if [[ $rbutton == "WKS" ]]
		then
			echo $snam' 	IN WKS 	'$st1.$st2.$st3.$st4 $rr >> /tmp/newmod
			nopass=y
		fi	
	fi
fi
if [[ $rbutton != "A" ]]
then
	if [[ $rr == "" ]]
	then
		sv $WBOX_TA string:"Resource Record is not complete!\nPlease enter a valid Resource Record."
		pu $WBOX
	else
		if [[ $rbutton == "NS" ]]
		then
			gv $DATA1 string:domain7
			cp /tmp/newmod /tmp/newmodtp
			echo   '  	 IN NS	 '$rr. > /tmp/newmod
			cat /tmp/newmodtp >> /tmp/newmod
			nopass='y'
		fi
		if [[ $rbutton == "HINFO" ]]
		then
			echo $snam' 	IN HINFO 	'$rr >> /tmp/newmod
			nopass=y
		fi
		if [[ $rbutton == "CNAME" ]]
		then
			echo $snam' 	IN CNAME 	'$rr >> /tmp/newmod
			nopass=y
		fi
		if [[ $rbutton == "MX" ]]
		then
			gv $DOMNAME string:dname
			if [[ $dname == "" ]]
			then
				sv $WBOX_TA string:"Enter a Domain name\nTry again."
				pu $WBOX
			else
				if [[ $mxp == "" ]]
				then
					sv $WBOX_TA string:"Enter an MX priortiy\nTry again."
					pu $WBOX
				else
					echo $snam$dname.' 	IN MX '$mxp' 	'$rr. >> /tmp/newmod
				fi
				nopass=y
			fi
		fi
	fi
fi
if [[ $nopass == "y" ]]
then
	sv $DATANAM string:""
	sv $DATAIP3 string:""
	sv $DATAIP4 string:""
	sv $RR string:""
	sv $MXP string:""
	slnoupdate $DATAG
	sldel $DATAG all
	sladd $DATAG -f /tmp/newmod
	slupdate $DATAG
fi
}

accitmc(){
gv $DATANAMC string:snam
gv $DATAIPC1 string:st1
gv $DATAIPC2 string:st2
gv $DATAIPC3 string:st3
gv $DATAIPC4 string:st4
if [[ $snam == "" || $st1 == "" || $st2 == "" || $st3 == "" || $st4 == "" ]]
then
	sv $WBOX_TA string:"Provider information is not complete."
	pu $WBOX
else
	echo ' 	99999999 IN NS '$snam. >/tmp/p1
	echo $snam.' 		99999999 IN A '$st1.$st2.$st3.$st4 >/tmp/p2
	sv $DATANAMC string:""
	sv $DATAIPC1 string:""
	sv $DATAIPC2 string:""
	sv $DATAIPC3 string:""
	sv $DATAIPC4 string:""
	sv $RR string:""
	slnoupdate $DATAC
	sldel $DATAC all
	sladd $DATAC -f /tmp/p1
	sladd $DATAC -f /tmp/p2
	slupdate $DATAC
fi
}

resetit(){
PROCID=`ps -e | grep in.named | awk '{print $1}'`
if [ "$PROCID" ] ; then
	kill $PROCID
fi
/usr/sbin/in.named
}

warnit(){
cps WBOX Attention noticeShell $TOPLEVEL \
	noticeType:WARNING
sv $WBOX_TA string:$info"\nPress 'OK' to continue."
cmw NAMEBUTT buttons flatButtons $WBOX_CA \
        itemFields:{label} items:{OK}
sv $NAMEBUTT selectProc:closeinfo
}

closeinfo(){
pd $WBOX
}

acceptit() {
cut -d' ' -f2 /tmp/list >/tmp/itshere
chksec=n
gv $DATA1 string:domainf
gv $DATA3 string:postf
cps NBOX Attention noticeShell $TOPLEVEL \
	noticeType:WARNING
sv $NBOX_TA string:"This may take a few minutes,\nPress 'OK' to continue."
cmw NAMEBUTT buttons flatButtons $NBOX_CA \
        itemFields:{label} items:{OK}
sv $NAMEBUTT selectProc:doit
XtPopup $NBOX GrabExclusive
}

doit(){
### MAKING NAMED.LOCAL
gv $DATA1 string:domainf
gv $DATA2 string:ndirn
echo '@		IN SOA '$domainf.'	'$postf '(' > $dirn/named.local
echo '		'$DTE' ;Serial' >>/$ndirn/named.local
echo '		10800 ;Refresh' >>/$ndirn/named.local
echo '		3600 ;Retry' >>/$ndirn/named.local
echo '		432000 ; Expire' >>/$ndirn/named.local
echo '		86400) ;Minimum' >>/$ndirn/named.local
echo '		IN NS' $me.$domainf. >>$ndirn/named.local
echo 1.0 IN PTR localhost. >>$ndirn/named.local
### MAKING NAMED.BOOT ###
echo '; ns setup' >/etc/inet/named.boot
echo ';type  domain/source  file or host' >> /etc/inet/named.boot
echo ';' >>/etc/inet/named.boot
echo directory $ndirn >>/etc/inet/named.boot
echo '	' >>/etc/inet/named.boot
echo cache . named.ca >>/etc/inet/named.boot
echo '	' >>/etc/inet/named.boot
cat /tmp/list >>/etc/inet/named.boot
echo primary 0.127.in-addr.arpa named.local >>/etc/inet/named.boot
### MAKING RESOLV.CONF ###
if [[ -f /etc/resolv.conf ]]
then
	cp /etc/resolv.conf /etc/resolv.conf.old
fi
echo domain $domainf >/etc/resolv.conf
echo nameserver 127.0.0.1 >>/etc/resolv.conf
#echo nameserver $ipadd >>/etc/resolv.conf
#### MAKING REVERSE FILES ###
rm -f $dirn/*.rev
rm -f $dirn/*.srev
touch /tmp/four
tlist=`awk '{if ($3!="**") printf "%s=%s=%s=%s\n",$1,$2,$3,$4}' /tmp/list`
for itfile in $tlist
do
	echo $itfile >/tmp/list5
	ftype=`cut -d= -f1 /tmp/list5`
	dname=`cut -d= -f2 /tmp/list5`
	if [[ $ftype == "secondary" ]]
	then
		if [[ $chksec != "y" ]]
		then
			resetit
			echo "waiting 10 seconds for secondary name server to get database from primary name server"
			sleep 10
			chksec=y
		fi
		file=`nawk -F= '{printf "%s\n",$NF}' /tmp/list5`
		cut -d= -f3 /tmp/list5 >/tmp/sip2
		a=`cut -d. -f1 /tmp/sip2`
		b=`cut -d. -f2 /tmp/sip2`
		c=`cut -d. -f3 /tmp/sip2`
		d=`cut -d. -f4 /tmp/sip2`
		sip3=`cat /tmp/sip2`
		echo secondary $b.$a.in-addr.arpa $sip3 $a.$b.srev >/tmp/one
		now=`cat -s /tmp/one`
		ten=`cut -d' ' -f4 /tmp/one`
		eleven=`fgrep -c $ten /tmp/four`
		if [[ $eleven == "0" ]]
		then 
			echo $now>>/tmp/four
		fi
	else
		file=`cut -d= -f3 /tmp/list5`
	fi
	awk '{if ($3=="A") printf "%s.%s\n",$1,$4}' $dirn/$file >/tmp/tfile1
	nlist=`cat -s /tmp/tfile1`
	for nitm in $nlist
	do
		if [[ $ftype == "primary" ]]
		then
			echo $nitm
			echo $nitm >/tmp/one
			nawk -F. '{if ($5!="") printf "%s.%s	IN PTR	%s.'$dname.'\n",$5,$4,$1}' /tmp/one > /tmp/two
			THS=`nawk -F. '{if ($5!="") printf "%s.%s.rev\n",$2,$3}' /tmp/one`
			if [[ ! -f $dirn/$THS ]]
			then
					gv $DATA1 string:domainf
					echo '@		IN SOA '$domainf.'	'$postf '(' > $dirn/$THS
					echo '		'$DTE' ;Serial' >>/$dirn/$THS
					echo '		10800 ;Refresh' >>/$dirn/$THS
					echo '		3600 ;Retry' >>/$dirn/$THS
					echo '		432000 ; Expire' >>/$dirn/$THS
					echo '		86400) ;Minimum' >>/$dirn/$THS
			fi
			five=`fgrep -c $THS /tmp/four`
			if [[ $five == "0" ]]
			then
				nawk -F. '{if ($5!="") printf "primary %s.%s.in-addr.arpa	%s.%s.rev\n",$3,$2,$2,$3}' /tmp/one >/tmp/three
				cat -s /tmp/three >>/tmp/four
			fi
			cat /tmp/two >>$dirn/$THS
		else
			echo $nitm
			echo $nitm >/tmp/one
			six=`nawk -F. '{if ($5!="") printf "%s.%s.%s.srev",$2,$3,$4}' /tmp/one`
			five=`fgrep -c $six /tmp/four`
			if [[ $five == "0" ]]
			then
				nawk -F. '{if ($5!="") printf "secondary %s.%s.%s.in-addr.arpa '$sip3' %s.%s.%s.srev\n",$4,$3,$2,$2,$3,$4}' /tmp/one >/tmp/three
				cat -s /tmp/three >>/tmp/four
			fi
		fi
	done
done
cat /tmp/four >>/etc/inet/named.boot
##### move data if different directory
if [[ $dirn != $ndirn ]]
then
	if [[ ! -d $ndirn ]]
	then
		mkdir $ndirn
	fi
	cat -s /tmp/list > /tmp/dirs
	cat -s /tmp/four >> /tmp/dirs
	glist=`awk '{if ($3!="**") printf "%s\n",$NF}' /tmp/dirs`
	for gfile in $glist
	do
		mv $dirn/$gfile $ndirn/$gfile
	done
	mv $dirn/named.ca $ndirn/named.ca
fi
resetit
### clean up
rm -f /tmp/four
rm -f /tmp/check2
rm -f /tmp/itshere
rm -f /tmp/list*
rm -f /tmp/one
rm -f /tmp/sip2
rm -f /tmp/tfile1
rm -f /tmp/three
rm -f /tmp/two
rm -f /tmp/dirs
exit
}
#################### Main #######################

oi TOPLEVEL dns-setup "dns-setup" $OPTIONS
checkroot
if [[ $checkrt == "no" ]]
then
	XBell
else
	main
	poptemplate
	poptemplate1
	poptemplate2
	poptemplate3
	warnit
fi
rw $TOPLEVEL
ml
