#!/bin/sh
samba_user="jin"
user_passwd="jin"

export LANG=en_US.UTF-8

host_name=`hostname | grep x64_server`
if [[ "${host_name}" = "" ]]
then
	echo "==service iptables stop===================="
	setenforce 0
	service iptables stop
fi

lanman_setting="\\
\\
	   encrypt passwords = yes\\
\\
        unix extensions = no\\
        map to guest = Bad User\\
\\
	   guest account = guest\\
        lanman auth = yes\\
        client lanman auth = yes\\
"
share_setting="\\
[share]\\
        comment = share all\\
        path = /tmp/samba\\
        browseable = yes\\
        public = yes\\
        writable = yes\\
	\\
[myshare]\\
        comment = share for users\\
        path = /home/jin/share\\
        browseable = yes\\
        writable = yes\\
        public = no\\
        valid users = @jin\\
"
disk_setting="\\
[win_disk]\\
        comment = share all\\
        path = /mnt/DISK1/SYSTEM\\
        browseable = yes\\
        public = yes\\
        writable = yes\\
"

sys_setting="\\
[SYS]\\
        comment = SYS\\
        path = /mnt/DISK1/SYSTEM/sys\\
        guest ok = yes\\
        browseable = yes\\
        public = yes\\
        writable = no\\
"

loacal_setting="\\
[media_local]\\
        comment = local\\
        path = /media/local\\
        browseable = yes\\
        public = yes\\
        writable = yes\\
"

tftpboot_setting="\\
[tftpboot]\\
        comment = tftpboot\\
        path = /tftpboot\\
        browseable = yes\\
        public = yes\\
        writable = yes\\
"


###os_setting="\\
###[winxpcn_sp3]\\
###        comment = windowsXP CN SP3\\
###        path = /share/DISK1/SYSTEM/winxpcn_sp3\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###[winxpjp_sp3]\\
###        comment = windowsXP JP SP3\\
###        path = /share/DISK1/SYSTEM/winxpjp_sp3\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###[win2kcnpro_sp4]\\
###        comment = windows2k CN SP4\\
###        path = /share/DISK1/SYSTEM/win2kcnpro_sp4\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###[win2kjppro_sp4]\\
###        comment = windows2k JP SP4\\
###        path = /share/DISK1/SYSTEM/win2kjppro_sp4\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###[win98cn]\\
###        comment = windows98 cn\\
###        path = /share/DISK1/SYSTEM/win98cn\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###[win98en]\\
###        comment = windows98SE en\\
###        path = /share/DISK1/SYSTEM/win98SE_en\\
###        browseable = yes\\
###        public = yes\\
###        writable = no\\
###"

##user
useradd guest

cp -p  ./smb.conf   /etc/samba/smb.conf

## add samba setting
samba_setting=`cat /etc/samba/smb.conf | grep "\[myshare\]"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add samba setting===================="

	sed -i "s/^;*\([[:space:]]\+workgroup[[:space:]]*=\).*$/\1 407/g"  /etc/samba/smb.conf
	sed -i "s/^[^[:space:]]\+\([[:space:]]*netbios[[:space:]]\+name[[:space:]]*=\).*$/\1 raspberrypi/g"  /etc/samba/smb.conf
	sed -i "s/^[^[:space:]]\+\([[:space:]]*hosts[[:space:]]\+allow[[:space:]]*=\).*$/\1 127. 192.168.1. /g"  /etc/samba/smb.conf
	sed -i "s/^[^[:space:]]\+\([[:space:]]*bind[[:space:]]\+interfaces[[:space:]]\+only[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	sed -i "s/^[^[:space:]]\+\([[:space:]]*interfaces[[:space:]]*=\).*$/\1 lo/g"  /etc/samba/smb.conf

	sed -i "s/^\([[:space:]]\+security[[:space:]]*=\).*$/\1 user/g"  /etc/samba/smb.conf

	sed -i "s/^;*\([[:space:]]\+domain[[:space:]]\+master[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	sed -i "s/^;*\([[:space:]]\+local[[:space:]]\+master[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	sed -i "s/^;*\([[:space:]]\+os[[:space:]]\+level[[:space:]]*=\).*$/\1 33/g"  /etc/samba/smb.conf
	sed -i "s/^;*\([[:space:]]\+preferred[[:space:]]\+master[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	sed -i "s/^;*\([[:space:]]\+wins[[:space:]]\+support[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	sed -i "s/^;*\([[:space:]]\+dns[[:space:]]\+proxy[[:space:]]*=\).*$/\1 yes/g"  /etc/samba/smb.conf
	
	

	inset_line=`grep -n "\[printers\]" /etc/samba/smb.conf  |  tail -1 | cut -f 1  -d:`
	let inset_line=inset_line+7
	sed -i "${inset_line}i${share_setting}"   /etc/samba/smb.conf
fi

samba_setting=`cat /etc/samba/smb.conf | grep "lanman"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add lanman setting===================="

	inset_line=`grep -En "^[[:space:]]*security" /etc/samba/smb.conf | cut -f 1  -d:`
	sed -i "${inset_line}i${lanman_setting}"   /etc/samba/smb.conf
