#!/bin/bash
#/usr/bin/firststartAlt.sh -s {share_url} -n {Username} -p {Password}
#/usr/bin/firststartAlt.sh -s "//10.10.243.33/Profiles" -n {Username} -p {Password}
#v1_010624

while getopts "d:s:n:p:" option; do
    case $option in    
    s) share_url=$OPTARG ;;
    n) username=$OPTARG ;;
    p) password=$OPTARG ;;
    \?) # Invalid option
        echo "Error: Invalid option"
        exit 1
        ;;
    esac
done

#macadr=$(cat /sys/class/net/*/address|head -n1|sed 's/://g')
source_folder="/DATAON_"
destination_folder="/mnt/windowsfiles"
tempfolder="/mnt/wincopy"
logpath="/tmp/winprofile.log"
compname=$(hostname | cut -d. -f1 | tr '[:upper:]' '[:lower:]')
echo "Start deploy on $compname" >>$logpath
mkdir -p -m 777 $tempfolder
mkdir $destination_folder

if [ -z "username" ];then
    options=guest,iocharset=utf8,file_mode=0777,dirmode=0777    
else 
    options=username="$username",password="$password",iocharset=utf8,file_mode=0777,dirmode=0777
fi
/bin/mount -t cifs "$share_url$source_folder$compname/Users" "$tempfolder" -o $options
echo "mounted $share_url$source_folder$compname" >>$logpath
# Проверка успешного подключения
if [ $? -eq 0 ]; then
    echo "Successfully connected to share"
else
    echo "Error. Is not able to connect to share"
    exit 1
fi

cp -av "$tempfolder/" "$destination_folder/"
echo "copied  $destination_folder" >>$logpath

if [ $? -eq 0 ]; then
    echo "Profile folder successfully copied"
    touch "$destination_folder"/success.txt
    errcd="0"
    echo "success" >>/tmp/frststart.log
else
    echo "failed copy of profile folder"
    errcd="1"
fi
for f in `find /mnt/windowsfiles/wincopy/ -maxdepth 1 -type d -regex '.*[A-Z]+.*'`; do mv -v "$f" "`echo $f | tr '[A-Z]' '[a-z]'`"; done
umount "$tempfolder"

if [ $? -eq 0 ]; then
    echo "Successfully unmounted from share"
else
    echo "unmount failed"
fi

#############################################################################################################################
#create service for each user loggin
cat <<EOL >/usr/local/bin/changeuserconf.sh
#!/bin/bash
user=\$(echo \$USER | cut -d'@' -f1 | tr A-Z a-z)
source_folder="/mnt/windowsfiles/wincopy/\$user/"
#desktop_folder="\$HOME/Desktop"
desktop_folder=\$(systemd-path user-desktop)
link_name="Данные пользователя"

if [ -d "\$source_folder" ]; then    
    echo "Folder \$source_folder for \$user is present."
    if [ -d "\$desktop_folder" ]; then
        ln -s "\$source_folder" "\$desktop_folder/\$link_name"
        echo "Link \$desktop_folder/\$link_name was created on desktop."
        systemctl --user disable changeuserconf.service
    else
        echo "Target folder \$desktop_folder for \$user is not present, link is not created."
        exit 1
    fi
else
    echo "Source folder \$source_folder for \$user doesn't exist, link is not created."
    systemctl --user disable changeuserconf.service
fi
EOL

chmod 774 /usr/local/bin/changeuserconf.sh
chmod +x /usr/local/bin/changeuserconf.sh
cat <<EOL >/etc/systemd/user/changeuserconf.service
[Unit]
Description=This service changes user conf files for apps like  Pidgin

[Service]
Type=simple
#Type=oneshot
ExecStart=/bin/bash -c "/usr/local/bin/changeuserconf.sh"
Restart=on-failure
RestartSec=20s

[Install]
WantedBy=default.target
EOL
# start service
systemctl --global enable changeuserconf.service || true
