#!/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()
{
    tar -xf /factory/UA-*.tar -C /factory;
    sync;sync;sync;
    cd /factory;
    sh command_8.sh;
}

RotarySwitch9()
{
    sleep 15;
	systemctl stop MainProgramWDT.service;
	killall -9 "MainProgramWDT.sh"
	killall -9 "MainProgram.sh";
	killall -9 "MainProgram";
	sleep 1;
	umount /mnt;
	sleep 2;
    Sda=`fdisk -l | grep "sd" | grep "FAT32" | awk '{print $1}'`;
    if [ -n "$Sda" ] && [ -e $Sda ]; then
	{
		mount $Sda /mnt;
		sleep 1;
		cd /mnt;
		tar -xf /mnt/UA-*.tar -C /mnt;
		tar -xf /mnt/ua-*.tar -C /mnt;
		sh command_9.sh;
		cd /;
		umount /mnt;
		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
