Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- if [[ -z $1 ]]
- then
- echo "Too few arguments. Pass -h or --help to get summary of options."
- exit 1
- elif [[ ($1 = "-h" || $1 = "--help") && -n $2 ]]
- then
- echo "Invalid usage. Do not specify anything after '-h' or '--help'!"
- exit 1
- elif [[ $1 = "-h" || $1 = "--help" ]]
- then
- printf "%s" "This script compounds list of audio files in specified directory, "
- printf "%s\n" "and places that list into /tmp/${USER}_mpv/."
- printf "%s" "This is useful, considering limited mpv capabilities to distinguish "
- printf "%s\n" "regular files from audio files."
- printf "%s\n\n" "Available options:"
- printf "%s\n" "-h (--help) — display this help."
- printf "%s\n" "-p (--path) — specify working directory. By default, current one is used."
- printf "%s" "-n (--name) — specify name for list. By default, script will use listN "
- printf "%s" "template. So that if there are no files in /tmp/${USER}_mpv, script will create "
- printf "%s" "list1, then list2 and so on. If you run this script twice witout specifying -n "
- printf "%s" "for the same directory, you will get two same lists with different names. "
- printf "%s" "However, if you run this script twice with the same -n, lists will be "
- printf "%s\n" " concatenated, which may be undesired behaviour. For this you have "
- printf "%s" "-r (--remove) option. It is used with -n and ensures that list is overwrited."
- printf "%s" "-d (--default) use default values. If you don't want to specify anything "
- printf "%s" "then you should specify this option. This is made to prevent you from running "
- printf "%s\n" "a script which purpose is unknown to you. -d overrides other options!"
- printf "%s" "-t (--tempname) exists just in case some wizard occupied ${USER}_mpv directory "
- printf "%s\n" "in /tmp and you cannot write in there. It specifies directory name within /tmp."
- printf "%s" "-b (--basedir). If for some reason you do not want to use /tmp, you can set "
- printf "%s\n" "basedir — value, that will substitute /tmp. Note that basedir must exist."
- printf "%s\n" "There must be no '/' in list- or tempname, otherwise you are on your own."
- exit 0
- fi
- while (( "$#" ))
- do
- case $1 in
- -p|--path)
- WORKDIR="$2"
- shift 2
- ;;
- -n|--name)
- LISTNAME="$2"
- shift 2
- ;;
- -d|--default)
- DEFAULT=1
- break
- ;;
- -t|--tempname)
- TEMPNAME="$2"
- shift 2
- ;;
- -b|--basedir)
- BASEDIR="$2"
- shift 2
- ;;
- -r|--remove)
- REMOVE=1
- shift
- ;;
- *)
- echo "Unrecognized option '$1'. Run '-h' or '--help'"
- exit 1
- ;;
- esac
- done
- DEFAULT_WORKDIR="$(pwd)"
- DEFAULT_TEMPNAME="${USER}_mpv"
- DEFAULT_BASEDIR=/tmp
- if (( $DEFAULT ))
- then
- WORKDIR="$DEFAULT_WORKDIR"
- TEMPNAME="$DEFAULT_TEMPNAME"
- BASEDIR="$DEFAULT_BASEDIR"
- else
- WORKDIR="${WORKDIR:-$DEFAULT_WORKDIR}"
- [[ -d $WORKDIR ]] || (
- echo "Working directory you specified is not directory! Run '-h' or '--help'"
- exit 1
- )
- TEMPNAME="${TEMPNAME:-$DEFAULT_TEMPNAME}"
- BASEDIR="${BASEDIR:-$DEFAULT_BASEDIR}"
- [[ -d $BASEDIR ]] || (
- echo "Base directory you specified is not directory! Run '-h' or '--help'"
- exit 1
- )
- # In case BASEDIR or WORKDIR have "/" at the end, we need to remove it
- [[ ${WORKDIR: -1} = "/" ]] && WORKDIR="${WORKDIR:0: -1}"
- [[ ${BASEDIR: -1} = "/" ]] && BASEDIR="${BASEDIR:0: -1}"
- fi
- # Set list name
- mkdir -p "$BASEDIR/$TEMPNAME"
- if [[ -z $LISTNAME ]]
- then
- LISTLASTNUMBER="$(ls -1 "$BASEDIR/$TEMPNAME" | egrep "^list[0-9]+$" | sort -V | tail -n1 | sed 's/list//')"
- if [[ -z $LISTLASTNUMBER ]]
- then
- LISTNAME="list1"
- else
- LISTNAME="list$(( ++LISTLASTNUMBER ))"
- fi
- fi
- (( REMOVE )) && {
- [[ $LISTNAME =~ ^list[0-9]+$ ]] && {
- echo "You can use -r only when list name does not match listN pattern! Exiting."
- exit 1
- }
- rm "$BASEDIR/$TEMPNAME/$LISTNAME"
- }
- while read FILE
- do
- # This dance with readlink is needed because when you specify relative paths with -p
- # everything messes quite a bit.
- FILE="$( readlink -f "$WORKDIR/$FILE" )"
- if [[ $(file --mime-type -b "$FILE" | sed -E 's/^(audio)(\/.+)/\1/') = "audio" ]]
- then
- echo "$FILE" >> "$BASEDIR/$TEMPNAME/$LISTNAME"
- fi
- done <<< "$(ls -1 "$WORKDIR")"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement