#!/bin/bash

Beep()
{
  FREQ_Hz=$(awk -v x=$2 'BEGIN {printf "%d",1/x}')
  LEN_ms=$(awk -v x=$2 'BEGIN {printf "%d",x*1000}')
  DELAY_ms=$(awk -v x=$3 'BEGIN {printf "%d",x*1000}')
  for(( i=0; i<$1; i=i+1))
  do
     beep -f $FREQ_Hz -l $LEN_ms -r 1 -D $DELAY_ms
  done
}


RotarySwitch8()
{
    Beep 1 1 0;
    tar -xf /factory/ua-arm64*.tar -C /factory;
    sync;sync;sync;
    cd /factory;
    bash command_8.sh;
}

RotarySwitch9()
{
 systemctl disable ua-runtime.service
 systemctl stop ua-runtime.service
    Sda=`fdisk -l | grep "sd" | grep "FAT32" | awk '{print $1}'`;
    if [ -n "$Sda" ] && [ -e $Sda ]; then
        dir="/mnt/USB"
        if [ ! -d "$dir" ]; then
            mkdir $dir;
        fi;
        mount $Sda /mnt/USB;
        sleep 1;
        cd /mnt/USB;
        Beep 1 1 0;
        FileSystem=`df -T | grep /mnt/USB | awk '{print $2}'`;
        if [ "$FileSystem" == "vfat" ] || [ "$FileSystem" == "ntfs" ]; then
            tar --no-same-owner -xf /mnt/USB/ua-arm64*.tar -C /mnt/USB;
        else
            tar -xf /mnt/USB/ua-arm64*.tar -C /mnt/USB;
        fi
        bash command_9.sh;
        cd /;
        umount /mnt/USB;
        sleep 1;
        eject $Sda;
    else
        Beep 4 0.25 0.1;
    fi
}

RotarySwitch=$(/usr/sbin/iTalk/rsw);

case $RotarySwitch in
  "0")
      #echo "RotarySwitch 0"
      ;;
  "1")
      #echo "RotarySwitch 1"
      ;;
  "2")
      #echo "RotarySwitch 2"
      ;;
  "3")
      #echo "RotarySwitch 3"
      ;;
  "4")
      #echo "RotarySwitch 4"
      ;;
  "5")
      #echo "RotarySwitch 5"
      ;;
  "6")
      #echo "RotarySwitch 6"
      ;;
  "7")
      #echo "RotarySwitch 7"
      ;;
  "8")
      echo "RotarySwitch 8"
      RotarySwitch8;
      ;;
  "9")
      echo "RotarySwitch 9"
      RotarySwitch9;
      ;;
esac
