# {{ ansible_managed }}

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
   . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# this is for the midnight-commander
# to become the last directory the midnight commander was in
# as the current directory when leaving the midnight commander
#
#. /usr/lib/mc/bin/mc.sh
#
if [ -f "/usr/share/mc/bin/mc.sh" ] ; then
   source /usr/share/mc/bin/mc.sh
fi

export LANG="de_DE.utf8"

# ---
# Mmount samba shares
# ---

# Don't try to mount samba shares if login at samba server
# 
[[ "$(hostname --long)" = "{{ samba_server }}" ]] && return

SERVER="{{ samba_server }}"
USER="{{ item.item.name }}"
PASSWORD='{{ item.item.password }}'
#VERSION="1.0"

# Use NTLMv2 password hashing and force packet signing
#
#    SEC="ntlmv2i"
#
# Use NTLMv2 password hashing encapsulated in Raw NTLMSSP message, and force packet signing
#
#    SEC="ntlmsspi"
#
SEC="ntlmsspi"

# - uid/guid of the user at fielserver
# -
_UID="$(id -u)"
_GID="$(id -g)"


# Logfile to see what happened..
#
_logfile=/tmp/profile_${USER}.log


echo "" > $_logfile
echo "$(date +"%Y-%m-%d-%H%M")" >> $_logfile

# Network present
#
_network=false

if [  "X$_addr" = "X" ] ; then
   echo "no inet address assigned yet.." >> $_logfile
   declare -i count=1
   while  ! $_network && [[ $count -lt 5 ]] ; do
      echo "sleeping 2 seconds.." >> $_logfile
      sleep 2
      _addr="$(hostname --ip-address)"
      if [ "X$_addr" != "X" ] ; then
         _network=true
         echo "inet address present: $_addr" >> $_logfile
      fi
      ((count++))
   done
fi

for dir in $(ls /mnt/$USER) ; do
   MOUNT_POINT=/mnt/$USER/$dir
   SHARE=$dir

   [ ! -d $MOUNT_POINT ] && continue

   if ! mount | grep $MOUNT_POINT > /dev/null ; then
      echo "Going to mount share '${SHARE}' .." >> $_logfile
      if [ -x /usr/bin/smb4k_mount ]; then
         ## - Ubuntu <= 12.04
         if [[ "$VERSION" = "1.0" ]]; then
            sudo /usr/bin/smb4k_mount -o user=$USER,password=$PASSWORD,iocharset=utf8,vers=1.0 \
               -n -t cifs //$SERVER/$SHARE $MOUNT_POINT >> $_logfile 2>&1
         else
            #sudo /usr/bin/smb4k_mount -o user=$USER,password=$PASSWORD,iocharset=utf8,uid=$_UID,gid=$_GID,vers=$VERSION \
            sudo /usr/bin/smb4k_mount -o user=$USER,password=$PASSWORD,iocharset=utf8,uid=$_UID,gid=$_GID \
               -n -t cifs //$SERVER/$SHARE $MOUNT_POINT >> $_logfile 2>&1
         fi
      else
         ## - Ubuntu Version >= 14.04
         if [[ "$VERSION" = "1.0" ]]; then
            sudo /bin/mount -o user=$USER,password=$PASSWORD,iocharset=utf8,cifsacl,vers=1.0 \
               -n -t cifs //$SERVER/$SHARE $MOUNT_POINT >> $_logfile 2>&1
         else
            #sudo /bin/mount -o user=$USER,password=$PASSWORD,iocharset=utf8,cifsacl,uid=$USER,sec=${SEC},vers=$VERSION \
            sudo /bin/mount -o user=$USER,password=$PASSWORD,iocharset=utf8,uid=$USER,gid=$_GID \
               -n -t cifs //$SERVER/$SHARE $MOUNT_POINT >> $_logfile 2>&1
         fi
      fi
   else
      echo "mount point $MOUNT_POINT already exists. nothing left to do.." >> $_logfile
   fi

done

