Advertisement
Guest User

Untitled

a guest
Feb 6th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.13 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 witout specifying -n "
  23.     printf "%s" "for the same directory, you will get two same lists with different names. "
  24.     printf "%s" "However, if you run this script twice with the same -n, lists will be "
  25.     printf "%s\n" " concatenated, which may be undesired behaviour. For this you have "
  26.     printf "%s" "-r (--remove) option. It is used with -n and ensures that list is overwrited."
  27.     printf "%s" "-d (--default) use default values. If you don't want to specify anything "
  28.     printf "%s" "then you should specify this option. This is made to prevent you from running "
  29.     printf "%s\n" "a script which purpose is unknown to you. -d overrides other options!"
  30.     printf "%s" "-t (--tempname) exists just in case some wizard occupied ${USER}_mpv directory "
  31.     printf "%s\n" "in /tmp and you cannot write in there. It specifies directory name within /tmp."
  32.     printf "%s" "-b (--basedir). If for some reason you do not want to use /tmp, you can set "
  33.     printf "%s\n" "basedir — value, that will substitute /tmp. Note that basedir must exist."
  34.     printf "%s\n" "There must be no '/' in list- or tempname, otherwise you are on your own."
  35.     exit 0
  36. fi
  37.  
  38. while (( "$#" ))
  39. do
  40.     case $1 in
  41.         -p|--path)
  42.             WORKDIR="$2"
  43.             shift 2
  44.             ;;
  45.         -n|--name)
  46.             LISTNAME="$2"
  47.             shift 2
  48.             ;;
  49.         -d|--default)
  50.             DEFAULT=1
  51.             break
  52.             ;;
  53.         -t|--tempname)
  54.             TEMPNAME="$2"
  55.             shift 2
  56.             ;;
  57.         -b|--basedir)
  58.             BASEDIR="$2"
  59.             shift 2
  60.             ;;
  61.         -r|--remove)
  62.             REMOVE=1
  63.             shift
  64.             ;;
  65.         *)
  66.             echo "Unrecognized option '$1'. Run '-h' or '--help'"
  67.             exit 1
  68.             ;;
  69.     esac
  70. done
  71.  
  72. DEFAULT_WORKDIR="$(pwd)"
  73. DEFAULT_TEMPNAME="${USER}_mpv"
  74. DEFAULT_BASEDIR=/tmp
  75.  
  76. if (( $DEFAULT ))
  77. then
  78.     WORKDIR="$DEFAULT_WORKDIR"
  79.     TEMPNAME="$DEFAULT_TEMPNAME"
  80.     BASEDIR="$DEFAULT_BASEDIR"
  81. else
  82.     WORKDIR="${WORKDIR:-$DEFAULT_WORKDIR}"
  83.     [[ -d $WORKDIR ]] || (
  84.     echo "Working directory you specified is not directory! Run '-h' or '--help'"
  85.     exit 1
  86.     )
  87.     TEMPNAME="${TEMPNAME:-$DEFAULT_TEMPNAME}"
  88.     BASEDIR="${BASEDIR:-$DEFAULT_BASEDIR}"
  89.     [[ -d $BASEDIR ]] || (
  90.     echo "Base directory you specified is not directory! Run '-h' or '--help'"
  91.     exit 1
  92.     )
  93.  
  94.     # In case BASEDIR or WORKDIR have "/" at the end, we need to remove it
  95.     [[ ${WORKDIR: -1} = "/" ]] && WORKDIR="${WORKDIR:0: -1}"
  96.     [[ ${BASEDIR: -1} = "/" ]] && BASEDIR="${BASEDIR:0: -1}"
  97. fi
  98.  
  99. # Set list name
  100. mkdir -p "$BASEDIR/$TEMPNAME"
  101. if [[ -z $LISTNAME ]]
  102. then
  103.     LISTLASTNUMBER="$(ls -1 "$BASEDIR/$TEMPNAME" | egrep "^list[0-9]+$" | sort -V | tail -n1 | sed 's/list//')"
  104.     if [[ -z $LISTLASTNUMBER ]]
  105.     then
  106.         LISTNAME="list1"
  107.     else
  108.         LISTNAME="list$(( ++LISTLASTNUMBER ))"
  109.     fi
  110. fi
  111.  
  112. (( REMOVE )) && {
  113.     [[ $LISTNAME =~ ^list[0-9]+$ ]] && {
  114.         echo "You can use -r only when list name does not match listN pattern! Exiting."
  115.         exit 1
  116.         }
  117.     rm "$BASEDIR/$TEMPNAME/$LISTNAME"
  118.     }
  119. while read FILE
  120. do
  121.     # This dance with readlink is needed because when you specify relative paths with -p
  122.     # everything messes quite a bit.
  123.     FILE="$( readlink -f "$WORKDIR/$FILE" )"
  124.     if [[ $(file --mime-type -b "$FILE" | sed -E 's/^(audio)(\/.+)/\1/') = "audio" ]]
  125.     then
  126.         echo "$FILE" >> "$BASEDIR/$TEMPNAME/$LISTNAME"
  127.     fi
  128. done <<< "$(ls -1 "$WORKDIR")"
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement