#!/bin/bash

set -e

samba_share_printer='/media/samba_share_printer'
samba_share_aux='/media/samba_share_aux'
model=$(cat /home/nimbus/release.txt | awk -F "_" '{print $2}' | sed 's/BM/027MB0/g')E1
model=${model:0:9}${model:10}
date=$(date +"%Y-%m-%d")
of=$1
mac=$2
name_file=$(echo ${mac//:})
name_file=$(echo "${name_file^^}.txt")
content="NOMBRE-ETIQUETA=DY798A04\nNUMERO-ETIQUETAS=1\nCODI_1=${model}\nN_FABRICACIO_1=$of\nDATA_1=$date\nMAC_1=\nMAC_V1=$mac\nMAC_VAL=GradiLux Code\nMAC_VVAL=O21E32A71O15X\nMAC_VVALL=3543\nMAC_SENSE=0019CF000DD7\n"

# --- Samba directories exist? If not, create them. ---
if [ ! -d $samba_share_printer ]; then
    mkdir $samba_share_printer
fi

if [ ! -d $samba_share_aux ]; then
    mkdir $samba_share_aux
fi

# --- Has "fstab file" been configured? If not, configure it. ---
config_printer="//172.26.0.134/Output/SATO $samba_share_printer cifs username=nimbus,password=slcKubernete5,domain=slc,vers=2.0 0 0"
config_aux="//172.26.0.3/compartit/temp/Nimbus $samba_share_aux cifs username=nimbus,password=slcKubernete5,domain=slc,vers=2.0 0 0"

if ! grep -Fxq "$config_printer" /etc/fstab; then
    echo $config_printer >> /etc/fstab
fi

if ! grep -Fxq "$config_aux" /etc/fstab; then
    echo $config_aux >> /etc/fstab
fi

#--- Are samba directories mounted? If not, mount them. ---  
if ! mountpoint -q $samba_share_printer; then
    mount $samba_share_printer
fi

if ! mountpoint -q $samba_share_aux; then
    mount $samba_share_aux
fi 

# --- Checking if the file already exists. If not copy it. ---
if [ ! -f "$samba_share_printer/$name_file" ]; then
    echo -e $content > "$samba_share_printer/$name_file"
fi

if [ ! -f "$samba_share_aux/$name_file" ]; then
    echo -e $content > "$samba_share_aux/$name_file"
fi

# --- Unmount directories ---
umount $samba_share_printer $samba_share_aux