Update: I've added a command-line argument (-c --configure) to explicitly invoke the configuration questionaire. If the .importPrefs file already exists, then the preferences are pulled from it and the process runs automatically without any interaction from the user. If the the .importPrefs file does not exist or -c (--configure) is specified on the command-line then the preferences are read from the user and create/overwrite the existing .importPrefs file.
Problem: if I create a function to handle command-line argument parsing then I lose the command-line arguments $1 and so-forth. In the current code where command-line parsing occurs first it correctly determines the value of $DOCONFIG but for some reason the check for .importPrefs fails and sets the value of DOCONFIG to 'yes' when it should remain 'no' given the scenario: .importPrefs exists and -c was
not passed on the command line. It should just produce "Done!' without doing anything.
Any suggestions are welcome.
Code:
#!/bin/bash
###############
# Name: importMusic.sh
# Author: Brion Swanson
# Desc: This script questions the user about the location of the music
# from which it will be imported and to where it will be stored.
#
# This script stores the answers of the response in a file called
# .importPrefs in the user's home directory and will pre-populate
# the responses to the questions if it exists.
#
# Files will be moved from the source to the destination and renamed
# according to the MP3/Ogg tagging if available. The format of the
# file name can be configured by the user.
###############
###############
# Variables #
###############
SRCDIR=".";
DESTDIR="/usr/share/music/";
RENAMEFILES="yes";
MOVEFILES="yes";
FNAMEFORMAT="R-A-T";
MOVECMD=`which mv`;
DOCONFIG="no";
###############
# Functions #
###############
### readPrefs
# Read user preferences from ~/.importPrefs (if it exists) and set
# the variables to those defaults
#####
readPrefs() {
for line in `cat ~/.importPrefs`; do
var=`echo $line | cut -d= -s -f1`;
value=`echo $line | cut -d= -s -f2`;
case ${var} in
sourceDir)
SRCDIR="$value";
;;
destDir)
DESTDIR="$value";
;;
renameFiles)
RENAMEFILES="$value";
;;
moveFiles)
MOVEFILES="$value";
;;
filenameFormat)
FNAMEFORMAT="$value";
;;
moveCmd)
MOVECMD="$value";
;;
esac
done
}
### writePrefs
# Writes the user's preferences out to the ~/.importPrefs file.
# These values will be read in as the default choices next time
# the script is run.
#####
writePrefs() {
# create an array to hold all the data to be written out
# for ease and speed of writing
declare -a state;
# add each piece of state data to the array in order
state[0]="sourceDir=$SRCDIR";
state[1]="destDir=$DESTDIR";
state[2]="renameFiles=$RENAMEFILES";
state[3]="moveFiles=$MOVEFILES";
state[4]="filenameFormat=$FNAMEFORMAT";
state[5]="moveCmd=$MOVECMD";
# clear out the previous preferences file
# and write a header to warn people not to modify by hand
echo "## This is a generated file - changes made by hand will be lost ##" > ~/.importPrefs;
# now loop through the array writing out the data to file
stateCount=${#state[*]}
for (( i=0; $i != $stateCount; i++ )) do
echo "${state[$i]}" >> ~/.importPrefs
done
}
### rebuildFilename
# Analyzes the MP3 or Ogg file's metadata and attempts to generate
# an approprite file name based on the user's preferred filename
# format (default: aRtist-Album-Track). Returns the generated name.
#####
buildFilename() {
echo "";
}
### normalizeFilename
# Normalizes filenames by removing apostrophes and quotes and converting
# parentheses and spaces to underscores.
#####
normalizeFilename() {
echo "";
}
###############
# Script #
###############
echo $DOCONFIG
if [ $# > 0 ]; then
echo $1;
if [ -n $1 ]; then
if [ $1=="-c" -o $1=="--configure" ]
then
DOCONFIG="yes"
fi
fi
fi
echo $DOCONFIG
# read in any saved preferences first
if [ -e ~/.importPrefs ]
then
readPrefs
else
DOCONFIG="yes"
fi
echo $DOCONFIG
# do user questionaire here if necessary
if [ $DOCONFIG == "yes" ]; then
read -p "Source directory? [$SRCDIR] " NEWSRCDIR
if [ -n "$NEWSRCDIR" ]
then
SRCDIR="$NEWSRCDIR";
fi
read -p "Destination directory? [$DESTDIR] " NEWDESTDIR
if [ -n "$NEWDESTDIR" ]
then
DESTDIR="$NEWDESTDIR";
fi
read -p "Rename destination file? [$RENAMEFILES] " NEWRENAMEFILE
if [ -n "$NEWRENAMEFILE" ]
then
RENAMEFILES="$NEWRENAMEFILE";
fi
read -p "Move files from destination to source? [$MOVEFILES] " NEWMOVEFILES
if [ -n "$NEWMOVEFILES" ]
then
MOVEFILES="$NEWMOVEFILES";
fi
read -p "Filename format pattern (aRtist, Album, Track) [$FNAMEFORMAT] " NEWFNAMEFORMAT
if [ -n "$NEWFNAMEFORMAT" ]
then
FNAMEFORMAT="$NEWFNAMEFORMAT";
fi
read -p "Command to transfer files from source to dest? [$MOVECMD] " NEWMOVECMD
if [ -n "$NEWMOVECMD" ]
then
MOVECMD="$NEWMOVECMD";
fi
echo "Saving preferences...";
writePrefs;
fi
# if file rename, do conversion here (call normalize function)
# copy/move file
# call insert function to the DB here
echo "Done!";