#!/bin/bash
DIR_USB=/media/usb_drive/
DIR_OPENSLX=/Openslx/
user_name=`users |  cut -f 1 -d ' '`

# prepares openslx partition to be used
# mounts partition if not mounted
# blocks program execution if it is not a openslx partition
# param $1 is the devpath to be mounted
# param $2 is the uuid of the partition
# return is done via the mountpath global variable, which is also used here
function prepare_usb {
    # get the usb_drive's path
    local devpath="/dev/"$1
    mountpath=""
    if [ ! 0 -eq $(mount | grep -q $devpath) ]; then
        echo $0": Device $devpath is already mounted"
        mountpath=$(mount | grep -w $devpath | cut -d' ' -f3)
    else
        echo $0": Device $devpath is not mounted"
        mkdir -p /media/openslx/$2
        if $(mount $devpath /media/openslx/$2); then
            echo $0": Device $devpath mounted successfully"
            mountpath="/media/openslx/"$2
        else
            echo $0": Device $devpath could not be mounted"
            exit 1
        fi
    fi
    if [ -e "$mountpath/Openslx/config" ]; then
        echo $0": $devpath is an openslx ready drive"
    else
        echo $0": $devpath is not ready to be used as an openslx drive"
        if [ $mountpath = "/media/openslx/$2" ]; then
            echo $0": umounting $devpath"
            umount $devpath
        fi
        exit 2
    fi
}

sleep 3
#TIRAR FOR
# list all the usb_drives, mounted or not mounted
usb_drives=`ls -lt 2>/dev/null /dev/disk/by-id/usb* | rev |  cut -f 3 -d ' ' | rev | head -1`

    prepare_usb
    echo $mount_path
    counter=0
    for user_info in `eval cat "$mount_path"/Openslx/config`; do
      type=`eval echo $user_info | cut -f 1 -d '='`
      counter=$(($counter+1))
      if [[ $type == "SLX_USB_PASSWD" ]]
         then
         passwd=`eval echo $user_info | cut -f 2 -d '='`
         user=` eval echo $user_info  | cut -f 2 -d '=' | cut -f 1 -d ':'`
         gshadow=`echo $user:!::`
         echo "user"$user
         check_user=`cat /etc/shadow | grep -w $user`
         passwd=`eval echo $user_info | cut -f 2 -d '='`
         if [[ ( ! -n $check_user) ]]; then
            echo "não existe user"
         fi

      elif [[ $type == "SLX_USB_SHADOW" ]]
        then

        shadow=`eval echo $user_info | cut -f 2 -d '='`

      elif [[ $type == "SLX_USB_GROUP" ]]
        then
        group=`eval echo $user_info | cut -f 2 -d '='`

      fi

      mod=$(( $counter % 3))
      if [[ $mod == 0 ]];
        then
        #insert data in the files
        if [[ (! -n $check_user)&&(-n $passwd)&&(-n $shadow)&&(-n $group)&&(-n $gshadow) ]]; then
          echo "Insert $counter"
          echo $passwd >> /etc/passwd
          echo $shadow >> /etc/shadow
          echo $group >> /etc/group
          #echo $gshadow >> /etc/gshadow
          if [ ! -d "/home/"$user ];  then
            mkdir /home/$user
            chown $user:$user /home/$user
          fi
        fi
        passwd=""
        shadow=""
        group=""
        gshadow=""
        user=""
      fi
    done



echo $shadow
echo $group

#user=` eval cat "$mount_path"/Openslx/config | head -n 1 | cut -f 2 -d '=' | cut -f 1 -d ':'`
#gshadow=`echo $user:!::`
#check_user=`cat /etc/shadow | grep -w $user`
#echo $shadow
#echo $group
#echo $check_user
