#!/bin/bash

USER=$(whoami)
FILECONFIGOCS="/etc/ocsinventory-agent/ocsinventory-agent.cfg"
if ! [ -f "${FILECONFIGOCS}" ] ; then 
    yad --title "Modificar TAG OCS" \
    --image "server" \
    --info --text "<b>No existe</b> el archivo de configuración de OCS....\n!Prueba a reinstalar la aplicación!" \
    --window-icon vitalinux \
    --width="400" --height="100" --center --justify="center" --timeout=7 --text-align="center"
    exit 1
fi
if [ $USER = "root" ]; then
    
    TAG=$(crudini --get "${FILECONFIGOCS}" '' "tag")
  
    TEXTO="\tLa TAG actual del equipo es: \"<b>$TAG</b>\" \n\t¿Quieres Modificarla?"
    
    VALIDO=0
    while test $VALIDO -eq 0 ; do
        RESPUESTA=$(yad --center --width 640 \
            --window-icon vitalinux \
            --image server \
            --title "Modificar TAG OCS" \
            --text-align center --justify="center" \
            --text "${TEXTO}" \
            --form \
            --field="TAG a usar: " "$TAG" \
            --button="Modificar TAG":0 \
            --button="Mantener anterior":1 \
        --buttons-layout center)
        if [[ ! $? -eq 0 ]] ; then
            exit 0
        fi
        TAG=$(echo $RESPUESTA | cut -d"|" -f1)
        # Comprobamos que la tag es correcta
        if [ -z ${TAG} ] || ! [[ $TAG =~ ^[0-9]*$ ]]; then
            TEXTO="\n\t¡¡Repasa la TAG \"<b>${TAG}</b>\" Indicada!! \n\tVuelve a introducirla correctamente, por favor ..."
        else
            if yad --center --width 400 \
            --window-icon vitalinux \
            --image server \
            --title "Modificar TAG OCS" \
            --text-align center --justify="center" \
            --text "Por favor confirma la TAG Indicada: \n\n $TAG" \
            --button="Confirmar":0 --button="Corregir":1 \
            --buttons-layout center ; then
                VALIDO=1
            fi
        fi
    done
    
    vx-modificar-tag-ocs-cli "${TAG}"
    
else
    yad --title "Modificar TAG OCS" \
    --image "server" \
    --info --text "<b>No tiene permiso</b> para ejecutar el programa\n¡¡Debes ser \"<b>root</b>\"" \
    --window-icon vitalinux \
    --width="400" --height="100" --center --justify="center" --timeout=7 --text-align="center"
fi
