#! /bin/bash
Beep()
{
  for(( i=0; i<$1; i=i+1))
  do
     cd /sys/class/pwm/ehrpwm.0:1;
     echo 100 > period_freq;
     echo 50 > duty_percent;
     echo 1 > run;
     uSecond=`awk -v x=$2 -v y=1000000 'BEGIN {printf "%d",x*y}'`;
     usleep $uSecond;
     echo 0 > run;
     uSecond=`awk -v x=$3 -v y=1000000 'BEGIN {printf "%d",x*y}'`;
     usleep $uSecond;
  done
}

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

RotarySwitch9()
{
    sleep 15;
	systemctl disable OPC-5231-Runtime.service
	systemctl stop OPC-5231-Runtime.service
	sleep 1;
	sleep 2;
    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;
		tar -xf /mnt/USB/UA-*.tar -C /mnt/USB;
		tar -xf /mnt/USB/ua-*.tar -C /mnt/USB;
		bash command_9.sh;
		cd /;
		umount /mnt/USB;
		sleep 1;
		eject $Sda;
	}
    else
      Beep 4 0.001 0.5;
    fi
}



RotarySwitch=`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
