Sunday, June 26, 2011

Checking if SD Card is writable and disk space

Another problem came up while my Eee PC was being used for sermon recording today - the temporary files could not be saved onto the SD Card. Finally found out that the write lock of the SD Card was switched on. Here's the modified bash script for starting up Audacity on my Eee PC:

#!/bin/bash

#Overwrite /root/.audacity and ~/.audacity settings with specially saved backup
#to ensure temp directory is set to /home/user/.audacity_temp
sudo cp /home/user/.audacity_backupPrefs /root/.audacity
sudo chmod 777 /root/.audacity
sudo cp /home/user/.audacity_backupPrefs /home/user/.audacity
sudo chmod 777 /home/user/.audacity


#Get name of SD card in card slot - should be first directory
DIRECTORY="/home/user/MMC-SD"
for file in $DIRECTORY/*
do
carddir=$file
break
done


#Test if carddir exists
if !(test -d $carddir)
then
kdialog --msgbox "No SD Card found! \n\n Audacity will not launch."
exit
fi


#Test if SD Card is writable (in case of accidental lock)
#2>/dev/null suppresses all error messages
if !(touch $carddir 2>/dev/null)
then
kdialog --msgbox "Cannot write to SD Card - is it locked?? \n\n Audacity will not launch."
exit
fi


#Check if there is enough disk space - at least 2GB, ie. 2097152 KB
#the backtick ` executes the command
#$4 refers to the 4th field
enough=`df $carddir | grep '/dev' | awk '{print (int($4)>2097152)}'`
if [ $enough == '0' ]
then
kdialog --msgbox "SD Card has less than 2GB available space for recording. \n\n Audacity will not launch"
exit
fi

#create .audacity_temp/ in SD Card
tempdir=$carddir'/.audacity_temp'
if !(test -d $tempdir)
then
sudo mkdir $tempdir
sudo chmod 777 $tempdir
fi


#create symbolic link to /home/user/.audacity_temp
#must remove old link first
sudo rm /home/user/.audacity_temp
sudo ln -s $tempdir /home/user/.audacity_temp
sudo chmod 777 /home/user/.audacity_temp


#Set microphone volume to 20% (else feedback), capture mode on, unmute
sudo amixer -c 0 set Capture 20% cap unmute


#Check whether AC power is on
powerStatus=$(awk '{ print $2 }' /proc/acpi/ac_adapter/AC0/state)
if [ $powerStatus != "on-line" ]
then
kdialog --msgbox "POWER IS NOT SWITCHED ON! \n\n Audacity will not launch."
exit
fi

#must run audacity with 'sudo' or else will have file writing permission error
sudo audacity
exit 0
(^ v ^)