#!/bin/bash

# 询问用户要创建的共享文件夹路径
#read -p "请输入要创建的Samba共享文件夹路径(需要写绝对路径): " shared_folder
shared_folder=$HOME/桌面/samba
current_user=$USER

# 创建共享文件夹
sudo mkdir -p "$shared_folder"
sudo chmod -R 777 "$shared_folder"

# Check if Samba parameters are already configured
if grep -q "$shared_folder" /etc/samba/smb.conf; then
    echo "Samba已经配置了，请查看/etc/samba/smb.conf"
    exit 0
fi

# 安装并配置Samba服务
#sudo apt-get update
#sudo apt-get install -y samba
sudo cp /etc/samba/smb.conf   /etc/samba/smb.conf.bak   # 备份原始配置文件
sudo echo -e "[samba]\n  path = $shared_folder\n  browseable = yes\n  writable = yes\n  guest ok = no\n  create mask = 0777\n  directory mask = 0777\n  valid users = $current_user\n" | sudo tee -a /etc/samba/smb.conf

#创建Samba用户
sudo smbpasswd   -a  $current_user

sudo systemctl  restart  smbd
sudo systemctl  enable  smbd
sudo systemctl  restart  nmbd
sudo systemctl  enable  nmbd

echo "Samba共享文件夹已创建并服务已启动。"