fi

samba_setting=`cat /etc/samba/smb.conf | grep "\[win_disk\]"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add win_disk setting===================="

	inset_line=`grep -n "\[myshare\]" /etc/samba/smb.conf  |  tail -1 | cut -f 1  -d:`
	let inset_line=inset_line+7
	sed -i "${inset_line}i${disk_setting}"   /etc/samba/smb.conf
	
	if [ ! -d /mnt/DISK0 ];then
		mkdir -p /mnt/DISK0
	fi
	if [ ! -d /mnt/DISK1 ];then
		mkdir -p /mnt/DISK1
	fi
	if [ ! -d /mnt/DISK1/SYSTEM ];then
		disk1_exist=`cat  /etc/fstab  | grep "mnt\/DISK1"`
		if [[ "$disk1_exist" = "" ]]
		then
			mkdir -p /media/netdir
			#mount /dev/mmcblk0p3 /mnt/DISK1
			echo  "#/dev/mmcblk0p3       /mnt/DISK0      ext4    rw      0       0"  >> /etc/fstab
			echo  "#/dev/mmcblk0p4       /mnt/DISK1      ext4    rw      0       0" >> /etc/fstab
			echo  "##nas-27-59-FD:/data/tmp/raspbrreyPi/mnt_DISK1       /mnt/DISK1      nfs    rsize=8192,wsize=8192,timeo=14,_netdev 0 0" >> /etc/fstab
			echo  "##nas-48-71-3B:/data/dev/raspbrreyPi/mnt/DISK2       /mnt/DISK1      nfs    rsize=8192,wsize=8192,timeo=14,_netdev 0 0" >> /etc/fstab
			echo  "nas-27-59-FD:/data/dev/raspbrreyPi/mnt/DISK2    /mnt/DISK1     nfs    nolock,nofail,x-systemd.automount,x-systemd.requires=network-online.target,x-systemd.device-timeout=10 0 0" >> /etc/fstab
			#  echo  "nas-27-59-FD:/data/tmp/raspbrreyPi/mnt_tools       /mnt/tools      nfs    rsize=8192,wsize=8192,timeo=14,_netdev 0 0" >> /etc/fstab
			#  echo  "nas-27-59-FD:/data/tmp/       /media/netdir      nfs    rsize=8192,wsize=8192,timeo=14,_netdev 0 0" >> /etc/fstab
			#  echo  "nas-48-71-3B:/data/Repository      /mnt/repo      nfs    rsize=8192,wsize=8192,timeo=14,_netdev 0 0" >> /etc/fstab
		fi
	fi
fi

samba_setting=`cat /etc/samba/smb.conf | grep "\[SYS\]"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add os source setting===================="

	inset_line=`grep -n "\[win_disk\]" /etc/samba/smb.conf  |  tail -1 | cut -f 1  -d:`
	let inset_line=inset_line+6
	sed -i "${inset_line}i${sys_setting}"   /etc/samba/smb.conf
fi

samba_setting=`cat /etc/samba/smb.conf | grep "\[media_local\]"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add media_local setting===================="

	inset_line=`grep -n "\[SYS\]" /etc/samba/smb.conf  |  tail -1 | cut -f 1  -d:`
	let inset_line=inset_line+7
	sed -i "${inset_line}i${loacal_setting}"   /etc/samba/smb.conf
fi

samba_setting=`cat /etc/samba/smb.conf | grep "\[tftpboot\]"`
if [[ "${samba_setting}" = "" ]]
then
	echo "==add tftpboot setting===================="

	inset_line=`grep -n "\[media_local\]" /etc/samba/smb.conf  |  tail -1 | cut -f 1  -d:`
	let inset_line=inset_line+6
	sed -i "${inset_line}i${tftpboot_setting}"   /etc/samba/smb.conf
fi



#chmod -R 777 /mnt/DISK1/SYSTEM

sudo service smbd restart

#/etc/init.d/nmb restart
#/etc/init.d/smb restart

