#!/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

ntfs="/mnt/ntfs"
logpath="/tmp/winprofile.log"
mkdir -p $ntfs
apt install ntfs-3g fuse -y

parts=$(blkid -o device | grep -v -e /sr0 -e loop)
for part in $parts; do
    #    part_name=$(basename "$part"
    mount_point="/mnt$part/"
    #Create folder for mounting
    mkdir -p "$mount_point"
    mount -o ro $part $mount_point
    test -f $mount_point/Windows/explorer.exe && windows=$mount_point && break
    umount $part $mount_point
    rm $mount_point
done

#############################################################################################################################
#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="\$windows/Users/\$user/"
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
