#!/bin/sh
#
# $Id: /user/syscfg/StorageAdaptor.sh $
#
# usage: StorageAdaptor.sh as an adaptor course will be execution via specfied files <DlnaApp sambaApp ftpdApps>
#
#
#PART1=`mount | grep "/media/" | sed 's/^.*media/\/media/g' | sed 's/ type.*$//g' | sed -n '1p'`
PART1="/mnt"
PART2=`mount | grep "/mnt/" | sed 's/^.*mnt/\/mnt/g' | sed 's/ type.*$//g' | sed -n '1p'`
echo "PART1:"$PART1""
CONFIG_PATH=/var
EXEC_PATH=/etc
# TR098 access path
TR098_STORAGE_PATH_WEB=InternetGatewayDevice.X_CT-COM_UserInfo.UserList
TR098_SAMBA_PATH=InternetGatewayDevice.Services.ServiceSamba
TR098_DLNA_PATH=InternetGatewayDevice.Services.ServiceDLNA
TR098_STORAGE_PATH=InternetGatewayDevice.Services.ServiceStorage.Account

###########################################################################
SambaInitialization()
{
	echo "SambaInitialization"
	smbnetbios=`cfgcmd get $TR098_SAMBA_PATH.NetBIOSName`
	smbwg=`cfgcmd get $TR098_SAMBA_PATH.NetWorkGroup`
	echo "samba.sh "$smbnetbios" "$smbwg" "$PART2""
	samba.sh "$smbnetbios" "$smbwg" "$PART2"

	Level=`cfgcmd get $TR098_SAMBA_PATH.Level`
	if [ "$Level" = "0" ];then #allow web user access
	local accounts=`cfgcmd get_idxes $TR098_STORAGE_PATH_WEB`
	if [ "$accounts" != "0" ]; then
		local public_user_all=""
		for a in $accounts ; do
			local user=`cfgcmd get $TR098_STORAGE_PATH_WEB.$a.UserName`
			local passwd=`cfgcmd get $TR098_STORAGE_PATH_WEB.$a.Password_realtext`
			echo "smbpasswd -a "$user" "$passwd""
			smbpasswd -a "$user" "$passwd"
			public_user_all=${user}" "${public_user_all}
		done
		echo "samba_add_dir.sh Public "$PART2" "$public_user_all" "yes""
		samba_add_dir.sh Public "$PART2" "$public_user_all" "yes"
	fi
    elif [ "$Level" = "2" ];then # allow create user access
	local accounts_stor=`cfgcmd get_idxes $TR098_STORAGE_PATH`
	if [ "$accounts_stor" != "0" ]; then
		for a in $accounts_stor ; do
			local stor_user=`cfgcmd get $TR098_STORAGE_PATH.$a.Username`
			local stor_passwd=`cfgcmd get $TR098_STORAGE_PATH.$a.Password`
			local stor_path=`cfgcmd get $TR098_STORAGE_PATH.$a.AccessPath`
			local stor_mask=`cfgcmd get $TR098_STORAGE_PATH.$a.WR-Mask`
			if [ "$stor_mask" = "1" ];then
			stor_mask=yes
			else
			stor_mask=no
			fi
			echo "smbpasswd -a "$stor_user" "$stor_passwd""
			smbpasswd -a "$stor_user" "$stor_passwd"
			if [ -n "$stor_path" ];then
			echo "samba_add_dir.sh Private_$stor_user "$stor_path" "$stor_user" "$stor_mask""
	        samba_add_dir.sh Private_$stor_user "$stor_path" "$stor_user" "$stor_mask"
	        else
	        echo "samba_add_dir.sh Private_$stor_user "$PART2" "$stor_user" "$stor_mask""
	        samba_add_dir.sh Private_$stor_user "$PART2" "$stor_user" "$stor_mask"
	        fi
		done
	fi
	else  # allow all user access
	local accounts=`cfgcmd get_idxes $TR098_STORAGE_PATH_WEB`
	if [ "$accounts" != "0" ]; then
	    local public_user_all=""
		for a in $accounts ; do
			local user=`cfgcmd get $TR098_STORAGE_PATH_WEB.$a.UserName`
			local passwd=`cfgcmd get $TR098_STORAGE_PATH_WEB.$a.Password_realtext`
			echo "smbpasswd -a "$user" "$passwd""
			smbpasswd -a "$user" "$passwd"
			public_user_all=${user}" "${public_user_all}
		done
		echo "samba_add_dir.sh Public "$PART2" "$public_user_all" "yes""
		samba_add_dir.sh Public "$PART2" "$public_user_all" "yes"
	fi
	local accounts_stor=`cfgcmd get_idxes $TR098_STORAGE_PATH`
	if [ "$accounts_stor" != "0" ]; then
		for a in $accounts_stor ; do
			local stor_user=`cfgcmd get $TR098_STORAGE_PATH.$a.Username`
			local stor_passwd=`cfgcmd get $TR098_STORAGE_PATH.$a.Password`
			local stor_path=`cfgcmd get $TR098_STORAGE_PATH.$a.AccessPath`
			local stor_mask=`cfgcmd get $TR098_STORAGE_PATH.$a.WR-Mask`
			if [ "$stor_mask" = "1" ];then
			stor_mask=yes
			else
			stor_mask=no
			fi
			echo "smbpasswd -a "$stor_user" "$stor_passwd""
			smbpasswd -a "$stor_user" "$stor_passwd"
			if [ -n "$stor_path" ];then
			echo "samba_add_dir.sh Private_$stor_user "$stor_path" "$stor_user" "$stor_mask""
	        samba_add_dir.sh Private_$stor_user "$stor_path" "$stor_user" "$stor_mask"
	        else
	        echo "samba_add_dir.sh Private_$stor_user "$PART2" "$stor_user" "$stor_mask""
	        samba_add_dir.sh Private_$stor_user "$PART2" "$stor_user" "$stor_mask"
	        fi
		done
	fi
	fi
}

###########################################################################
# Setting storage for user access account : samba ftp dlna
case "$1" in
	"samba")
		killall -q smbd
		killall -q nmbd
		if [ ! -e "$PART2" ]; then
			echo "1st usb partition does not exist"
			cfgcmd sset $TR098_SAMBA_PATH.Enable 0
			exit 0
		fi
		smbenable=`cfgcmd get $TR098_SAMBA_PATH.Enable`
		if [ "$smbenable" == "1" ]; then
			touch /var/smbpasswd
			SambaInitialization
			nmbd -s /var/smb.conf
			smbd -s /var/smb.conf
		fi
		;;
	"dlna")
		DlnaEnable=`cfgcmd get $TR098_DLNA_PATH.Enable`
		if [ "$DlnaEnable" == "1" ]; then
			killall -q minidlna
			SerialNumber=`cfgcmd get "InternetGatewayDevice.DeviceInfo.SerialNumber"`
			MediaDir=`cfgcmd get $TR098_DLNA_PATH.MediaDIR`
			echo "DlnaEnable:"$DlnaEnable""
			DlnaName=`cfgcmd get "InternetGatewayDevice.DeviceInfo.ModelName"`
			echo "minidlnaconf.sh minidlna br0 "${PART1}/${MediaDir}" "$DlnaName" "$SerialNumber""
			minidlnaconf.sh minidlna br0 "${PART1}/${MediaDir}" "$DlnaName" "$SerialNumber"
			echo "minidlna -f $CONFIG_PATH/minidlna.conf"
			sleep 1
			minidlna -R -f $CONFIG_PATH/minidlna.conf
		fi
		;;
esac
