Advertisement
Guest User

Untitled

a guest
Feb 6th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.61 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. if [[ -z $1 ]]
  4. then
  5.     echo "Too few arguments. Pass -h or --help to get summary of options."
  6.     exit 1
  7. elif [[ ($1 = "-h" || $1 = "--help") && -n $2 ]]
  8. then
  9.     echo "Invalid usage. Do not specify anything after '-h' or '--help'!"
  10.     exit 1
  11. elif [[ $1 = "-h" || $1 = "--help" ]]
  12. then
  13.     printf "%s" "This script compounds list of audio files in specified directory, "
  14.     printf "%s\n" "and places that list into /tmp/${USER}_mpv/."
  15.     printf "%s" "This is useful, considering limited mpv capabilities to distinguish "
  16.     printf "%s\n" "regular files from audio files."
  17.     printf "%s\n\n" "Available options:"
  18.     printf "%s\n" "-h (--help) — display this help."
  19.     printf "%s\n" "-p (--path) — specify working directory. By default, current one is used."
  20.     printf "%s" "-n (--name) — specify name for list. By default, script will use listN "
  21.     printf "%s" "template. So that if there are no files in /tmp/${USER}_mpv, script will create "
  22.     printf "%s" "list1, then list2 and so on. If you run this script twice for the same "
  23.     printf "%s\n" "directory, you will get two same lists with different names."
  24.     printf "%s" "-d (--default) use default values. If you don't want to specify anything "
  25.     printf "%s" "then you should specify this option. This is made to prevent you from running "
  26.     printf "%s\n" "a script which purpose is unknown to you. -d overrides other options!"
  27.     printf "%s" "-t (--tempname) exists just in case some wizard occupied ${USER}_mpv directory "
  28.     printf "%s\n" "in /tmp and you cannot write in there. It specifies directory name within /tmp."
  29.     printf "%s" "-b (--basedir). If for some reason you do not want to use /tmp, you can set "
  30.     printf "%s\n" "basedir — value, that will substitute /tmp. Note that basedir must exist."
  31.     printf "%s\n" "There must be no '/' in list- or tempname, otherwise you are on your own."
  32.     exit 0
  33. fi
  34.  
  35. while (( "$#" ))
  36. do
  37.     case $1 in
  38.         -p|--path)
  39.             WORKDIR="$2"
  40.             shift 2
  41.             ;;
  42.         -n|--name)
  43.             LISTNAME="$2"
  44.             shift 2
  45.             ;;
  46.         -d|--default)
  47.             DEFAULT=1
  48.             break
  49.             ;;
  50.         -t|--tempname)
  51.             TEMPNAME="$2"
  52.             shift 2
  53.             ;;
  54.         -b|--basedir)
  55.             BASEDIR="$2"
  56.             shift 2
  57.             ;;
  58.         *)
  59.             echo "Unrecognized option '$1'. Run '-h' or '--help'"
  60.             exit 1
  61.             ;;
  62.     esac
  63. done
  64.  
  65. DEFAULT_WORKDIR="$(pwd)"
  66. DEFAULT_TEMPNAME="${USER}_mpv"
  67. DEFAULT_BASEDIR=/tmp
  68.  
  69. if (( $DEFAULT ))
  70. then
  71.     WORKDIR="$DEFAULT_WORKDIR"
  72.     TEMPNAME="$DEFAULT_TEMPNAME"
  73.     BASEDIR="$DEFAULT_BASEDIR"
  74. else
  75.     WORKDIR="${WORKDIR:-$DEFAULT_WORKDIR}"
  76.     [[ -d $WORKDIR ]] || (
  77.     echo "Working directory you specified is not directory! Run '-h' or '--help'"
  78.     exit 1
  79.     )
  80.     TEMPNAME="${TEMPNAME:-$DEFAULT_TEMPNAME}"
  81.     BASEDIR="${BASEDIR:-$DEFAULT_BASEDIR}"
  82.     [[ -d $BASEDIR ]] || (
  83.     echo "Base directory you specified is not directory! Run '-h' or '--help'"
  84.     exit 1
  85.     )
  86.  
  87.     # In case BASEDIR or WORKDIR have "/" at the end, we need to remove it
  88.     [[ ${WORKDIR: -1} = "/" ]] && WORKDIR="${WORKDIR:0: -1}"
  89.     [[ ${BASEDIR: -1} = "/" ]] && BASEDIR="${BASEDIR:0: -1}"
  90. fi
  91.  
  92. # Set list name
  93. mkdir -p "$BASEDIR/$TEMPNAME"
  94. if [[ -z $LISTNAME ]]
  95. then
  96.     LISTLASTNUMBER="$(ls -1 "$BASEDIR/$TEMPNAME" | egrep "^list[0-9]+$" | sort -V | tail -n1 | sed 's/list//')"
  97.     if [[ -z $LISTLASTNUMBER ]]
  98.     then
  99.         LISTNAME="list1"
  100.     else
  101.         LISTNAME="list$(( ++LISTLASTNUMBER ))"
  102.     fi
  103. fi
  104.  
  105. while read FILE
  106. do
  107.     # This dance with readlink is needed because when you specify relative paths with -p
  108.     # everything messes quite a bit.
  109.     FILE="$( readlink -f "$WORKDIR/$FILE" )"
  110.     if [[ $(file --mime-type -b "$FILE" | sed -E 's/^(audio)(\/.+)/\1/') = "audio" ]]
  111.     then
  112.         echo "$FILE" >> "$BASEDIR/$TEMPNAME/$LISTNAME"
  113.     fi
  114. done <<< "$(ls -1 "$WORKDIR")"
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement