#!/bin/bash
MAIN_TITLE="Controllo e configurazione dei dischi USB / PenDrive"

mkdir /mnt/usb_hd >/dev/null 2>&1
source /opt/euro-beta/set3g_env
if ! which sysmsg >/dev/null 2>&1 ;
then
  PATH=$PATH:/opt/euro-beta
fi 
export DISPLAY=:0 

sysquery "$MAIN_TITLE" "Durante la procedura di verifica del PenDrive viene interotta eventuale registrazione delle immagini sullo stesso PenDrive" "Continuare" "Abandonare"
if [ $? -ne 0 ] ;
then
  exit 0
fi

rm /dev/usb_disc
# /dev/usb_disc will be created as symbolic link by hotplug subsystem

sysmsg "$MAIN_TITLE" "Se collegato, SCOLLEGA PenDrive dalla porta USB del sistema Euro3G, attendi qualche secondo ( probabilmente si sentira' il suono, che conferma il distacco ) e premi O.K."
sleep 1
sysmsg "$MAIN_TITLE" "Collega PenDrive alla porta USB, attendi qualche secondo ( dovresti sentire il suono che conferma il collegamento ) e premi O.K."
sleep 1

PHYS_DEVICE=`ls -l /dev/usb_disc | cut -d'>' -f2 | tr -d " "`
umount /dev/usb_disc
if [ ! -z "$PHYS_DEVICE" ] ;
then
  umount $PHYS_DEVICE
fi
BACKUP_PHYS_DEVICE=`ls -l /dev/backup | cut -d'>' -f2 | tr -d " "`
if [ ! -z "$BACKUP_PHYS_DEVICE" -a ".$BACKUP_PHYS_DEVICE"=".$PHYS_DEVICE" ] ;
then
  UM_MOUNTPOINT=`mount | grep $BACKUP_PHYS_DEVICE | cut -d' ' -f3`
  export UM_MOUNTPOINT
  /etc/usbmount/umount.d/01_remove_euro3g_symlink
  umount $BACKUP_PHYS_DEVICE
  rm /dev/backup
fi



mount /dev/usb_disc /mnt/usb_hd -t vfat
if [ $? -ne 0 ] ; 
then
  mount /dev/usb_disc /mnt/usb_hd
fi

if [ $? -eq 0 ] ; 
then
  if [ -f /mnt/usb_hd/vidsor.txt ] ;
  then
    IPCAM_STORAGE_SIGNATURE=`head -1 /mnt/usb_hd/vidsor.txt | tr -d -c [[:print:]]`
    if [ "$IPCAM_STORAGE_SIGNATURE." = ".." -o -z $IPCAM_STORAGE_SIGNATURE ] ;
    then
      VIDEO_MODE="4 videocamere"
    else
      VIDEO_MODE="videocamera [$IPCAM_STORAGE_SIGNATURE]"
    fi
    sysmsg "$MAIN_TITLE" "Il PenDrive e' configurato per registrare immagini da $VIDEO_MODE e quindi NON puo' essere utilizzato per i salvataggi o per l'installazione dei aggiornamenti software"          
  else
    sysmsg "$MAIN_TITLE" "Il PenDrive non e' configurato per la registrazione delle immagini e quindi puo' essere utilizzato per i salvataggi o per instalazzione dei aggiornamenti software"  
  fi

  echo "<xml>" > /opt/euro-beta/pendrive_tools.xml
  echo >> /opt/euro-beta/pendrive_tools.xml
  echo "  <title text='Configurazione del PenDrive USB'>" >> /opt/euro-beta/pendrive_tools.xml
  echo "  </title>" >> /opt/euro-beta/pendrive_tools.xml  
  echo >> /opt/euro-beta/pendrive_tools.xml
  echo "  <choice name='Configura registrazione da 4 videocamere' command='echo . &gt; /mnt/usb_hd/vidsor.txt'>" >> /opt/euro-beta/pendrive_tools.xml
  echo "  </choice>" >> /opt/euro-beta/pendrive_tools.xml
  echo >> /opt/euro-beta/pendrive_tools.xml
  for IPCAM in 1 2 3 4 ; do
    echo "  <choice name='Configura registrazione da videocamera Nr.$IPCAM' command='echo ipcam_$IPCAM &gt; /mnt/usb_hd/vidsor.txt'>" >> /opt/euro-beta/pendrive_tools.xml
    echo "  </choice>" >> /opt/euro-beta/pendrive_tools.xml
    echo >> /opt/euro-beta/pendrive_tools.xml
  done
  echo "  <choice name='Configura uso normale ( salvataggi / aggiornamenti )' command='rm /mnt/usb_hd/vidsor.txt'>" >> /opt/euro-beta/pendrive_tools.xml
  echo "  </choice>" >> /opt/euro-beta/pendrive_tools.xml
  echo >> /opt/euro-beta/pendrive_tools.xml

  if [ -z $VIDEO_MODE ] ;
  then
    echo "  <choice name='Visualizza il contenuto' command='echo \"&lt;pre&gt;\" &gt; /tmp/pendrive_dump.txt ;  ls -lA /mnt/usb_hd | cut -d\" \" -f 5-  &gt;&gt; /tmp/pendrive_dump.txt ; echo \"&lt;/pre&gt;\" &gt;&gt; /tmp/pendrive_dump.txt ; /opt/euro-beta/show_file /tmp/pendrive_dump.txt \" Contenuto del PenDrive \"'>" >> /opt/euro-beta/pendrive_tools.xml
    echo "  </choice>" >> /opt/euro-beta/pendrive_tools.xml
    echo >> /opt/euro-beta/pendrive_tools.xml
  fi

  echo >> /opt/euro-beta/pendrive_tools.xml
  echo "</xml>">> /opt/euro-beta/pendrive_tools.xml

  /opt/euro-beta/btn_choice pendrive_tools.xml

#  VIDEO_MODE=""
#  if [ -f /mnt/usb_hd/vidsor.txt ] ;
#  then
#    IPCAM_STORAGE_SIGNATURE=`head -1 /mnt/usb_hd/vidsor.txt | tr -d -c [[:print:]]`
#    if [ "$IPCAM_STORAGE_SIGNATURE." = ".." -o -z $IPCAM_STORAGE_SIGNATURE ] ;
#    then
#      VIDEO_MODE="4 videocamere"
#    else
#      VIDEO_MODE="videocamera [$IPCAM_STORAGE_SIGNATURE]"
#    fi
#  fi

  umount /dev/usb_disc
  UM_MOUNTPOINT=`mount | grep $PHYS_DEVICE | cut -d' ' -f3`
  export UM_MOUNTPOINT
  /etc/usbmount/umount.d/01_remove_euro3g_symlink
  sysmsg "$MAIN_TITLE" "Procedura terminata. SCOLLEGA PenDrive dalla porta USB del sistema Euro3G e premi O.K."

else
  sysmsg "$MAIN_TITLE" "Impossibile verificare il PenDrive. Riprovare e se il problemma persiste contattare l'assistenza tecnica."
fi

exit 0


