#!/bin/sh

echo " Purpose: To upload mp3's to  MuVo 128k"
echo "  The Creative Labs MuVo 128k sucks in Linux!!!"
echo "  To dependibly write and erase from it, "
echo "  I have to mount and unmount for every write."
echo "  This is a script so I don't have to do it by hand"
echo "  " 
echo "  Script assumes we are mounting /dev/sda1 "
echo "  Script checks where you mount it from /etc/fstab." 
echo "  You need a line in /etc/fstab like:"
echo "      /dev/sda1 /mnt/sda1 auto noauto,user,exec 0 0 " 
echo "  Script asks for a directory containing mp3's you want to upload"
echo "  Script mounts, writes one file, and unmounts, until the list is done."
echo "  " 

mount_point=`grep sda1 /etc/fstab  |awk '{print $2}'`
if [ x$mount_point == "x" ]; then
   echo "You don't have an entery in /etc/fstab for /dev/sda1"
   echo "exiting"
   exit 1
fi
echo "Do you want to erase the mp3's on the player?(y/n)"
read Anser
if ( $Anser == [yY] ); then
  mount $mount_point
  files=`ls $mount_point`
  for file in $files
  do
     umount $mount_point
     mount $mount_point
     rm -rf $mount_point/$file
  done
 


echo "Please print the absolute path to the directory with your mp3's."
echo "( Do not include the final slash )"
read Directory
echo "Listing $Directory"
Files=`ls $Directory`
echo "I'll upload these files:"
for File in  $Files; do
    echo -e "\t$File"
done
echo "Does this look correct?(y/n)"
read Answer
if [ x$Answer == "xy" ]; then
   echo "Beginning process"
   for File in $Files; do
       echo "unmounting $mount_point"
       umount $mount_point  
       echo "mounting $mount_point"
       mount $mount_point 
       echo "copying $Directory/$File to $mount_point/"
       cp $Directory/$File $mount_point/ 
       echo "Checking file sizes:"
	   ls -alF $Directory/$File
	   ls -alF $mount_point/$File
       echo "unmounting $mount_point"
       umount $mount_point  
   done
fi
echo "Job done"
