#!/bin/bash

DIR=""

for folder in `ls /media 2> /dev/null`; do
  if [ -d "/media/$folder/Maquinas" ]; then
    DIR="/media/$folder/Maquinas"
  elif [ -d "/media/$folder/Machines" ]; then
    DIR="/media/$folder/Machines"
  fi
done

if [ "$DIR" = "" ]; then
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  echo "Can't find a Machines PEN... Insert one and logout and login again..."
  echo "---------------------------------------------------------------------"
  echo "Press CTRL+D after the PEN is inserted..."
  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  exit 0
fi

COUNT=1
MSTR=""

for machine in `ls $DIR 2> /dev/null | sort`; do
  if [ "$machine" != "software" ]; then
    MSTR="$MSTR $machine $COUNT "
    COUNT=`expr $COUNT + 1`
  fi
done

MAQ=$(whiptail --title "Machine Selector" --menu "Select machine" 30 45 22 \
$MSTR \
3>&1 1>&2 2>&3
)

exitstatus=$?

if [ "$exitstatus" = "1" ]; then
  exit
fi

if [ -f "$DIR/$MAQ/install" ]; then
  disk_rw
  $DIR/$MAQ/install
else
  echo "Missing - $DIR/$MAQ/install"
fi

exit 0;

