Advertisement
ZaxonXP45

mod_cleanup.sh

Oct 13th, 2022 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.79 KB | None | 0 0
  1. #!/bin/bash
  2. name=$(basename $0)
  3.  
  4.  SORTED=~/Music/Modules/Sorted/
  5. DELETED=~/Music/Modules/_scripts/deleted
  6.  GENRES=~/Music/Modules/_scripts/downloaded
  7.  
  8. if [[ ! $1 ]]; then
  9.     echo ""
  10.     echo "Plays the MOD from the directory and allows to decide if to delete or keep afterwards"
  11.     echo ""
  12.     echo "$name mods_dir_name"
  13.     echo ""
  14.     exit 1
  15. fi
  16.  
  17. ###########################################
  18. choose_location() {
  19.     local name=$1
  20.     local ff=$2
  21.  
  22.     unset folder
  23.  
  24.     name=${name##*/}
  25.  
  26.     # find if the file has genre
  27.     fgen=$(grep ";$name;" $GENRES | cut -d';' -f3 )
  28.  
  29.     # if genre found then move it automatically
  30.     # otherwise allow user to choose
  31.     if [[ "$fgen" != "n/a" ]]; then
  32.  
  33.         [ ! -d $SORTED/$fgen ] && mkdir "$SORTED/$fgen"
  34.         [   -d $SORTED/$fgen ] && mv "$ff" $SORTED/$fgen/ && FLAG=1
  35.  
  36.     else
  37.         IFS=$'\n'
  38.         # get existing dirs and also from the genres list
  39.         dirs1=( $( cat $GENRES | cut -d';' -f3 | grep -v "n/a" ) )
  40.         dirs2=( $( find $SORTED -mindepth 1 -type d -printf "%f\n") )
  41.         dirs=( ${dirs1[*]} ${dirs2[*]} )
  42.         dirs=( 0 $(echo "${dirs[*]}" | sort | uniq ) )
  43.         max=$(( ${#dirs[*]} - 1 ))
  44.        
  45.         for (( i=1; i <= $max; i++ )); do
  46.             printf "%02d) %-35s\n" "$i" "${dirs[$i]}"
  47.         done | column
  48.        
  49.         read -N 2 -p "Select / (ne)w: " answ
  50.  
  51.         # User selected existing folder or will type the new name
  52.         if [[ ($answ -gt 0) && ($answ -le $max) ]]; then
  53.  
  54.             folder=${dirs[$answ]}
  55.             [ ! -d $SORTED/$folder ] && mkdir "$SORTED/$folder"
  56.             [   -d $SORTED/$folder ] && mv "$ff" $SORTED/$folder/ && FLAG=1
  57.         else
  58.             read -p "New folder name: " folder
  59.             [ ! -d $SORTED/$folder ] && mkdir "$SORTED/$folder"
  60.             [   -d $SORTED/$folder ] && mv "$ff" $SORTED/$folder/ && FLAG=1
  61.         fi
  62.     fi
  63. }
  64.  
  65. ###########################################
  66. cleanup() {
  67.     rm -r $tmp_dir
  68.     exit 0;
  69. }
  70.  
  71. ###########################################
  72. trap 'cleanup' SIGINT
  73.  
  74. if [[ -d $1 ]]; then
  75.     dir=$1
  76.  
  77.     tmp_dir=$(mktemp -d)
  78.  
  79.     IFS=$'\n'
  80.  
  81.     files=($dir/*)
  82.  
  83.     while [ $files ]; do
  84.  
  85.         if [ ! -v current ]; then
  86.             count=${#files[*]}
  87.  
  88.             file=${files[0]}
  89.  
  90.             if [ ${file: -3} == .7z ]; then
  91.  
  92.                 rm $tmp_dir/* > /dev/null 2>&1
  93.  
  94.                 7z e $file -o$tmp_dir
  95.                 current=$tmp_dir/$(ls $tmp_dir)
  96.             else
  97.                 current=$file
  98.             fi
  99.         fi
  100.  
  101.         clear
  102.         echo -e "\e[39m$count files to process"
  103.         echo -e ""
  104.         openmpt123 --no-meters -- $current
  105.         echo ""
  106.         echo -en "\e[33m[R]eplay, \e[31m[D]elete, \e[32m[K]eep, \e[36m[Q]uit\e[39m ? : "
  107.         read -N 1 answ
  108.         echo ""
  109.  
  110.         case $answ in
  111.         [d])
  112.  
  113.             echo -ne "\e[31mDelete \"$file\" ? "
  114.             read -N 1 answ2
  115.  
  116.             if [[ "$answ2" == "y" ]]; then
  117.                 rm $file
  118.                 files=(${files[@]:1:${#files[@]}})
  119.                 prefix=${current%/*}
  120.                 echo ${current/$prefix\/} >> $DELETED
  121.                 unset current
  122.             fi
  123.             ;;
  124.  
  125.         [D])
  126.             rm $file
  127.             files=(${files[@]:1:${#files[@]}})
  128.             prefix=${current%/*}
  129.             echo ${current/$prefix\/} >> $DELETED
  130.             unset current
  131.             ;;
  132.         [kK])
  133.             choose_location $current $file
  134.             if [ $FLAG -gt 0 ]; then
  135.                 files=(${files[@]:1:${#files[@]}})
  136.                 FLAG=0
  137.                 unset current
  138.             fi
  139.             ;;
  140.         [rR])
  141.             ;;
  142.         [qQ])
  143.             cleanup
  144.             echo ""
  145.             exit 0
  146.             ;;
  147.         esac
  148.     done
  149. fi
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement