#!/usr/bin/env bash

#  usr/bin/guvcview-verify-busy
#  Created: 0000/00/00
#  Altered: 2023/09/02
#
#  Copyright (c) 2023-2023, Vilmar Catafesta <vcatafesta@gmail.com>
#                0000-2023, bigbruno Bruno Gonçalves <bruno@biglinux.com.br>
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

APP="${0##*/}"
_VERSION_="1.0.0-20230902"
export BOOTLOG="/tmp/bigcontrolcenter-$USER-$(date +"%d%m%Y").log"
export LOGGER='/dev/tty8'
LIBRARY=${LIBRARY:-'/usr/share/bigbashview/bcc/shell'}
[[ -f "${LIBRARY}/bcclib.sh" ]] && source "${LIBRARY}/bcclib.sh"

function sh_config {
   #Translation
   export TEXTDOMAINDIR="/usr/share/locale"
   export TEXTDOMAIN=bigcontrolcenter
   declare -g video_device='/dev/video0'
   declare -gA Amsg=(
      [title]=$(gettext $"Central de Controle - Webcam")
      [error]=$(gettext $"Nenhuma webcam localizada.")
      [busy]=$(gettext $"A webcam já está sendo utilizada pelos aplicativos: ")
      [msg1]=$(gettext $"Abrirei em modo de configuração. ")
      [msg2]=$(gettext $"As alterações feitas podem ser vistas")
      [msg3]=$(gettext $"na imagem da sua webcam em outros programas.")
   )
}

function sh_main {
   if ! test -c "$video_device"; then
      kdialog --title "${Amsg[title]}" --error "${Amsg[error]}"
      return
   fi

#  WEBCAM_BUSY="$(ps ax | grep $(fuser "$video_device" 2>/dev/null) | awk '{print $5}')"
#  WEBCAM_BUSY=$(lsof "$video_device" 2>/dev/null | awk 'NR==2 {print $1}')
#  WEBCAM_BUSY=$(lsof "$video_device" 2>/dev/null| tail -1| awk '{print $1}')
#  WEBCAM_BUSY=$(lsof /dev/video0 2>/dev/null | awk 'NR>1 {print $0}' | grep guvcview)
   WEBCAM_BUSY=$(lsof "$video_device" 2>/dev/null | awk 'NR>1 {print "pid : " $2 " => " $1}' | grep guvcview)
   if [[ -z "$WEBCAM_BUSY" ]]; then
		guvcview
   else
      if kdialog --yesno "${Amsg[busy]}\n$WEBCAM_BUSY\n\n${Amsg[msg1]}\n${Amsg[msg2]}\n${Amsg[msg3]}\n" --title "${Amsg[error]}"; then
         guvcview -z
      fi
   fi
}

#sh_debug
sh_config
sh_main
