From Gray Cat, 3 Years ago, written in Bash.
Embed
  1. #!/bin/bash
  2.  
  3. ## Set up the running environment
  4. # Making sure spaces are not interpreted as newline
  5. SAVEIFS=$IFS
  6. IFS=$(echo -e "\n\b")
  7. # Making sure PATH variable contains all necessary binary paths
  8. path_backup="$PATH";
  9. initial_path=":$PATH:";
  10. new_path="$PATH";
  11. for test_path in $(echo -e "/usr/local/sbin\n/usr/local/bin\n/usr/sbin\n/usr/bin\n/sbin\n/bin\n/usr/games\n/usr/bin\n/bin\n/usr/sbin\n/sbin\n/usr/local/bin\n/usr/X11/bin\n/Applications\n/opt/syb/sigma/bdj/jvm/bin\n/usr/bin/X11\n/opt/syb/app/bin\n/opt/syb/app/sbin\n/opt/syb/sigma/bin\n/nmt/apps"); do
  12.         if [[ -d "$test_path" && "$initial_path" != *:$test_path:* ]]; then
  13.                 new_path="$new_path:$test_path";
  14.         fi
  15. done
  16. export PATH="$new_path"
  17.  
  18. for bin in $(echo -e "basename\ncat\nchmod\nchown\ndate\ndirname\ndu\necho\negrep\nexpr\nfind\ngrep\nid\nls\nmkdir\nmv\nrm\nsed\ntouch\nwc"); do
  19.         if [ ! -x "$(which "$bin")" ]; then missing_bin="$missing_bin\n$bin"; fi
  20. done
  21. if [ "$missing_bin" ]; then
  22.         if [ -t 1 ]; then echo -e "Sorry but this script cannot be run. Those binaries are missing :\n$missing_bin\n\n"; fi;
  23.         exit;
  24. fi
  25.  
  26.  
  27. ##################################################################################
  28. ##                   TORRENTEXPANDER
  29. ##                   v0.21
  30. ##
  31. ## You can use and modify this script to your convenience
  32. ## Any suggestion on how to improve this script will be welcome
  33. ##
  34. ##
  35. ## There are various ways to run this script as a daemon :
  36. ## - It can be invoked with the command torrentexpander.sh "/Path/to/your/torrent"
  37. ## - It can be triggered by the Transmission torrent client
  38. ## - You can export torrent="/Path/to/your/torrent" and start torrentexpander.sh
  39. ## - If you want Flexget to trigger torrentexpander.sh, have it echo the torrent path
  40. ##   to a file and start torrentexpander.sh. The third_party_log file will be used
  41. ##   as a source file for torrentexpander.sh and the resulting files will then be
  42. ##   listed in this third_party_log file.
  43. ## - You can edit your settings in the torrentexpander_settings.ini file that will
  44. ##   be created the first time this script is launched. You can also access your
  45. ##   settings by starting this script with the command /path/to/torrentexpander.sh -c
  46. ##
  47. ## If youre running this script manually you ll get a basic GUI that will enable you to
  48. ## choose a torrent file or folder and the destination where it should be expanded
  49. ##
  50. ##################################################################################
  51.  
  52. ##################################################################################
  53. ############################# REQUIRED SOFTWARE ##################################
  54. # unrar
  55. # unrar-nonfree
  56. # unzip
  57. ################## SOFTWARE USED BY OPTIONAL FUNCTIONALITIES #####################
  58. ## Convert DTS track from MKV files to AC3
  59. # https://github.com/JakeWharton/mkvdts2ac3/blob/master/mkvdts2ac3.sh
  60. # http://www.bunkus.org/videotools/mkvtoolnix/downloads.html
  61. # http://www.videolan.org/developers/libdca.html
  62. # http://aften.sourceforge.net/
  63. ## Convert IMG to ISO
  64. # http://sourceforge.net/projects/ccd2iso/
  65. ##################################################################################
  66. ############################### END SOFTWARE #####################################
  67.  
  68. ##################################################################################
  69. ################################ USER VARIABLES ##################################
  70. ########################### These variables are required #########################
  71. ################# On 1st run they will be stored in an ini file###################
  72. ## The destination folder is where files will be extracted
  73. ## I recommend using a different folder from the one where your torrents are located
  74. ## A sub directory of your torrents directory is fine though.
  75. ## If you really want to extract your torrents in-place and delete the
  76. ## original torrent, switch destructive_mode to yes
  77. destination_folder="/path/to/your/destination/folder/"
  78. destructive_mode="no"
  79. ################################ Software paths ##################################
  80. ################# Please check if these variables are correct ####################
  81. unrar_bin="/usr/local/bin/unrar"
  82. unzip_bin="/usr/local/bin/unzip"
  83. wget_curl="/usr/local/bin/wget"
  84. ccd2iso_bin="/usr/bin/ccd2iso"
  85. mkvdts2ac3_bin="/path/to/mkvdts2ac3.sh"
  86. ##################### Supported file extensions - Comma separated ################
  87. ########### You must have at least one extension enabled in each field ###########
  88. ############### DON T ADD RAR OR ZIP EXTENSIONS IN THESE FIELDS ##################
  89. supported_extensions="avi,mkv,divx,mp4,ts,iso,img,mp3,m4a,wav,srt,idx,sub,dvd"
  90. tv_show_extensions="avi,mkv,divx,mp4,srt,idx,sub"
  91. movies_extensions="avi,mkv,divx,mp4,ts,iso,img,srt,idx,sub"
  92. music_extensions="mp3,m4a,wav"
  93. ##################### Movies detection patterns - Comma separated ################
  94. ############# You must have at least one pattern enabled in each field ###########
  95. # movies_detect_patterns and movies_detect_patterns_pt_2 are the same - only splitted
  96. # scene patterns is used for scenes that add their name at the beginning of the file name
  97. movies_detect_patterns="HDTV,DVDRip,BDRip,BRRip,DVDR,576p,3D HSBS 720p,720p,3D HSBS 1080p,1080p,HD1080p"
  98. movies_detect_patterns_pt_2="TS,PPVrip,TVRip,DVDSCR,R5,Workprint,SCR,Screener,HDRip,DVDScreener"
  99. other_movies_patterns="pdtv,hdtv,xvid,webrip,web-dl,readnfo,ntsc,pal,ws,uncut,unrated,internal,480p,festival,bluray,extended,italian,dubbed,collection,season,nlsubs,spanish,divx,x264,hdtvrip,xxx,custom[. _-].*[. _-]subs,[^ ].*[. _-]subs,plsub,subtit,tsxvid,plsubbed,subbed,multisubs,multi-subs,fansub,retail,telesync,telecine,dvb,swesub,vostfr,3d,sbs,dvd,eng,dvd5,dvd9,torrent,torrents,www,x264,dvdripspanish,half-sbs,full-sbs"
  100. scene_patterns="\[[. _-]*www[. _-].*[. _-].*[. _-]*\],aaf,dvdriptorrents,skidrow,klaxxon,axxo,vomit,dita,omifast,extratorrent,2lions,fxm,duqa,newartriot,nhanc3,ddc,keltz,\[fileking[. _-]pl\],fqm,eztv,limited,real[. _-]proper,proper,repack,resync,syncfix,rerip,theatrical.cut,remastered,convert,republic"
  101. audio_quality_patterns="AC3,DTS,LiNE,CAM AUDIO,LD,Studio Audio"
  102. ####################### Optional functionalities variables #######################
  103. #################### Set these variables to "no" to disable ######################
  104. ## Fix numbering for TV Shows - Switch variable to "yes" to enable
  105. tv_shows_fix_numbering="yes"
  106. ## Cleanup Filenames - Switch variable to "yes" to enable
  107. ## 3 different schemas - type_1 = Movie (year).ext - type_2 = Movie Year (video_quality).ext - type_3 = Movie Year (audio_quality-video_quality).ext
  108. clean_up_filenames="yes"
  109. movies_rename_schema="type_1"
  110. ## Keep a dummy video file with the original filename for subtitles retrieval
  111. ## A torrentexpander_subtitles_dir directory will be created in the same directory as the script
  112. ## Fetch subtitles for files in this directory and on next run, torrentexpander will try to move them
  113. ## In the same directory as your movies
  114. subtitles_handling="no"
  115. ## Repack handling - Switch variable to "yes" to enable
  116. ## Only useful if you want to be able to recognize repacks
  117. repack_handling="no"
  118. ## Create Wii Cuesheet - Switch variable to "yes" to enable
  119. ## Will generate a CloneCD cuesheet for Wii backups
  120. wii_post="no"
  121. ## Convert img to iso - Switch variable to "yes" to enable
  122. ## Will use CCD2ISO to convert .img disk images to .iso
  123. img_post="no"
  124. ## Copy or move TV Shows to a specific folder - choose action (copy / move)
  125. ## and add path to enable.
  126. ## Using tv_shows_post_path_mode, series files can also be sorted by /Series/Episode (s)
  127. ## or /Series/Season X/Episode (ss) or /Series/Season XX/Episode (sss)
  128. tv_shows_post="no"
  129. tv_shows_post_path="no"
  130. tv_shows_post_path_mode="no"
  131. ## Copy or move movies to a specific folder - choose action (copy / move)
  132. ## and add path to enable.
  133. ## You can also force folder creation for single file movies by setting force_single_file_movies_folder to yes
  134. movies_post="no"
  135. movies_post_path="no"
  136. force_single_file_movies_folder="no"
  137. ## Copy or move music to a specific folder - choose action (copy / move)
  138. ## and add path to enable
  139. music_post="no"
  140. music_post_path="no"
  141. ## IMDB Integration - Mainly for NetworkedMediaTank
  142. ## Torrentexpander can generate a .nfo with imdb URL, dowonload a poster and download a fanart
  143. ## Id like to thank Loginbug for suggesting these imdb features and providing huge chunks of code
  144. imdb_poster="no"
  145. # Poster format could be: normal, large, small or full
  146. imdb_poster_format="normal"
  147. imdb_nfo="no"
  148. imdb_fanart="no"
  149. # Fanart format could be: thumb, poster, w1280, original
  150. imdb_fanart_format="w1280"
  151. # Disable NMJ scan it IMDB lookup fails
  152. disable_nmj_scan="no"
  153. ## Convert DTS track from MKV files to AC3 - Check mkvdts2ac3.sh path and switch variable to "yes" to enable
  154. ## The DTS track will be kept and the AC3 track will be added
  155. dts_post="no"
  156. ## Edit files and folders permissions - If you dont know what that means set it all to "no"
  157. ## If you don t want or can t use sudo, these features will be deprecated
  158. user_perm_post="no"
  159. group_perm_post="no"
  160. files_perm_post="no"
  161. folder_perm_post="no"
  162. edit_perm_as_sudo="no"
  163. ## Use a source / resulting files log shared with a third party app - Add path to enable
  164. ## Take a look at the wiki to learn more about it
  165. third_party_log="no"
  166. ## Reset timestamp (mtime)
  167. reset_timestamp="no"
  168. ## Debug mode (only used in a few routines) - If enabled, it will be named torrentexpander_debug.log
  169. ## and stored in the same directory as the script
  170. debug_mode="no"
  171. ## Auto Update script with latest svn release
  172. ## values can be "no", "daily" or "weekly"
  173. auto_update_script="no"
  174. ## In destructive_mode, torrentexpander can pause then remove torrent and data from your transmission torrent client
  175. remove_torrent_from_client="no"
  176. torrent_daemon_bin="/usr/local/bin/transmission-remote"
  177. torrent_daemon_login=""
  178. torrent_daemon_password=""
  179. torrent_daemon_port="9091"
  180. ## Run Script - Will be run once torrentexpander is done
  181. ## You can input the path to a script of your choice or just type the name of a binary available in your PATH
  182. ## 3 types of script can be run by torrentexpander :
  183. ## - all_files_script sends every processed file to the script
  184. ##      -> set any variable to file_path to send path to your script as $1, $2, $3, $4 or $5
  185. ##      -> set any variable to name_without_extension to send filename without extension to your script as $1, $2, $3, $4 or $5
  186. ##      -> set any variable to name_with_extension to send filename with extension to your script as $1, $2, $3, $4 or $5
  187. ##      -> set any variable to imdb_title to send IMDB title to your script as $1, $2, $3, $4 or $5
  188. ##      -> set any variable to imdb_id to send IMDB ID to your script as $1, $2, $3, $4 or $5
  189. ##      -> set any variable to imdb_url to send IMDB URL to your script as $1, $2, $3, $4 or $5
  190. ##      -> set any variable to imdb_poster_url to send IMDB Poster URL to your script as $1, $2, $3, $4 or $5
  191. ##      -> set any variable to tmdb_fanart_url to send TMDB Fanart URL to your script as $1, $2, $3, $4 or $5
  192. ## - processed_torrent_script sends only the resulting file or directory to the script
  193. ##              -> post tasks will then switch from copy to move mode
  194. ##      -> set any variable to file_path to send path to your script as $1, $2, $3, $4 or $5
  195. ##      -> set any variable to name_without_extension to send filename without extension to your script as $1, $2, $3, $4 or $5
  196. ##      -> set any variable to name_with_extension to send filename with extension to your script as $1, $2, $3, $4 or $5
  197. ##      -> set any variable to imdb_title to send IMDB title to your script as $1, $2, $3, $4 or $5
  198. ##      -> set any variable to imdb_id to send IMDB ID to your script as $1, $2, $3, $4 or $5
  199. ##      -> set any variable to imdb_url to send IMDB URL to your script as $1, $2, $3, $4 or $5
  200. ##      -> set any variable to imdb_poster_url to send IMDB Poster URL to your script as $1, $2, $3, $4 or $5
  201. ##      -> set any variable to tmdb_fanart_url to send TMDB Fanart URL to your script as $1, $2, $3, $4 or $5
  202. ## - post_run_script only triggers a script without sending any variable
  203. ###
  204. all_files_script_enabled="no"
  205. all_files_script=""
  206. all_files_script_variable_1=""
  207. all_files_script_variable_2=""
  208. all_files_script_variable_3=""
  209. all_files_script_variable_4=""
  210. all_files_script_variable_5=""
  211. ###
  212. processed_torrent_script_enabled="no"
  213. processed_torrent_script=""
  214. processed_torrent_script_variable_1=""
  215. processed_torrent_script_variable_2=""
  216. processed_torrent_script_variable_3=""
  217. processed_torrent_script_variable_4=""
  218. processed_torrent_script_variable_5=""
  219. ###
  220. post_run_script_enabled="no"
  221. post_run_script=""
  222. ###
  223. ############################ END USER VARIABLES ##################################
  224. ##################################################################################
  225.  
  226.  
  227.  
  228.  
  229. ##################################################################################
  230. ## Interpreting commandline options
  231. commandline_arguments="$(echo -e "$*" | sed 's; -\([0-9a-zA-Z-]\);\\\n-\1;g' | sed 's; /\([0-9a-zA-Z-]\);\\\n/\1;g' )"
  232. for arg in $(echo -e "$commandline_arguments"); do
  233.         # Making sure the torrent fed into torrentexpander is a file or directory
  234.         if [[ -f "$arg" || -d "$arg" ]] && [[ ! -f "$torrent" && ! -d "$torrent" ]]; then torrent=`echo "$arg"` && commandline_arguments="$(echo "$commandline_arguments" | egrep -v "^$arg$")";
  235.         # alt_destination will be used instead of destination_folder if you launch the script using "/path/to/torrentexpander.sh /torrent /destination"
  236.         elif [[ -d "$arg" ]] && [[ -f "$torrent" || -d "$torrent" ]]; then alt_dest_enabled="yes" && alt_destination=`echo "$arg"` && commandline_arguments="$(echo "$commandline_arguments" | egrep -v "^$arg$")";
  237.         fi
  238. done
  239. for arg in $(echo -e "$commandline_arguments"); do
  240.         # if the script is run for the first time or using "torrentexpander -c", launch setup
  241.         if [[ "$arg" == "-c" || "$arg" == "--config" ]]; then first_run="yes";
  242.         # Commandline arguments help
  243.         elif [[ "$arg" == "-h" || "$arg" == "--help" ]] && [ -t 1 ]; then echo -e "\n\nAllowed commands are :\n\n-c  or --config            ->   Launch Setup\n-h  or --help              ->   Help\n-d  or --destructive       ->   Destructive Mode\n-nd or --non-destructive   ->   Disable Destructive Mode\n-s  or --silent            ->   Silent Mode (default if no display is available)\n-u  or --update            ->   Manual Update Mode\n\nFirst Path is your Torrent Path\nSecond Path is an Optional Destination Path of your choice\n\n" && exit;
  244.         # Force destructive or non-destructive mode
  245.         elif [[ "$arg" == "-d" || "$arg" == "--destructive" ]]; then force_destructive="yes";
  246.         elif [[ "$arg" == "-nd" || "$arg" == "--non-destructive" ]]; then force_destructive="no";
  247.         # if a torrent path is passed in commandline, we allow silent mode
  248.         elif [[ "$arg" == "-s" || "$arg" == "--silent" ]] && [[ -f "$torrent" || -d "$torrent" ]]; then silent_mode="yes";
  249.         # Manually trigger script update
  250.         elif [[ "$arg" == "-u" || "$arg" == "--update" ]]; then update_mode="yes" && silent_mode="yes";
  251.         fi
  252. done
  253.  
  254. ##################################################################################
  255. ## Gathering more info about the environment
  256. # This routine is used to detect if the script can display output
  257. # while in subtitles_mode, we never enable display
  258. if [ -t 1 ] && [ "$subtitles_mode" != "yes" ] && [ "$silent_mode" != "yes" ]; then has_display="yes"; fi
  259. # we ll remind the user about the help command
  260. if [[ ! "$*" &&  "$has_display" == "yes" ]]; then echo -e "\n\nUse the -h or --help command if you need to see a list of all available commands\n\n"; fi
  261.  
  262. # Detect if the OS handles the nice command
  263. nice -n 15 echo > /dev/null 2>&1 && if [ "$?" == "0" ]; then nice_available="yes"; fi
  264.  
  265. ##################################################################################
  266. ## Save variables to another file. Updating the script will be less painful
  267.  
  268. # Detecting script path
  269. PRG="$0"
  270. while [ -h "$PRG" ] ; do
  271.         ls=`ls -ld "$PRG"`
  272.         link=`expr "$ls" : '.*-> \(.*\)$'`
  273.         if expr "$link" : '/.*' > /dev/null; then
  274.                 PRG="$link"
  275.         else
  276.                 PRG="`dirname -- "$PRG"`/$link"
  277.         fi
  278. done
  279. script_path=`dirname -- "$PRG"`
  280.  
  281. # settings_file and debug_log will be stored in the same directory as the script
  282. settings_file="$script_path/torrentexpander_settings.ini"
  283. debug_log="$script_path/torrentexpander_debug.log"
  284.  
  285. # Defining path to subtitles directory. If run from Mac OS X Services, this feature will be disabled
  286. subtitles_directory="$script_path/torrentexpander_subtitles_dir"
  287. if [[ "$script_path" == *torrentexpander.workflow* ]]; then subtitles_handling="no"; fi
  288.  
  289. ##################################################################################
  290. ######################### Script setup user interface ############################
  291.  
  292. # Switch to setup mode if settings file is missing and used interaction is possible
  293. if [ -t 1 ] && [ ! -f "$settings_file" ]; then first_run="yes" && has_display="yes"; fi
  294.  
  295. # Generating settings file
  296. if [ ! -f "$settings_file" ]; then
  297.         touch "$settings_file"
  298. fi
  299.  
  300. # Detecting if gnu sed is available. Else switch to BSD sed routines
  301. # sed -i will then be replaced by sed -i ''
  302. sed -i "s;^this_string_should_not_be_there$;^$;g" "$settings_file" > /dev/null 2>&1; if [ "$?" == "0" ]; then gnu_sed_available="yes"; fi
  303.  
  304. # Copying content of settings to a variable
  305. check_settings=$(echo "$(cat "$settings_file")")
  306.  
  307.  
  308. ## Trying to guess default paths
  309. # If destination_folder is not defined or unavailable, switch to Desktop on Mac and Ubuntu
  310. # On NetworkedMediaTank, the script will use /share/Downloads/Expanded or /share/Download/Expanded
  311. if [[ "$check_settings" != *estination_folder=* || "$check_settings" == *estination_folder=incorrect_or_not_se* ]]; then
  312.         # This line should work on most unix systems
  313.         if [ ! -d "$destination_folder" ] && [ -d "$HOME/Desktop" ]; then destination_folder="$HOME/Desktop"; fi
  314.         # This line is for ubuntu systems when the desktop has a language specific name
  315.         if [ ! -d "$destination_folder" ]; then xdg-user-dir > /dev/null 2>&1 && if [ "$?" == "0" ]; then destination_folder="$(xdg-user-dir DESKTOP)"; fi; fi
  316.         # This line is for PopCornHour media players
  317.         if [ ! -d "$destination_folder" ] && [ -d "/share/Downloads" ]; then destination_folder="/share/Downloads/Expanded";
  318.         elif [ ! -d "$destination_folder" ] && [ -d "/share/Download" ]; then destination_folder="/share/Download/Expanded";
  319.         fi
  320. fi
  321.  
  322. # Looking for unrar in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  323. if [[ "$check_settings" != *nrar_bin=* || "$check_settings" == *nrar_bin=incorrect_or_not_se* ]]; then
  324.         if [ ! -x "$unrar_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name unrar; fi; done | sed -n -e '1p')" ]; then unrar_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name unrar; fi; done | sed -n -e '1p')"; fi
  325.         # If unrar is unavailable, switch back to 7z
  326.         if [ ! -x "$unrar_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name 7z; fi; done | sed -n -e '1p')" ]; then unrar_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name 7z; fi; done | sed -n -e '1p')"; fi
  327. fi
  328.  
  329. # Looking for unzip in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  330. if [[ "$check_settings" != *nzip_bin=* || "$check_settings" == *nzip_bin=incorrect_or_not_se* ]]; then
  331.         if [ ! -x "$unzip_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name unzip; fi; done | sed -n -e '1p')" ]; then unzip_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name unzip; fi; done | sed -n -e '1p')"; fi
  332.         # If unzip is unavailable, switch back to 7z
  333.         if [ ! -x "$unzip_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name 7z; fi; done | sed -n -e '1p')" ]; then unzip_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name 7z; fi; done | sed -n -e '1p')"; fi
  334. fi
  335.  
  336. # Looking for wget or curl in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  337. if [[ "$check_settings" != *get_curl=* || "$check_settings" == *get_curl=incorrect_or_not_se* ]]; then
  338.         # Looking for wget
  339.         if [ ! -x "$wget_curl" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name wget; fi; done | sed -n -e '1p')" ]; then wget_curl="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name wget; fi; done | sed -n -e '1p')"; fi
  340.         # If wget is unavailable, switch back to curl
  341.         if [ ! -x "$wget_curl" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name curl; fi; done | sed -n -e '1p')" ]; then wget_curl="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name curl; fi; done | sed -n -e '1p')"; fi
  342. fi
  343.  
  344. # Looking for ccd2iso in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  345. if [[ "$check_settings" != *cd2iso_bin=* || "$check_settings" == *cd2iso_bin=incorrect_or_not_se* ]]; then
  346.         if [ ! -x "$ccd2iso_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name ccd2iso; fi; done | sed -n -e '1p')" ]; then ccd2iso_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name ccd2iso; fi; done | sed -n -e '1p')"; fi
  347. fi
  348.  
  349. # Looking for a text editor in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  350. if [[ "$check_settings" != *ext_editor_bin=* || "$check_settings" == *ext_editor_bin=incorrect_or_not_se* ]]; then
  351.         if [ ! -x "$text_editor_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name nano; fi; done | sed -n -e '1p')" ]; then text_editor_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name nano; fi; done | sed -n -e '1p')"; fi
  352.         # If nano is unavailable, switch back to vi
  353.         if [ ! -x "$text_editor_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name vi; fi; done | sed -n -e '1p')" ]; then text_editor_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name vi; fi; done | sed -n -e '1p')"; fi
  354. fi
  355.  
  356. # Looking for a torrent client in the PATH variable or /Applications /nmt/apps /usr/local/bin directories
  357. if [[ "$check_settings" != *orrent_daemon_bin=* || "$check_settings" == *orrent_daemon_bin=incorrect_or_not_se* ]]; then
  358.         if [ ! -x "$torrent_daemon_bin" ] && [ -x "$(for d in $(echo -e "/share/Apps/Transmission/bin\n/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name transmission-remote; fi; done | sed -n -e '1p')" ]; then torrent_daemon_bin="$(for d in $(echo -e "/share/Apps/Transmission/bin\n/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name transmission-remote; fi; done | sed -n -e '1p')"; fi
  359.         # If transmission-remote is unavailable we give xmlrpc a try
  360.         if [ ! -x "$torrent_daemon_bin" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name xmlrpc; fi; done | sed -n -e '1p')" ]; then torrent_daemon_bin="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name xmlrpc; fi; done | sed -n -e '1p')"; fi
  361. fi
  362.  
  363. # Looking for all_files_script in your PATH variable or /Applications /nmt/apps /usr/local/bin directories
  364. if [ "$all_files_script" ] && [[ "$check_settings" != *ll_files_script=* || "$check_settings" == *ll_files_script=incorrect_or_not_se* ]]; then
  365.         if [ ! -x "$all_files_script" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$all_files_script"; fi; done | sed -n -e '1p')" ]; then all_files_script="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$all_files_script"; fi; done | sed -n -e '1p')"; fi
  366. fi
  367.  
  368. # Looking for processed_torrent_script in your PATH variable or /Applications /nmt/apps /usr/local/bin directories
  369. if [ "$processed_torrent_script" ] && [[ "$check_settings" != *rocessed_torrent_script=* || "$check_settings" == *rocessed_torrent_script=incorrect_or_not_se* ]]; then
  370.         if [ ! -x "$processed_torrent_script" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$processed_torrent_script"; fi; done | sed -n -e '1p')" ]; then processed_torrent_script="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$processed_torrent_script"; fi; done | sed -n -e '1p')"; fi
  371. fi
  372.  
  373. # Looking for post_run_script in your PATH variable or /Applications /nmt/apps /usr/local/bin directories
  374. if [ "$post_run_script" ] && [[ "$check_settings" != *ost_run_script=* || "$check_settings" == *ost_run_script=incorrect_or_not_se* ]]; then
  375.         if [ ! -x "$post_run_script" ] && [ -x "$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$post_run_script"; fi; done | sed -n -e '1p')" ]; then post_run_script="$(for d in $(echo -e "/Applications\n/usr/local/bin\n/nmt/apps\n/bin\n/usr/bin\n/share/Apps/TorrentExpander/bin/\n$(echo -e "$PATH" | sed "s;:;\\\n;g")"); do if [ -d "$d" ]; then find "$d" -maxdepth 2 -name "$post_run_script"; fi; done | sed -n -e '1p')"; fi
  376. fi
  377.  
  378. # Inserting path to binaries into the settings file
  379. for c in $(echo -e "unrar_bin\nunzip_bin\nwget_curl\nccd2iso_bin\ntext_editor_bin\nmkvdts2ac3_bin\ntorrent_daemon_bin\nall_files_script\nprocessed_torrent_script\npost_run_script"); do
  380.         pat="$(echo "$c" | sed "s;^.\(.*\)$;\*\1=\*;")"
  381.         pat_two="$(echo "$c" | sed "s;^.\(.*\)$;\*\1=incorrect_or_not_se\*;")"
  382.         if [[ "$check_settings" != $pat && -x "${!c}" ]] || [[ "$check_settings" == $pat_two && -x "${!c}" ]]; then
  383.                 if [[ "$check_settings" == $pat_two && "$gnu_sed_available" != "yes" ]]; then sed -i '' "/$c=/d" "$settings_file"; fi;
  384.                 if [[ "$check_settings" == $pat_two && "$gnu_sed_available" == "yes" ]]; then sed -i "/$c=/d" "$settings_file"; fi;
  385.                 echo "$c=${!c}" >> "$settings_file"
  386.         elif [[ "$check_settings" != $pat ]]; then echo "$c=incorrect_or_not_set" >> "$settings_file"
  387.         fi
  388. done
  389.  
  390. # Inserting destinations paths into settings file
  391. for c in $(echo -e "destination_folder\ntv_shows_post_path\nmovies_post_path\nmusic_post_path"); do
  392.         pat="$(echo "$c" | sed "s;^.\(.*\)$;\*\1=\*;")"
  393.         pat_two="$(echo "$c" | sed "s;^.\(.*\)$;\*\1=n\*;")"
  394.         if [[ "$check_settings" != $pat && -d "${!c}" ]] || [[ "$check_settings" == $pat_two && -d "${!c}" ]]; then
  395.                 if [[ "$check_settings" == $pat_two && "$gnu_sed_available" != "yes" ]]; then sed -i '' "/$c=/d" "$settings_file"; fi;
  396.                 if [[ "$check_settings" == $pat_two && "$gnu_sed_available" == "yes" ]]; then sed -i "/$c=/d" "$settings_file"; fi;
  397.                 echo "$c=${!c}" >> "$settings_file"
  398.         elif [[ "$check_settings" != $pat ]]; then echo "$c=no" >> "$settings_file"
  399.         fi
  400. done
  401.  
  402. # Adding path to third party log into settings file
  403. if [ "$third_party_log" != "no" ]; then third_party_log_directory="$(echo "$(dirname "$third_party_log")")"; fi
  404. if [[ "$check_settings" != *hird_party_log=* && -d "$third_party_log_directory" ]] || [[ "$check_settings" == *hird_party_log=n* && -d "$third_party_log_directory" ]]; then
  405.         if [[ "$check_settings" == *hird_party_log=n* && "$gnu_sed_available" != "yes" ]]; then sed -i '' "/third_party_log=/d" "$settings_file"; fi;
  406.         if [[ "$check_settings" == *hird_party_log=n* && "$gnu_sed_available" == "yes" ]]; then sed -i "/third_party_log=/d" "$settings_file"; fi;
  407.         echo "third_party_log="$third_party_log"" >> "$settings_file";
  408. elif [[ "$check_settings" != *hird_party_log=* ]]; then echo "third_party_log=no" >> "$settings_file"
  409. fi
  410.  
  411. # Adding other values in settings file
  412. for c in $(echo -e "destructive_mode\nuser_defined_other_movies_patterns\nuser_defined_scene_patterns\nuser_defined_scene_patterns_hothead_edition\ntv_shows_fix_numbering\nclean_up_filenames\nmovies_rename_schema\nsubtitles_handling\nrepack_handling\nwii_post\nimg_post\ntv_shows_post\ntv_shows_post_path_mode\nmovies_post\nforce_single_file_movies_folder\nmusic_post\nimdb_poster\nimdb_poster_format\nimdb_nfo\nimdb_fanart\nimdb_fanart_format\ndisable_nmj_scan\ndts_post\nuser_perm_post\ngroup_perm_post\nfiles_perm_post\nfolder_perm_post\nedit_perm_as_sudo\nreset_timestamp\nsupported_extensions\ntv_show_extensions\nmovies_extensions\nmusic_extensions\ndebug_mode\nauto_update_script\nremove_torrent_from_client\ntorrent_daemon_login\ntorrent_daemon_password\ntorrent_daemon_port\nall_files_script_enabled\nall_files_script_variable_1\nall_files_script_variable_2\nall_files_script_variable_3\nall_files_script_variable_4\nall_files_script_variable_5\nprocessed_torrent_script_enabled\nprocessed_torrent_script_variable_1\nprocessed_torrent_script_variable_2\nprocessed_torrent_script_variable_3\nprocessed_torrent_script_variable_4\nprocessed_torrent_script_variable_5\npost_run_script_enabled"); do
  413.         pat="$(echo "$c" | sed "s;^.\(.*\)$;\*\1=\*;")"
  414.         if [[ "$check_settings" != $pat ]]; then echo "$c=${!c}" >> "$settings_file"; fi
  415. done
  416.  
  417. # Removing patterns from settings file generated by a previous version of the script
  418. if [[ "$check_settings" == *ovies_detect_patterns=* && "$gnu_sed_available" != "yes" ]]; then sed -i '' "/movies_detect_patterns=/d" "$settings_file"; fi
  419. if [[ "$check_settings" == *ovies_detect_patterns=* && "$gnu_sed_available" == "yes" ]]; then sed -i "/movies_detect_patterns=/d" "$settings_file"; fi
  420.  
  421. # Add quotes in settings file
  422. if [ "$(echo "$check_settings" | egrep -i "([\\]) ")" ] && [ "$gnu_sed_available" != "yes" ]; then sed -i '' 's;\\ ; ;g' "$settings_file"; fi
  423. if [ "$(echo "$check_settings" | egrep -i "([\\]) ")" ] && [ "$gnu_sed_available" == "yes" ]; then sed -i 's;\\ ; ;g' "$settings_file"; fi
  424. if [ "$(echo "$check_settings" | egrep -i "([^\"]) ")" ] && [ "$gnu_sed_available" != "yes" ]; then sed -i '' 's;^\(.*\)=\([^"]\)\(.*\)\([^"]\)$;\1="\2\3\4";g' "$settings_file"; fi
  425. if [ "$(echo "$check_settings" | egrep -i "([^\"]) ")" ] && [ "$gnu_sed_available" == "yes" ]; then sed -i 's;^\(.*\)=\([^"]\)\(.*\)\([^"]\)$;\1="\2\3\4";g' "$settings_file"; fi
  426.  
  427. # Fetching values from settings file
  428. source "$settings_file"
  429.  
  430. # Adding trailing slash to destination paths
  431. if [[ "$tv_shows_post_path" != */ ]] && [ "$tv_shows_post" != "no" ]; then tv_shows_post_path="$tv_shows_post_path/"; fi
  432. if [[ "$music_post_path" != */ ]] && [ "$music_post_path" != "no" ]; then music_post_path="$music_post_path/"; fi
  433. if [[ "$movies_post_path" != */ ]] && [ "$movies_post_path" != "no" ]; then movies_post_path="$movies_post_path/"; fi
  434.  
  435. # Override destructive-mode setting using commandline argument
  436. if [ "$force_destructive" == "yes" ]; then destructive_mode="yes"; elif [ "$force_destructive" == "no" ]; then destructive_mode="no"; fi
  437.  
  438. # Making patterns regexp friendly
  439. supported_extensions_rev="\.$(echo $supported_extensions | sed 's;,;\$\|\\\.;g')$"
  440. tv_show_extensions_rev="\.$(echo $tv_show_extensions | sed 's;,;\$\|\\\.;g')$"
  441. movies_extensions_rev="\.$(echo $movies_extensions | sed 's;,;\$\|\\\.;g')$"
  442. music_extensions_rev="\.$(echo $music_extensions | sed 's;,;\$\|\\\.;g')$"
  443. movies_detect_patterns_rev="[^[:alnum:]]$(echo $movies_detect_patterns | sed 's;,;[^[:alnum:]]|[^[:alnum:]];g')[^[:alnum:]]"
  444. movies_detect_patterns_pt_2_rev="[^[:alnum:]]$(echo $movies_detect_patterns_pt_2 | sed 's;,;[^[:alnum:]]|[^[:alnum:]];g')[^[:alnum:]]"
  445. other_movies_patterns="$other_movies_patterns,$audio_quality_patterns,$user_defined_other_movies_patterns"
  446. scene_patterns="$scene_patterns,$user_defined_scene_patterns"
  447.  
  448. ##################################################################################
  449. ############################### Setup Assistant ##################################
  450. if [ "$first_run" == "yes" ] && [ "$has_display" == "yes" ]; then echo -e "----------------------------------------------------\n----------------------------------------------------\n\nWELCOME TO TORRENTEXPANDER\n\n----------------------------------------------------\n----------------------------------------------------\n\n"; fi
  451. if [ "$first_run" == "yes" ] && [ "$has_display" == "yes" ]; then echo -e "This is the first time you're running this script\nA few settings are required for it to run\nThese required settings are :\n- The destination_folder -> This is where the content of your torrents will be expanded / copied\n- unrar_bin -> This is the path to the Unrar binary. If it's not already installed on your computer then Google is your friend\n- unzip_bin -> This is the path to the Unrar binary. It's probably already installed on your computer\n\nAll other options are already set to their default value\nIf you want more details about those options, open this script with a text editor\n\nA nano or vi text editor will now open so that you can edit your settings\nTo save them in nano you'll have to press Control-X then Y then Enter\nTo save them in vi you'll have to press Escape then type :wq then press Enter\n\nOnce you're ready press Enter" && read -p ""; fi
  452. if [ "$first_run" == "yes" ] && [ "$has_display" == "yes" ]; then "$text_editor_bin" "$settings_file" && echo -e "\n\nYou're done with your setup\nThis script will exit now\nIf you need to edit your settings again just run $script_path/torrentexpander.sh -c" && exit; fi
  453.  
  454. ##################################################################################
  455. ###################### Kinda graphical user interface ############################
  456. if [[ "$has_display" == "yes" && ! "$torrent" ]] || [[ "$has_display" == "yes" && ! "$alt_dest_enabled" ]]; then echo -e "\n\n----------------------------------------------------\n----------------------------------------------------\n\nWELCOME TO TORRENTEXPANDER\n\n----------------------------------------------------\n----------------------------------------------------\n\n"; fi
  457.  
  458. ## Trying to find out transmission download-dir from transmission settings file - Temporarily disabled until I find a better one
  459. transmission_settings_file="$(if [ -f "/share/Apps/Transmission/config/settings.json" ]; then echo "/share/Apps/Transmission/config/settings.json"; elif [ -f "/share/.transmission/settings.json" ]; then echo "/share/.transmission/settings.json"; elif [ -f "$HOME/.config/transmission/settings.json" ]; then echo "$HOME/.config/transmission/settings.json"; fi)"
  460. if [ "$has_display" == "yes" ] && [ -f "$transmission_settings_file" ] && [ ! "$torrent" ]; then cd "$(echo "$(sed -n '/"download-dir": /p' "$transmission_settings_file")" | sed -e 's/    "download-dir": "//g' -e 's/", //g')"; elif [ "$has_display" == "yes" ] && [ ! -f "$transmission_settings_file" ] && [ ! "$torrent" ]; then cd "$HOME"; fi
  461.  
  462. ## Asking the User for the torrent source
  463. selected=0
  464. item_selected=""
  465. if [ "$has_display" == "yes" ] && [ ! "$torrent" ]; then
  466.         while [[ $selected -eq 0 ]] ; do
  467.                 count=-1 && echo "Select Torrent Source :" && echo "" && echo "$(pwd)" && echo ""
  468.                 # Listing content of directory in the GUI
  469.                 for item in $(echo -e "Select current folder\n..\n$(ls -1)"); do
  470.                         count=$(( $count + 1 )); var_name="sel$count"; var_val="$item"; eval ${var_name}=`echo -e \""${var_val}"\"`; echo "$count - $item"
  471.                 done
  472.                 echo "" && echo "Type the ID of the Torrent Source :"
  473.                 # Asking user for its selection
  474.                 read answer && sel_item="$(echo "sel$answer")"
  475.                 item_selected=${!sel_item}
  476.                 # If selection is 0, current directory is selected as the source
  477.                 if [ "$item_selected" == "Select current folder" ]; then item_selected="$(pwd)" && selected=1
  478.                 # If selection is 1, switch to parent directory
  479.                 elif [ "$item_selected" == ".." ]; then cd "$(dirname $(pwd))"
  480.                 # Preventing an issue when climbing up to root directory
  481.                 elif [[ "$(pwd)" == "/" && -d "/$item_selected" ]]; then cd "/$item_selected"
  482.                 # cd to the selected directory
  483.                 elif [ -d "$(pwd)/$item_selected" ]; then cd "$(pwd)/$item_selected"
  484.                 # if item is a file, the item is then selected as the source
  485.                 elif [ -f "$(pwd)/$item_selected" ]; then item_selected="$(pwd)/$item_selected" && selected=1
  486.                 fi
  487.                 echo ""
  488.         done
  489. fi
  490.  
  491. # Display selected source
  492. if [ "$has_display" == "yes" ] && [ ! "$torrent" ] && [ "$item_selected" ] && [[ $selected -eq 1 ]]; then torrent="$item_selected" && echo "" && echo "Your File Source is $torrent" && echo "" && echo ""; fi
  493.  
  494. ## Asking the User for the torrent destination - A default destination can be set by inserting a gui_transmission_destination in the settings file
  495. if [ "$has_display" == "yes" ] && [ ! "$alt_dest_enabled" ] && [ ! "$alt_destination" ] && [ "$gui_transmission_destination" ]; then cd "$gui_transmission_destination"; elif [[ "$has_display" == "yes" && ! "$alt_dest_enabled" && ! "$alt_destination" && -d "$destination_folder" ]]; then cd "$destination_folder"; elif [ "$has_display" == "yes" ] && [ ! "$alt_dest_enabled" ] && [ ! "$alt_destination" ]; then cd "$HOME"; fi
  496. selected=0
  497. item_selected=""
  498. if [ "$has_display" == "yes" ] && [ ! "$alt_dest_enabled" ] && [ ! "$alt_destination" ]; then
  499.         while [[ $selected -eq 0 ]] ; do
  500.                 count=-1 && echo "" && echo "Select Destination Folder :" && echo "" && echo "$(pwd)" && echo ""
  501.                 # Listing content of directory in the GUI
  502.                 for item in $(echo -e "Select current folder\n..\n$(ls -1)"); do
  503.                         count=$(( $count + 1 )); var_name="sel$count"; var_val="$item"; eval ${var_name}=`echo -e \""${var_val}"\"`; echo "$count  -  $item"
  504.                 done
  505.                 echo "" && echo "Type the ID of the Destination Folder :"
  506.                 # Asking user for its selection
  507.                 read answer && sel_item="$(echo "sel$answer")"
  508.                 item_selected=${!sel_item}
  509.                 # If selection is 0, current directory is selected as the destination
  510.                 if [ "$item_selected" == "Select current folder" ]; then item_selected="$(pwd)" && selected=1
  511.                 # If selection is 1, switch to parent directory
  512.                 elif [ "$item_selected" == ".." ]; then cd "$(dirname $(pwd))"
  513.                 # Preventing an issue when climbing up to root directory
  514.                 elif [[ "$(pwd)" == "/" && -d "/$item_selected" ]]; then cd "/$item_selected"
  515.                 # cd to the selected directory
  516.                 elif [ -d "$(pwd)/$item_selected" ]; then cd "$(pwd)/$item_selected"
  517.                 # if item is a file, the item is then selected as the destination
  518.                 elif [ -f "$(pwd)/$item_selected" ]; then cd "$(pwd)"
  519.                 fi
  520.                 echo ""
  521.         done
  522. fi
  523.  
  524. # Display selected destination
  525. if [ "$has_display" == "yes" ] && [ ! "$alt_dest_enabled" ] && [ ! "$alt_destination" ] && [ "$item_selected" ] && [[ $selected -eq 1 ]]; then alt_dest_enabled="yes" && alt_destination="$item_selected" && echo "" && echo "Your Destination Folder is $alt_destination" && echo "" && echo ""; fi
  526.  
  527. if [ "$alt_dest_enabled" == "yes" ]; then destination_folder=`echo "$alt_destination"`; fi
  528. if [[ "$destination_folder" != */ ]]; then destination_folder="$destination_folder/"; fi
  529.  
  530. ## This temp folder is used for zip archives and dts conversion and will be
  531. ## automatically removed. By default it will be created in your destination folder
  532. torrentexpander_temp="torrentexpander_temp"
  533. temp_folder="$(echo "$destination_folder$torrentexpander_temp/")"
  534. temp_folder_without_slash="$(echo "$destination_folder$torrentexpander_temp")"
  535.  
  536. ##################################################################################
  537. ############################# TORRENT SOURCE SETUP ###############################
  538. ################# You probably dont need to do anything here #####################
  539. ## This script will use a variable named torrent if file, else cd variable torrent,
  540. ## else use transmission variables if file, else cd transmission variables,
  541. ## else use current folder
  542. if [ "$TR_TORRENT_NAME" ] && [ ! "$torrent" ]; then
  543.         torrent="$TR_TORRENT_DIR/$TR_TORRENT_NAME";
  544.         torrent_name="$TR_TORRENT_NAME";
  545. fi
  546. if [[ -f "$torrent" || -d "$torrent" ]] && [ -r "$torrent" ]; then
  547.         delete_third_party_log="yes"
  548.         if [ ! "$torrent_name" ] && [ "$torrent" ]; then torrent_name=`echo "$(basename "$torrent")"`; fi
  549.         if [[ -d "$torrent" && -r "$torrent" ]]; then cd "$torrent" && current_folder=`echo "$(pwd)"` && folder_short=`echo "$( basename "$(pwd)" )"` && torrent=""; fi
  550. elif [ "$third_party_log" != "no" ] && [[ -f "$third_party_log" && -w "$third_party_log" ]]; then
  551.         torrent="$(cat "$third_party_log")"
  552.         if [ ! "$torrent_name" ] && [ "$torrent" ]; then torrent_name=`echo "$(basename "$torrent")"`; fi
  553.         if [[ -d "$torrent" && -r "$torrent" ]]; then cd "$torrent" && current_folder=`echo "$(pwd)"` && folder_short=`echo "$( basename "$(pwd)" )"` && torrent=""; fi
  554. elif [ "$has_display" == "yes" ]; then
  555.         echo "I cannot detect any Torrent Source or permissions to this torrent are not set correctly - This script will exit" && exit
  556. elif [ "$update_mode" == "yes" ]; then echo > /dev/null 2>&1
  557. else exit
  558. fi
  559.  
  560. ######################### END TORRENT SOURCE SETUP ###############################
  561. ##################################################################################
  562.  
  563. ##################### CHECKING IF VARIABLES ARE CORRECT ##########################
  564. variables_check="Please check your script variables"
  565. temp_directory="$(echo "$(dirname "$temp_folder")")"
  566. third_party_log_directory="$(echo "$(dirname "$third_party_log")")"
  567. errors_file="$script_path/torrentexpander_errors.log"
  568. if [ "$torrent" ] && [ ! "$current_folder" ]; then
  569.         torrent_directory="$(echo "$(dirname "$torrent")/")"
  570. elif [ "$current_folder" ] && [ ! "$torrent" ]; then
  571.         torrent_directory="$(echo "$(dirname "$current_folder")/")"
  572. fi
  573.  
  574. # Deleting previous error file
  575. if [ -f "$errors_file" ]; then rm -f "$errors_file"; fi
  576.  
  577. # If variables are correct, notify the user directly or through an error file generated in the script directory
  578. # If some necessary variables are incorrect, the script will exit
  579. # If some optional variables are incorrect, the script will continue but those features will be disabled
  580.  
  581. if [ ! -d "$(dirname "$destination_folder")" ]; then echo "Your destination folder is incorrect please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "Your destination folder is incorrect please edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  582. if [[ -d "$(dirname "$destination_folder")" && ! -d "$destination_folder" && -w "$(dirname "$destination_folder")" ]]; then mkdir -p "$destination_folder"; fi
  583. if [[ ! -w "$destination_folder" || ! -d "$destination_folder" ]]; then echo "Permissions on your destination folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your destination folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder\n"; fi; quit_on_error="yes"; fi
  584.  
  585. ## This part of the script has been moved a bit later in the script
  586. # if [ ! -d "$temp_directory" ]; then echo "Your temp folder path is incorrect please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour temp folder path is incorrect please edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  587. # if [ -d "$temp_folder" ]; then echo "Temp folder already exists. Please delete it or edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nTemp folder already exists. Please delete it or edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  588. # if [[ ! -w "$temp_directory" ]]; then echo "Permissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder\n"; fi; quit_on_error="yes"; fi
  589.  
  590. if [ ! -d "$tv_shows_post_path" ] && [ "$tv_shows_post" != "no" ]; then echo "Your TV Shows path is incorrect - TV Shows Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your TV Shows path is incorrect - TV Shows Post will be disabled"; fi; tv_shows_post="no"; fi
  591. if [[ ! -w "$tv_shows_post_path" && "$tv_shows_post" != "no" ]]; then echo "Permissions on your TV Shows folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - TV Shows Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your TV Shows folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - TV Shows Post will be disabled\n"; fi; tv_shows_post="no"; fi
  592. if [[ "$tv_shows_post_path_mode" != "s" && "$tv_shows_post_path_mode" != "ss" && "$tv_shows_post_path_mode" != "sss" && "$tv_shows_post_path_mode" != "no" ]]; then echo "Your TV Shows path mode is incorrect - TV Shows Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your TV Shows path mode is incorrect - TV Shows Post will be disabled"; fi; tv_shows_post="no"; fi
  593.  
  594. if [ ! -d "$music_post_path" ] && [ "$music_post" != "no" ]; then echo "Your music path is incorrect - Music Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your music path is incorrect - Music Post will be disabled"; fi; music_post="no"; fi
  595. if [[ ! -w "$music_post_path" && "$music_post" != "no" ]]; then echo "Permissions on your Music folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - Music Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your Music folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - Music Post will be disabled\n"; fi; music_post="no"; fi
  596.  
  597. if [ ! -d "$movies_post_path" ] && [ "$movies_post" != "no" ]; then     echo "Your movies path is incorrect - Movies Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your movies path is incorrect - Movies Post will be disabled"; fi; movies_post="no"; fi
  598. if [[ ! -w "$movies_post_path" && "$movies_post" != "no" ]]; then echo "Permissions on your Movies folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - Movies Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your Movies folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder - Movies Post will be disabled\n"; fi; movies_post="no"; fi
  599.  
  600. if [ ! -d "$third_party_log_directory" ] && [ "$third_party_log" != "no" ]; then echo "Your third party log path is incorrect please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your third party log path is incorrect please edit your torrentexpander_settings.ini file"; fi; quit_on_error="yes"; fi
  601. if [ ! -w "$third_party_log_directory" ] && [ "$third_party_log" != "no" ]; then echo "Your third party log path permissions are incorrect please edit your torrentexpander_settings.ini file or your permissions" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour third party log path permissions are incorrect please edit your torrentexpander_settings.ini file or your permissions\n"; fi; quit_on_error="yes"; fi
  602.  
  603. if [ "$torrent_directory" == "$destination_folder" ] && [ "$destructive_mode" != "yes" ]; then echo "Your destination folder should be different from the one where your torrent is located. Please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour destination folder should be different from the one where your torrent is located. Please edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  604.  
  605. if [ ! -x "$unrar_bin" ]; then echo "Your Unrar path is incorrect or permissions are incorrect please edit your torrentexpander_settings.ini file or edit your permissions" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour Unrar path is incorrect or permissions are incorrect please edit your torrentexpander_settings.ini file or edit your permissions\n"; fi; quit_on_error="yes"; fi
  606.  
  607. if [ ! -x "$unzip_bin" ]; then echo "Your unzip path is incorrect or permissions are incorrect please edit your torrentexpander_settings.ini file or edit your permissions" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour Unzip path is incorrect or permissions are incorrect please edit your torrentexpander_settings.ini file or edit your permissions\n"; fi; quit_on_error="yes"; fi
  608.  
  609. if [ ! -x "$wget_curl" ] && [[ "$imdb_poster" == "yes" || "$imdb_nfo" == "yes" || "$imdb_fanart" == "yes" ]]; then echo "Path to wget or curl is incorrect - IMDB features will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Path to wget or curl is incorrect - IMDB features will be disabled"; fi; imdb_poster="no" && imdb_nfo="no" && imdb_fanart="no"; fi
  610.  
  611. if [ ! -x "$mkvdts2ac3_bin" ] && [ "$dts_post" == "yes" ]; then echo "Path to mkvdts2ac3.sh is incorrect - DTS Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPath to mkvdts2ac3.sh is incorrect - DTS Post will be disabled\n"; fi; dts_post="no"; fi
  612.  
  613. if [ ! -x "$ccd2iso_bin" ] && [ "$img_post" == "yes" ]; then echo "Path to ccd2iso is incorrect - IMG to ISO Post will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPath to ccd2iso is incorrect - IMG to ISO Post will be disabled\n"; fi; img_post="no"; fi
  614.  
  615. if [ ! -x "$torrent_daemon_bin" ] && [ "$destructive_mode" == "yes" ] && [ "$remove_torrent_from_client" == "yes" ]; then echo "Path to your torrent_daemon_bin is incorrect - Your torrent will not be removed from your torrent client" >> "$errors_file"; auto_delete_torrent="no"; fi
  616.  
  617. if [ ! -x "$all_files_script" ] && [ ! -x "$(which "$all_files_script")" ] && [ "$all_files_script_enabled" == "yes" ]; then echo "Path to your all_files_script is incorrect - This feature will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPath to your all_files_script is incorrect - This feature will be disabled\n"; fi; all_files_script_enabled="no"; fi
  618.  
  619. if [ ! -x "$processed_torrent_script" ] && [ ! -x "$(which "$processed_torrent_script")" ] && [ "$processed_torrent_script_enabled" == "yes" ]; then echo "Path to your processed_torrent_script is incorrect - This feature will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPath to your processed_torrent_script is incorrect - This feature will be disabled\n"; fi; processed_torrent_script_enabled="no"; fi
  620.  
  621. if [ ! -x "$post_run_script" ] && [ ! -x "$(which "$post_run_script")" ] && [ "$post_run_script_enabled" == "yes" ]; then echo "Path to your post_run_script is incorrect - This feature will be disabled" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPath to your post_run_script is incorrect - This feature will be disabled\n"; fi; post_run_script_enabled="no"; fi
  622.  
  623. if [[ "$supported_extensions_rev" == *rar* ]] || [[ "$tv_show_extensions_rev" == *rar* ]] || [[ "$movies_extensions_rev" == *rar* ]] || [[ "$music_extensions_rev" == *rar* ]] || [[ "$supported_extensions_rev" == *zip* ]] || [[ "$tv_show_extensions_rev" == *zip* ]] || [[ "$movies_extensions_rev" == *zip* ]] || [[ "$music_extensions_rev" == *zip* ]]; then echo "Your supported file extensions are incorrect please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo "Your supported file extensions are incorrect please edit your torrentexpander_settings.ini file"; fi; quit_on_error="yes"; fi
  624.  
  625. # If in third_party_log mode we turn tv_shows_post, music_post and movies_post from move to copy mode.
  626. if [[ "$third_party_log" != "no" && -f "$third_party_log" ]] || [[ "$processed_torrent_script_enabled" != "no" && -x "$processed_torrent_script" ]] || [[ "$processed_torrent_script_enabled" != "no" && -x "$(which "$processed_torrent_script")" ]]; then
  627.         if [ "$tv_shows_post" != "no" ]; then
  628.                 tv_shows_post="copy";
  629.         fi
  630.         if [ "$music_post" != "no" ]; then
  631.                 music_post="copy";
  632.         fi
  633.         if [ "$movies_post" != "no" ]; then
  634.                 movies_post="copy";
  635.         fi
  636. fi
  637.  
  638. if [ "$quit_on_error" == "yes" ]; then if [ "$has_display" == "yes" ]; then echo -e "\n\nThere's something wrong with your settings. Please review them now." && read -p "" && "$text_editor_bin" "$settings_file" && echo -e "\n\nYou're done with your setup\nThis script will exit now\nIf you need to edit your settings again just run $script_path/torrentexpander.sh -c"; elif [ "$update_mode" != "yes" ]; then exit; fi; fi
  639.  
  640.  
  641. ##################################################################################
  642.  
  643. # Starting to count steps in the script. Used only if there is a display
  644. step_number=0
  645.  
  646. ##################### CHECKING IF SCRIPT IS ALREADY RUNNING ######################
  647. script_notif="torrentexpander is running"
  648. log_file="$(echo "$destination_folder$script_notif")"
  649.  
  650. # Wait up to 240 x 15 seconds if the script is already running
  651. count=0
  652. while [ -f "$log_file" ]; do
  653.         if [ "$has_display" == "yes" ]; then echo "Waiting for another instance of the script to end . . . . . ."; fi
  654.         sleep 15; count=$(( count + 1 )); if [[ $count -gt 240 ]]; then rm "$log_file" && exit; fi
  655. done
  656.  
  657. # Generating log file that will be used all along the script
  658. if [ ! -f "$log_file" ]; then
  659.         touch "$log_file"
  660. fi
  661.  
  662.  
  663. ##################################################################################
  664.  
  665. if [ ! -d "$temp_directory" ]; then echo "Your temp folder path is incorrect please edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nYour temp folder path is incorrect please edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  666. if [ -d "$temp_folder" ]; then echo "Temp folder already exists. Please delete it or edit your torrentexpander_settings.ini file" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nTemp folder already exists. Please delete it or edit your torrentexpander_settings.ini file\n"; fi; quit_on_error="yes"; fi
  667. if [[ ! -w "$temp_directory" ]]; then echo "Permissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder" >> "$errors_file"; if [ "$has_display" == "yes" ]; then echo -e "\nPermissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder\n"; fi; quit_on_error="yes"; fi
  668.  
  669. # Creating temp folder
  670. mkdir -p "$temp_folder"
  671.  
  672. if [[ ! -w "$temp_folder" ]]; then
  673.         echo "Permissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder" >> "$errors_file"
  674.         if [ "$has_display" == "yes" ]; then echo "Permissions on your temp folder are incorrect please edit your torrentexpander_settings.ini file or your permissions for this folder"; fi
  675.         quit_on_error="yes"
  676. fi
  677.  
  678. if [ "$quit_on_error" == "yes" ]; then if [ "$has_display" == "yes" ]; then echo -e "\n\nThere's something wrong with your settings. Please review them now." && read -p "" && "$text_editor_bin" "$settings_file" && echo -e "\n\nYou're done with your setup\nThis script will exit now\nIf you need to edit your settings again just run $script_path/torrentexpander.sh -c"; elif [ "$update_mode" != "yes" ]; then exit; fi; fi
  679.  
  680.  
  681. ############################# SCRIPT AUTO UPDATE #################################
  682. if [ "$update_mode" == "yes" ] && [ -t 1 ]; then has_display="yes"; fi
  683. date_today=$(($(date "+%Y" | sed 's/^0*//')*365+$(date "+%m" | sed 's/^0*//')*30+$(date "+%d" | sed 's/^0*//')))
  684. if [ ! "$last_update" ]; then last_update=0; fi
  685.  
  686. if [[ "$wget_curl" == *wget* || "$wget_curl" == *curl* ]] && [[ "$auto_update_script" == "daily" && $last_update -lt $(($date_today-1)) ]] || [[ "$auto_update_script" == "weekly" && $last_update -lt $(($date_today-7)) ]] || [[ "$auto_update_script" == "monthly" && $last_update -lt $(($date_today-30)) ]] || [ "$update_mode" == "yes" ]; then
  687.         if [ "$has_display" == "yes" ]; then step_number=$(( $step_number + 1 )) && echo -e "Step $step_number : Updating Torrentexpander\n\n";  fi
  688.         if [[ "$wget_curl" == *wget* ]]; then
  689.                 rel_cont=`echo "$("$wget_curl" -q "http://code.google.com/p/torrentexpander/source/browse/trunk" -O -; wait)"`;
  690.         elif [[ "$wget_curl" == *curl* ]]; then
  691.                 rel_cont=`echo "$("$wget_curl" -silent -i "http://code.google.com/p/torrentexpander/source/browse/trunk"; wait)"`;
  692.         fi
  693.         release_vers="$(echo "$rel_cont" | egrep "trunk/torrentexpander.sh" | egrep ">[0-9][0-9][0-9]<" | sed "s;.*href=.trunk/torrentexpander.sh.>\([0-9][0-9][0-9]\)<.*;\1;")"
  694.         if [[ $current_version -eq $release_vers ]]; then
  695.                 if [[ "$has_display" == "yes" ]]; then echo -e  "Torrentexpander is up to date\n\n"; fi
  696.                 if [[ "$gnu_sed_available" != "yes" ]]; then sed -i '' "/last_update=/d" "$settings_file"; fi
  697.                 if [[ "$gnu_sed_available" == "yes" ]]; then sed -i "/last_update=/d" "$settings_file"; fi
  698.                 echo "last_update=$date_today" >> "$settings_file"
  699.         fi
  700.         if [[ "$release_vers" && ! "$current_version" ]] || [[ "$release_vers" && $current_version -lt $release_vers ]]; then
  701.                 if [[ "$has_display" == "yes" ]]; then echo -e "A new version of Torrentexpander is available.\n\nDownloading it right now\n\n"; fi
  702.                 if [[ "$wget_curl" == *wget* ]]; then
  703.                         "$wget_curl" -q "http://torrentexpander.googlecode.com/svn/trunk/torrentexpander.sh" -O "$temp_folder_without_slash/new_script"; wait;
  704.                 elif [[ "$wget_curl" == *curl* ]]; then
  705.                         "$wget_curl" -# -C - -o "$temp_folder_without_slash/new_script" "http://torrentexpander.googlecode.com/svn/trunk/torrentexpander.sh" > /dev/null 2>&1; wait;
  706.                 fi
  707.                 if [ "$(cat "$temp_folder_without_slash/new_script" | grep "# REQUIRED SOFTWARE #")" ]; then
  708.                         if [[ "$has_display" == "yes" ]]; then echo -e "A new version of Torrentexpander has been downloaded.\nNow installing\n\n"; fi
  709.                         if [[ "$check_settings" != *urrent_version=* ]]; then echo "current_version=$release_vers" >> "$settings_file"; fi
  710.                         if [[ "$last_update" != *ast_update=* ]]; then echo "last_update=$date_today" >> "$settings_file"; fi
  711.                         if [[ $current_version -lt $release_vers ]]; then
  712.                                 if [[ "$gnu_sed_available" != "yes" ]]; then sed -i '' "/current_version=/d" "$settings_file"; fi
  713.                                 if [[ "$gnu_sed_available" == "yes" ]]; then sed -i "/current_version=/d" "$settings_file"; fi
  714.                                 echo "current_version=$release_vers" >> "$settings_file"
  715.                                 if [[ "$gnu_sed_available" != "yes" ]]; then sed -i '' "/last_update=/d" "$settings_file"; fi
  716.                                 if [[ "$gnu_sed_available" == "yes" ]]; then sed -i "/last_update=/d" "$settings_file"; fi
  717.                                 echo "last_update=$date_today" >> "$settings_file"
  718.                         fi
  719.                         if [[ "$has_display" == "yes" ]] && [ "$silent_mode" != "yes" ]; then echo -e "Torrentexpander is gonna restart now\n\n"; fi
  720.                         cat "$temp_folder_without_slash/new_script" > "$script_path/torrentexpander.sh"; wait;
  721.                         rm -rf "$temp_folder"
  722.                         rm -f "$log_file"
  723.                         export script_updated="yes"
  724.                         if [ "$update_mode" != "yes" ] && [ "$silent_mode" != "yes" ]; then . "$script_path/torrentexpander.sh" "$torrent" "$destination_folder";
  725.                         elif [ "$update_mode" != "yes" ] && [ "$silent_mode" == "yes" ]; then . "$script_path/torrentexpander.sh" "$torrent" "$destination_folder" -c;
  726.                         fi
  727.                         sleep 5
  728.                         exit
  729.                 fi
  730.         fi
  731.         if [ "$update_mode" == "yes" ]; then rm -rf "$temp_folder" && rm -f "$log_file" && exit; fi
  732. fi
  733.  
  734. silent_mode="yes"
  735.  
  736. ##################################################################################
  737.  
  738.  
  739. ## Expanding and copying folders to the temp folder
  740. if [ "$has_display" == "yes" ]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Expanding / moving content of the torrent";  fi
  741. for item in $(if [[ "$current_folder" ]]; then find "$current_folder" -type d -follow; else echo "$torrent"; fi); do
  742.         # Don t bother with Mac OS X invisible files
  743.         if [[ "$item" == */.AppleDouble ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then
  744.                 echo "" > /dev/null 2>&1
  745.         # Fetch .rar and .001 rar files
  746.         elif [[ "$(ls "$item" | egrep -i "\.rar$|\.001$")" ]]; then
  747.                 # Find the right .rar file
  748.                 if [[ "$(ls "$item" | egrep -i "part001\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part001\.rar$"` && searchPath="$item/$rarFile";
  749.                 elif [[ "$(ls "$item" | egrep -i "part01\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part01\.rar$"` && searchPath="$item/$rarFile";
  750.                 elif [[ "$(ls "$item" | egrep -i "part1\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part1\.rar$"` && searchPath="$item/$rarFile";
  751.                 elif [[ -d "$item" && "$(ls "$item" | egrep -i "\.rar$")" ]]; then searchPath=`find "$item" -maxdepth 1 ! -name "._*" -type f -follow | egrep -i "\.rar$"`;
  752.                 elif [[ "$(echo "$torrent" | egrep -i "\.rar$" )" ]]; then searchPath=`echo "$item" | egrep -i "\.rar$"`;
  753.                 # switch back to the .001 file
  754.                 elif [[ "$(ls "$item" | egrep -i "\.001$")" ]]; then rarFile=`ls "$item" | egrep -i "\.001$"` && searchPath="$item/$rarFile";
  755.                 fi
  756.                 # use unrar to unrar files. Use nice -n 15 if available. Output will be displayed if possible
  757.                 if [[ "$unrar_bin" == *unrar* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unrar_bin" x -y -o+ -p- `echo "$f"` "$temp_folder"; done
  758.                 elif [[ "$unrar_bin" == *unrar* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unrar_bin" x -y -o+ -p- `echo "$f"` "$temp_folder" > /dev/null 2>&1; done
  759.                 elif [[ "$unrar_bin" == *unrar* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y -o+ -p- `echo "$f"` "$temp_folder" ; done
  760.                 elif [[ "$unrar_bin" == *unrar* ]]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y -o+ -p- `echo "$f"` "$temp_folder" > /dev/null 2>&1; done
  761.                 # use 7z to unrar files. Use nice -n 15 if available. Output will be displayed if possible
  762.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -15 "$unrar_bin" x -y `echo "$f"` -o"$temp_folder"; done
  763.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -15 "$unrar_bin" x -y `echo "$f"` -o"$temp_folder" > /dev/null 2>&1; done
  764.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y `echo "$f"` -o"$temp_folder"; done
  765.                 elif [[ "$unrar_bin" == *7z* ]]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y `echo "$f"` -o"$temp_folder" > /dev/null 2>&1; done
  766.                 fi
  767.         # Fetch zip files
  768.         elif [[ "$(ls $item | egrep -i "\.zip$")" ]]; then
  769.                 # use unzip to unzip files. Use nice -n 15 if available. Output will be displayed if possible
  770.                 if [[ -d "$item" && "$(ls "$item" | egrep -i "\.zip$")" ]]; then searchPath=`find "$item" -maxdepth 1 ! -name "._*" -type f -follow | egrep -i "\.zip$"`;
  771.                 elif [[ "$(echo "$item" | egrep -i "\.zip$" )" ]]; then searchPath=`echo "$item" | egrep -i "\.zip$"`;
  772.                 fi
  773.                 if [[ "$unzip_bin" == *unzip* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" -o -j `echo "$f"` -d "$temp_folder"; done
  774.                 elif [[ "$unzip_bin" == *unzip* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" -o -j `echo "$f"` -d "$temp_folder" > /dev/null 2>&1; done
  775.                 elif [[ "$unzip_bin" == *unzip* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" -o -j `echo "$f"` -d "$temp_folder"; done
  776.                 elif [[ "$unzip_bin" == *unzip* ]]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" -o -j `echo "$f"` -d "$temp_folder" > /dev/null 2>&1; done
  777.                 # use 7z to unzip files. Use nice -n 15 if available. Output will be displayed if possible
  778.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" x -y `echo "$f"` -o"$temp_folder"; done
  779.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" x -y `echo "$f"` -o"$temp_folder" > /dev/null 2>&1; done
  780.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" x -y `echo "$f"` -o"$temp_folder"; done
  781.                 elif [[ "$unzip_bin" == *7z* ]]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" x -y `echo "$f"` -o"$temp_folder" > /dev/null 2>&1; done
  782.                 fi
  783.         fi
  784. done
  785.  
  786. for item in $(if [[ "$current_folder" ]]; then find "$current_folder" -type d -follow; else echo "$torrent"; fi); do
  787.         # Don t bother with Mac OS X invisible files
  788.         if [[ "$item" == */.AppleDouble ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then
  789.                 echo "" > /dev/null 2>&1
  790.         # Now fetch all other files and copy them to the temp folder, or move them if in destructive mode
  791.         elif [[ "$(ls $item | egrep -i -v "\.[0-9][0-9][0-9]$|\.r[0-9][0-9]$|\.rar$|\.001$|\.zip$")" ]]; then
  792.                 #
  793.                 if [[ -d "$item" && "$(ls "$item" | egrep -i -v "\.[0-9][0-9][0-9]$|\.r[0-9][0-9]$|\.rar$|\.001$|\.zip$")" ]]; then otherFiles=`find "$item" -maxdepth 1 ! -name "._*" -type f -follow | egrep -i -v "\.[0-9][0-9][0-9]$|\.r[0-9][0-9]$|\.rar$|\.001$|\.zip$"`
  794.                 dest_path=`echo "$temp_folder$(echo "$item" | sed "s;.*/;;g")/"`
  795.                 elif [[ "$(echo "$item" | egrep -i -v "\.[0-9][0-9][0-9]$|\.r[0-9][0-9]$|\.rar$|\.001$|\.zip$")" ]]; then otherFiles=`echo "$item" | egrep -i -v "\.[0-9][0-9][0-9]$|\.r[0-9][0-9]$|\.rar$|\.001$|\.zip$"`
  796.                 dest_path=`echo "$temp_folder"`
  797.                 fi
  798.                 for f in $(echo -e "$otherFiles"); do
  799.                         if [ ! -d "$dest_path" ]; then mkdir -p "$dest_path"; fi
  800.                         otherFile=`echo "$f"`
  801.                         count=1
  802.                         dest_path_2=`echo "$dest_path$(basename "$otherFile")"`
  803.                         while [ -f "$dest_path_2" ]; do
  804.                                 if [[ count -eq 1 ]]; then
  805.                                         dest_path_2=`echo "$dest_path$(echo "$(basename "$otherFile")")"`;
  806.                                 else
  807.                                         dest_path_2=`echo "$dest_path$(echo "$(basename "$otherFile")" | sed 's/\(.*\)\..*/\1/') [$count]$(echo "$otherFile" | sed 's;.*\.;.;')"`;
  808.                                 fi
  809.                                 count=$(( count + 1 ))
  810.                         done
  811.                         if [[ "$nice_available" == "yes" && "$destructive_mode" != "yes" ]]; then nice -n 15 cp -f "$otherFile" "$dest_path_2";
  812.                         elif [[ "$nice_available" != "yes" && "$destructive_mode" != "yes" ]]; then cp -f "$otherFile" "$dest_path_2";
  813.                         elif [ "$destructive_mode" == "yes" ]; then nice -n 15 mv -f "$otherFile" "$dest_path_2";
  814.                         fi
  815.                 done
  816.         fi
  817. done
  818.  
  819.  
  820. ## If destructive_mode is enabled, remove original torrent
  821. cd "$destination_folder"
  822. if [[ "$has_display" == "yes" && "$destructive_mode" == "yes" ]]; then
  823.         step_number=$(( $step_number + 1 ));
  824.         echo "Step $step_number : Deleting original torrent";
  825. fi
  826.  
  827. # Make sure torrentexpander did not fail
  828. if [[ "$destructive_mode" == "yes" ]] && [[ -d "$current_folder" || -f "$torrent" ]]; then
  829.         temp_size=$(du -a -c "$temp_folder" | sed -n '$p' | sed "s;\([0-9]*\)\t.*;\1;")
  830.         count=0 && files=$(( $count + $(find "$temp_folder_without_slash" -type f | wc -l) ))
  831.         if [[ "$current_folder" ]]; then torrent_size=$(du -a -c "$current_folder" | sed -n '$p' | sed "s;\([0-9]*\)\t.*;\1;"); else echo torrent_size=$(du -a -c "$torrent" | sed -n '$p' | sed "s;\([0-9]*\)\t.*;\1;"); fi
  832.         temp_size=$(( $temp_size + ( $temp_size / 10 ) ))
  833.         if [ "$temp_size" -lt "$torrent_size" ] || [[ $files -eq 0 ]]; then destructive_mode="no"; fi
  834. fi
  835.  
  836. # If in GUI mode, we ask the user if he wants to remove this torrent from his torrent client
  837. user_confirm_removal=0
  838. if [ "$has_display" == "yes" ] && [[ "$destructive_mode" == "yes" && "$torrent_name" ]] && [ -x "$torrent_daemon_bin" ]; then
  839.         while [[ "$user_confirm_removal" != "1" ]] && [[ "$user_confirm_removal" != "2" ]]; do
  840.                 echo -e "\n\nDo you want to remove this torrent from your torrent client ?\n\n1) Yes, Please\n2) No, Thank You\n\n"
  841.                 read user_confirm_removal
  842.                 if [[ "$user_confirm_removal" != "1" ]] && [[ "$user_confirm_removal" != "2" ]]; then
  843.                         echo -e "\n\nUh-oh, I cannot understand your answer, please try again"
  844.                 elif [[ "$user_confirm_removal" == "1" ]] || [[ "$user_confirm_removal" == "2" ]]; then
  845.                         echo -e "\n\nDuly noted\n\n"
  846.                 fi
  847.         done
  848.         if [ "$user_confirm_removal" == "1" ]; then
  849.                 remove_torrent_from_client="yes";
  850.         fi
  851.         if [ "$user_confirm_removal" == "2" ]; then
  852.                 remove_torrent_from_client="no";
  853.         fi
  854. fi
  855.  
  856. # Trying to remove torrent from torrent client if destructive_mode is activated
  857. if [[ "$destructive_mode" == "yes" && "$torrent_name" ]] && [[ -x "$torrent_daemon_bin" && "$(echo "$torrent_daemon_bin" | grep "transmission-remote")" && "$torrent_daemon_port" ]] && [ "$remove_torrent_from_client" == "yes" ]; then
  858.         # Getting torrent ID
  859.         if [[ ! "$torrent_daemon_login" ]] || [[ ! "$torrent_daemon_password" ]]; then
  860.                 daemon_ip="localhost:$torrent_daemon_port"
  861.                 torrent_id=$("$torrent_daemon_bin" "$daemon_ip" -l > /dev/null 2>&1 | grep "$torrent_name" | sed "s;^ \([0-9]*\)   .*;\1;")
  862.         elif [[ "$torrent_daemon_login" ]] && [[ "$torrent_daemon_password" ]]; then
  863.                 daemon_ip="localhost:$torrent_daemon_port"
  864.                 daemon_l_p="$torrent_daemon_login:$torrent_daemon_password"
  865.                 torrent_id=$("$torrent_daemon_bin" "$daemon_ip" -n "$daemon_l_p" -l > /dev/null 2>&1 | grep "$torrent_name" | sed "s;^ *\([0-9]*\).*$;\1;")
  866.         fi
  867.         # Pausing and deleting torrent
  868.         if [[ "$torrent_id" ]]; then
  869.                 if [[ ! "$torrent_daemon_login" ]] || [[ ! "$torrent_daemon_password" ]]; then
  870.                         if [[ "$has_display" == "yes" ]]; then
  871.                                 "$torrent_daemon_bin" "$daemon_ip" -t "$torrent_id" -S && "$torrent_daemon_bin" "$daemon_ip" -t "$torrent_id" --remove-and-delete
  872.                         else
  873.                                 "$torrent_daemon_bin" "$daemon_ip" -t "$torrent_id" -S > /dev/null 2>&1 && "$torrent_daemon_bin" "$daemon_ip" -t "$torrent_id" --remove-and-delete > /dev/null 2>&1
  874.                         fi
  875.                 elif [[ "$torrent_daemon_login" ]] && [[ "$torrent_daemon_password" ]]; then
  876.                         if [[ "$has_display" == "yes" ]]; then
  877.                                 "$torrent_daemon_bin" "$daemon_ip" -n "$daemon_l_p" -t "$torrent_id" -S && "$torrent_daemon_bin" "$daemon_ip" -n "$daemon_l_p" -t "$torrent_id" --remove-and-delete
  878.                         else
  879.                                 "$torrent_daemon_bin" "$daemon_ip" -n "$daemon_l_p" -t "$torrent_id" -S > /dev/null 2>&1 && "$torrent_daemon_bin" -n "$daemon_l_p" "$daemon_ip" -t "$torrent_id" --remove-and-delete > /dev/null 2>&1
  880.                         fi
  881.                 fi
  882.         fi
  883. elif [[ "$destructive_mode" == "yes" && "$torrent_name" ]] && [[ -x "$torrent_daemon_bin" && "$(echo "$torrent_daemon_bin" | grep "xmlrpc")" ]] && [ "$remove_torrent_from_client" == "yes" ]; then
  884.         # Getting torrent IDs
  885.         if [[ ! "$torrent_daemon_login" ]] || [[ ! "$torrent_daemon_password" ]]; then
  886.                 daemon_ip="localhost"
  887.                 torrent_ids=$("$torrent_daemon_bin" "$daemon_ip" download_list | grep "Index" | sed "s;.*Index.*String.*'\(.*\)'.*;\1;g")
  888.         elif [[ "$torrent_daemon_login" ]] && [[ "$torrent_daemon_password" ]]; then
  889.                 daemon_ip="localhost"
  890.                 torrent_ids=$("$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" download_list | grep "Index" | sed "s;.*Index.*String.*'\(.*\)'.*;\1;g")
  891.         fi
  892.         # Getting torrent ID
  893.         for id in $(echo -e "$torrent_ids"); do
  894.                 if [[ ! "$torrent_daemon_login" ]] || [[ ! "$torrent_daemon_password" ]]; then
  895.                         daemon_ip="localhost"
  896.                         torrent_id=$("$torrent_daemon_bin" "$daemon_ip" d.get_name "$id" | grep "String" | sed "s;.*String.*'\(.*\)';\1;g")
  897.                 elif [[ "$torrent_daemon_login" ]] && [[ "$torrent_daemon_password" ]]; then
  898.                         daemon_ip="localhost"
  899.                         torrent_id=$("$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" d.get_name "$id" | grep "String" | sed "s;.*String.*'\(.*\)';\1;g")
  900.                 fi
  901.                 # Pausing and deleting torrent
  902.                 if [[ "$id" ]] && [ "$torrent_id" == "$torrent_name" ]; then
  903.                         if [[ ! "$torrent_daemon_login" ]] || [[ ! "$torrent_daemon_password" ]]; then
  904.                                 if [[ "$has_display" == "yes" ]]; then
  905.                                         "$torrent_daemon_bin" "$daemon_ip" d.close "$id" && "$torrent_daemon_bin" "$daemon_ip" d.erase "$id"
  906.                                 else
  907.                                         "$torrent_daemon_bin" "$daemon_ip" d.close "$id" > /dev/null 2>&1 && "$torrent_daemon_bin" "$daemon_ip" d.erase "$id" > /dev/null 2>&1
  908.                                 fi
  909.                         elif [[ "$torrent_daemon_login" ]] && [[ "$torrent_daemon_password" ]]; then
  910.                                 if [[ "$has_display" == "yes" ]]; then
  911.                                         "$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" d.close "$id" && "$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" d.erase "$id"
  912.                                 else
  913.                                         "$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" d.close "$id" > /dev/null 2>&1 && "$torrent_daemon_bin" -u "$torrent_daemon_login" -p "$torrent_daemon_password" "$daemon_ip" d.erase "$id" > /dev/null 2>&1
  914.                                 fi
  915.                         fi
  916.                 fi
  917.         done
  918. fi
  919.  
  920.  
  921. # If torrent client daemon cannot be controlled, we ll remove the torrent anyway
  922. if [[ "$destructive_mode" == "yes" && "$torrent" && -f "$torrent" ]]; then
  923.        rm -f "$torrent";
  924. elif [[ "$destructive_mode" == "yes" && "$current_folder" && -d "$current_folder" ]]; then
  925.        rm -rf "$current_folder";
  926. fi
  927.  
  928.  
  929. ## If .rar archives within archives - Expanding to the temp folder
  930. for item in $(find "$temp_folder_without_slash" -type d); do
  931.         if [[ "$item" == */.AppleDouble ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then
  932.                 echo "" > /dev/null 2>&1
  933.         elif [[ "$(ls "$item" | egrep -i "\.rar$")" ]]; then
  934.                 # Fetch .rar that were previously rared
  935.                 if [[ "$(ls "$item" | egrep -i "part001\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part001\.rar$"` && searchPath="$item/$rarFile";
  936.                 elif [[ "$(ls "$item" | egrep -i "part01\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part01\.rar$"` && searchPath="$item/$rarFile";
  937.                 elif [[ "$(ls "$item" | egrep -i "part1\.rar$")" ]]; then rarFile=`ls "$item" | egrep -i "part1\.rar$"` && searchPath="$item/$rarFile";
  938.                 elif [[ -d "$item" && "$(ls "$item" | egrep -i "\.rar$")" ]]; then searchPath=`find "$item" -maxdepth 1 ! -name "._*" -type f -follow | egrep -i "\.rar$"`;
  939.                 elif [[ "$(echo "$torrent" | egrep -i "\.rar$" )" ]]; then searchPath=`echo "$item" | egrep -i "\.rar$"`;
  940.                 elif [[ "$(ls "$item" | egrep -i "\.001$")" ]]; then rarFile=`ls "$item" | egrep -i "\.001$"` && searchPath="$item/$rarFile";
  941.                 fi
  942.                 # use unrar to unrar files. Use nice -n 15 if available. Output will be displayed if possible
  943.                 if [[ "$unrar_bin" == *unrar* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unrar_bin" x -y -o+ -p- `echo "$f"` "$item"; done
  944.                 elif [[ "$unrar_bin" == *unrar* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unrar_bin" x -y -o+ -p- `echo "$f"` "$item" > /dev/null 2>&1; done
  945.                 elif [[ "$unrar_bin" == *unrar* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y -o+ -p- `echo "$f"` "$item"; done
  946.                 elif [[ "$unrar_bin" == *unrar* ]]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y -o+ -p- `echo "$f"` "$item" > /dev/null 2>&1; done
  947.                 # use 7z to unrar files. Use nice -n 15 if available. Output will be displayed if possible
  948.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -15 `echo "$f"` x -y "$searchPath" -o"$item"; done
  949.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -15 "$unrar_bin" x -y `echo "$f"` -o"$item" > /dev/null 2>&1; done
  950.                 elif [[ "$unrar_bin" == *7z* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y `echo "$f"` -o"$item"; done
  951.                 elif [[ "$unrar_bin" == *7z* ]]; then for f in $(echo -e "$searchPath"); do "$unrar_bin" x -y `echo "$f"` -o"$item" > /dev/null 2>&1; done
  952.                 fi
  953.         fi
  954. done
  955.  
  956. ## If .zip archives within archives - Expanding to the temp folder
  957. for item in $(find "$temp_folder_without_slash" -type d); do
  958.         if [[ "$item" == */.AppleDouble ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then
  959.                 echo "" > /dev/null 2>&1
  960.         elif [[ "$(ls "$item" | egrep -i "\.zip$")" ]]; then
  961.                 searchPath=`find "$item" -maxdepth 1 ! -name "._*" -type f -follow | egrep -i "\.zip$"`;
  962.                 # use unzip to unzip files. Use nice -n 15 if available. Output will be displayed if possible
  963.                 if [[ "$unzip_bin" == *unzip* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" -o -j `echo "$f"` -d "$item"; done
  964.                 elif [[ "$unzip_bin" == *unzip* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" -o -j `echo "$f"` -d "$item" > /dev/null 2>&1; done
  965.                 elif [[ "$unzip_bin" == *unzip* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" -o -j `echo "$f"` -d "$item"; done
  966.                 elif [[ "$unzip_bin" == *unzip* ]]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" -o -j `echo "$f"` -d "$item" > /dev/null 2>&1; done
  967.                 # use 7z to unzip files. Use nice -n 15 if available. Output will be displayed if possible
  968.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$nice_available" == "yes" ] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" x -y `echo "$f"` -o"$item"; done
  969.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$nice_available" == "yes" ]; then for f in $(echo -e "$searchPath"); do nice -n 15 "$unzip_bin" x -y `echo "$f"` -o"$item" > /dev/null 2>&1; done
  970.                 elif [[ "$unzip_bin" == *7z* ]] && [ "$has_display" == "yes" ]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" x -y `echo "$f"` -o"$item"; done
  971.                 elif [[ "$unzip_bin" == *7z* ]]; then for f in $(echo -e "$searchPath"); do "$unzip_bin" x -y `echo "$f"` -o"$item" > /dev/null 2>&1; done
  972.                 fi
  973.         fi
  974. done
  975.  
  976. ## Delete Mac OS X invisible files and sample from temp folder
  977. for item in $(find "$temp_folder_without_slash"); do
  978.         item=`echo "$item"`
  979.         if [[ "$item" == */.AppleDouble* ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then rm -rf "$item"
  980.         elif [[ "$(echo "$item" | egrep -i "^sample[^A-Za-z0-9_]" )" && "$(echo "$item" | egrep -i "\.avi$|\.mkv$|\.divx$|\.mp4$|\.ts$" )" ]] || [[ "$(echo "$item" | egrep -i "[^A-Za-z0-9_]sample[^A-Za-z0-9_]" )" && "$(echo "$item" | egrep -i "\.avi$|\.mkv$|\.divx$|\.mp4$|\.ts$" )" ]]; then rm -rf "$item"
  981.         fi
  982. done
  983.  
  984. ## Count number of resulting files and disable optional functionalities if no supported file
  985. count=0 && files=$(( $count + $(find "$temp_folder_without_slash" -type f | egrep -i "$supported_extensions_rev" | wc -l) ))
  986.  
  987.  
  988. ## No supported file, disable all optional features except for permissions and timestamp
  989. if [[ $files -eq 0 ]]; then
  990.         tv_shows_fix_numbering="no" && clean_up_filenames="no" && dts_post="no" && img_post="no" && wii_post="no" && tv_shows_post="no" && music_post="no" && movies_post="no" && imdb_poster="no" && imdb_nfo="no" && imdb_fanart="no" && debug_mode="no" && subtitles_handling="no" && repack_handling="no" && force_single_file_movies_folder="no" && disable_nmj_scan="no"
  991.         count=0 && files=$(( $count + $(find "$temp_folder_without_slash" -type f | wc -l) ))
  992.         if [[ $files -eq 0 ]]; then
  993.                 # No file at all something is wrong so we disable all features
  994.                 if [[ "$has_display" == "yes" ]]; then
  995.                         step_number=$(( $step_number + 1 ));
  996.                         echo "Step $step_number : Uh Oh, Something went wrong, I cannot continue";
  997.                 fi
  998.                 destructive_mode="no" && user_perm_post="no" && group_perm_post="no" && files_perm_post="no" && folder_perm_post="no" && edit_perm_as_sudo="no" && third_party_log="no" && reset_timestamp="no" && remove_torrent_from_client="no" && all_files_script_enabled="no" && processed_torrent_script_enabled="no" && post_run_script_enabled="no" && script_failed="yes"
  999.         else
  1000.                 # Unsupported files only so we will only do the bare minimum
  1001.                 if [ ! "$folder_short" ]; then folder_short=`echo "$torrent" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`; fi
  1002.                 for item in $(find "$temp_folder" -type f); do
  1003.                         item=`echo "$item"`
  1004.                         mkdir -p "$temp_folder$folder_short/"
  1005.                         if [ ! -f "$temp_folder$folder_short/$(basename "$item")" ]; then
  1006.                                 mv -f "$item" "$temp_folder$folder_short/"
  1007.                         fi
  1008.                         echo "$item" >> "$log_file"
  1009.                 done
  1010.         fi
  1011. fi
  1012.  
  1013.  
  1014. ## If only one resulting file rename it according to the initial torrent
  1015. if [[ $files -eq 1 ]]; then
  1016.         if [ ! "$folder_short" ]; then folder_short=`echo "$torrent" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`; fi
  1017.         item=`echo "$(find "$temp_folder_without_slash" -type f | egrep -i "$supported_extensions_rev")"`;
  1018.         extension=`echo "$item" | sed 's;.*\.;.;'`;
  1019.         if [[ "$item" != "$temp_folder$folder_short$extension" ]]; then mv "$item" "$temp_folder$folder_short$extension"; fi && echo "$temp_folder$folder_short$extension" >> "$log_file"
  1020.         subtitles_dest=`echo "$subtitles_directory/$(basename "$item")"`
  1021.         already_subtitles=`echo "$(echo "$item" | sed 's/\(.*\)\..*/\1\.srt/')"`
  1022.         if [[ "$subtitles_mode" != "yes" && "$subtitles_handling" == "yes" && ! -f "$already_subtitles" && "$(echo "$item" | egrep -i "\.avi$|\.mkv$|\.divx$|\.mp4$|\.ts$")" ]]; then if [ ! -d "$subtitles_directory" ]; then mkdir -p "$subtitles_directory"; fi ; echo "$(basename "$item")" > "$subtitles_dest"; fi
  1023.         # Reset folder_short variable because no folder will be generated
  1024.         folder_short=""
  1025. fi
  1026.  
  1027. ## If more than one supported file, create folder named as the initial one and move the resulting files there
  1028. if [[ $files -gt 1 ]]; then for directory in $(find "$temp_folder_without_slash" -type d); do
  1029.         # Archive that contains several files. We ll use the original name of the archive
  1030.         if [ ! "$folder_short" ]; then folder_short=`echo "$torrent" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`; fi
  1031.         # For audio files, we ll change things a bit, in order to group album tracks from an audio pack
  1032.         if [ "$(ls $directory | egrep -i "$music_extensions_rev" )" ]; then
  1033.                 audioFiles=`ls $directory | egrep -i "$music_extensions_rev"`;
  1034.                 for f in $(echo -e "$audioFiles"); do
  1035.                         item=`echo "$directory/$f"`;
  1036.                         depth=$(( $(echo "$directory/" | sed "s;$torrent_directory;;g" | sed "s;[^/];;g" | wc -c) - 1 ))
  1037.                         if [[ $depth -eq 1 ]]; then destination_name="$temp_folder$folder_short/"; elif [[ $depth -gt 1 ]]; then destination_name="$temp_folder$folder_short/$(echo "$item" | sed "s;$temp_folder;;g" | sed "s;/; - ;g")"; fi
  1038.                         mkdir -p "$temp_folder$folder_short/" && mv -f "$item" "$destination_name"
  1039.                 done
  1040.         # We ll move all the other files to a directory named after the torrent
  1041.         elif [ "$(ls $directory | egrep -i "$supported_extensions_rev" )" ]; then
  1042.                 otherFiles=`ls $directory | egrep -i "$supported_extensions_rev"`;
  1043.                 if [ ! -f "$temp_folder$folder_short/$(basename $item)" ]; then
  1044.                         for f in $(echo -e "$otherFiles"); do item=`echo "$directory/$f"`; mkdir -p "$temp_folder$folder_short/" && mv -f "$item" "$temp_folder$folder_short/"; done
  1045.                 fi
  1046.         fi
  1047. done
  1048.  
  1049. ## Generate dummy 0k video for later subtitles fetching if no subtitles is already available
  1050. for item in $(find "$temp_folder$folder_short" -type f | egrep -i "$supported_extensions_rev"); do
  1051.         subtitles_dest=`echo "$subtitles_directory/$(basename "$item")"`
  1052.         already_subtitles=`echo "$(echo "$item" | sed 's/\(.*\)\..*/\1\.srt/')"`
  1053.         if [[ "$subtitles_mode" != "yes" && "$subtitles_handling" == "yes" && ! -f "$already_subtitles" && "$(echo "$item" | egrep -i "\.avi$|\.mkv$|\.divx$|\.mp4$|\.ts$")" ]]; then if [ ! -d "$subtitles_directory" ]; then mkdir -p "$subtitles_directory"; fi ; echo "$(basename "$item")" > "$subtitles_dest"; fi
  1054.         echo "$item" >> "$log_file"
  1055. done
  1056. fi
  1057.  
  1058.  
  1059. ######################### Optional functionalities ################################
  1060.  
  1061. # defining imdb_funct_on variable so that we don't have to ckeck 3 variables everytime
  1062. if [[ "$imdb_poster" == "yes" || "$imdb_nfo" == "yes" || "$imdb_fanart" == "yes" ]]; then imdb_funct_on="yes"; fi
  1063.  
  1064. # Adding the surrounding folder to the log file so that it can be renamed
  1065. if [[ "$folder_short" && "$tv_shows_fix_numbering" == "yes" ]] || [[ "$folder_short" && "$clean_up_filenames" == "yes" ]] || [[ "$folder_short" && "$imdb_funct_on" == "yes" ]]; then echo "$temp_folder$folder_short" >> "$log_file"; fi
  1066.  
  1067. ## Try to solve TV Shown Numbering issues
  1068. if [[ "$has_display" == "yes" && "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([0-9])([0-9])([xX])([0-9])([0-9])([. _-])")" ]] || [[ "$has_display" == "yes" && "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([123456789])([xX])([0-9])([0-9])")" ]] || [[ "$has_display" == "yes" && "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([01])([0-9])([0-3])([0-9])([^pPiI])")" ]] || [[ "$has_display" == "yes" && "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i ".([^eE])([12345689])([0123])([0-9])([^0123456789pPiI])")" ]]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Trying to solve TV Shows numbering issues";  fi
  1069. # Looking for files that look like TV shows because they contain SxEE, SSEE, SEE
  1070. if [[ "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([0-9])([0-9])([xX])([0-9])([0-9])([. _-])")" ]] || [[ "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([123456789])([xX])([0-9])([0-9])")" ]] || [[ "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i "([. _-])([01])([0-9])([0-3])([0-9])([^pPiI])")" ]] || [[ "$tv_shows_fix_numbering" == "yes" && "$(cat "$log_file" | egrep -i ".([^eE])([12345689])([0123])([0-9])([^0123456789pPiI])")" ]]; then for line in $(cat "$log_file"); do
  1071.         item=`echo "$(basename "$line")"`;
  1072.         ren_file=`echo "$item"`;
  1073.         source=`echo "$line"`;
  1074.         # Looking for SSxEE pattern
  1075.         if [[ "$tv_shows_fix_numbering" == "yes" && "$(echo "$line" | egrep -i "([. _-])([0-9])([0-9])([xX])([0-9])([0-9])([. _-])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$line" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1076.                 ren_file=`echo "$item" | sed 's;\(.*\)[. _-]\([0-9]\)\([0-9]\)\([xX]\)\([0-9]\)\([0-9]\)\([. _-]\);\1 S\2\3E\5\6\7;g'`;
  1077.         # Looking for SxEE pattern
  1078.         elif [[ "$tv_shows_fix_numbering" == "yes" && "$(echo "$line" | egrep -i "([. _-])([123456789])([xX])([0-9])([0-9])([. _-])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$line" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1079.                 ren_file=`echo "$item" | sed 's;\(.*\)[. _-]\([123456789]\)\([xX]\)\([0-9]\)\([0-9]\)\([. _-]\);\1 S0\2E\4\5\6;g'`;
  1080.         # Looking for SSEE pattern
  1081.         elif [[ "$tv_shows_fix_numbering" == "yes" && "$(echo "$line" | egrep -i "([. _-])([01])([0-9])([0-3])([0-9])([^pPiI])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$line" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1082.                 ren_file=`echo "$item" | sed 's;\(.*\)[. _-]\([01]\)\([0-9]\)\([0-3]\)\([0-9]\)\([^pPiI]\);\1 S\2\3E\4\5\6;g'`;
  1083.         # Looking for SEE pattern
  1084.         elif [[ "$tv_shows_fix_numbering" == "yes" && "$(echo "$line" | egrep -i ".([^eE])([12345689])([0123])([0-9])([^0123456789pPiI])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$line" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1085.                 ren_file=`echo "$item" | sed 's;\(.*\)\(.\)\([^eE]\)\([12345689]\)\([0123]\)\([0-9]\)\([^0123456789pPiI]\);\1\2\3S0\4E\5\6\7;g'`;
  1086.         fi
  1087.         bis="_bis"
  1088.         ren_location=`echo "$(dirname "$source")/$ren_file"`;
  1089.         ren_temp_location=`echo "$(dirname "$source")/$ren_file$bis"`;
  1090.         source_bis=`echo "$line"`;
  1091.         source_ter=$(echo "$line" | sed "s;\([][]\);\\\\\1;g") && source_ter=`echo "$source_ter"`;
  1092.         ren_location_bis=$(echo "$ren_location" | sed "s;\([][)(]\);\\\\\1;g") && ren_location_bis=`echo "$ren_location_bis"`;
  1093.         # Displaying output if possible
  1094.         if [ "$has_display" == "yes" ] && [ "$item" != "$ren_file" ]; then echo "- Renaming $item to $ren_file";  fi
  1095.         # Working around Mac OS X case insensitive filesystem
  1096.         if [[ -d "$ren_location" && "$(dirname "$source")/" == "$temp_folder" && "$item" != "$ren_file" ]]; then mv -f "$source" "$ren_temp_location"; rm -rf "$ren_location"; source="$ren_temp_location"; fi
  1097.         # Renaming file the bsd sed way
  1098.         if [ "$item" != "$ren_file" ] && [ "$gnu_sed_available" != "yes" ]; then mv -f "$source" "$ren_location" && sed -i '' "s;^$source_ter;$ren_location_bis;g" "$log_file"
  1099.         # Renaming file the gnu sed way
  1100.         elif [ "$item" != "$ren_file" ] && [ "$gnu_sed_available" == "yes" ]; then mv -f "$source" "$ren_location" && sed -i "s;^$source_ter;$ren_location_bis;g" "$log_file"
  1101.         fi
  1102. done
  1103. fi
  1104.  
  1105.  
  1106. ## Cleanup filenames
  1107. if [[ "$has_display" == "yes" && "$clean_up_filenames" == "yes" ]]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Cleaning up filenames";  fi
  1108.  
  1109. # When clean_up_finenames is disabled but imdb enabled, we ll only get the last line of the log file to improve speed
  1110. if [ "$clean_up_filenames" == "yes" ]; then temp_log_file="$(echo -e "$(cat "$log_file")")"
  1111.         elif [ "$imdb_funct_on" == "yes" ]; then temp_log_file="$(echo -e "$(cat "$log_file" | sed -n '$p')")"
  1112. fi
  1113.  
  1114. if [ "$clean_up_filenames" == "yes" ] || [ "$imdb_funct_on" == "yes" ]; then for line in $(echo -e "$temp_log_file"); do
  1115.         file_renamed="no";
  1116.         item=`echo "$(basename "$line")"`;
  1117.         ren_file=`echo "$item"`;
  1118.         source=`echo "$line"`;
  1119.         # Resetting quality and audio quality in order not to keep values from previous pass
  1120.         if [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1121.                 movie_year="$(echo "$movie_year" | sed 's;^ ;;g')"
  1122.         else
  1123.                 quality="";
  1124.                 audio_quality="";
  1125.                 quality_quality="";
  1126.                 movie_year="";
  1127.         fi
  1128.         movie_year_bis=""
  1129.         movie_year_ter=""
  1130.         # When renaming a folder we ll use an empty extension
  1131.         # We ll get rid of the extension in the title_clean variable
  1132.         if [ -d "$source" ]; then extension="" && title_clean=`echo "$item"`; else extension=`echo "$item" | sed 's;.*\.;.;'` && title_clean=`echo "$item" | sed 's/\(.*\)\..*/\1/'`; fi
  1133.         # I admit this line could be shortened by using OS dependent commands
  1134.         # I prefer to use a slower way that should run on all platform
  1135.         # We ll first replace dots and underscores by spaces if they are not followed by a space
  1136.         # Then we ll add a temporary underscore at the beginning and at the end
  1137.         # We ll then remove brackets
  1138.         # Then we convert everything to lowercase
  1139.         # We'll try not to fuck up capitalization in names like McLachlan or MacDonald
  1140.         # Once this is done we ll remove the underscore at the beginning of the name
  1141.         title_clean_bis=`echo "$title_clean" | sed 's/\([\._]\)\([^ ]\)/ \2/g' | sed "s/^/_/g" | sed "s/$/_/g" | sed 's/\+/ /g' | sed "s/A/a/g" | sed "s/B/b/g" | sed "s/C/c/g" | sed "s/D/d/g" | sed "s/E/e/g" | sed "s/F/f/g" | sed "s/G/g/g" | sed "s/H/h/g" | sed "s/I/i/g" | sed "s/J/j/g" | sed "s/K/k/g" | sed "s/L/l/g" | sed "s/M/m/g" | sed "s/N/n/g" | sed "s/O/o/g" | sed "s/P/p/g" | sed "s/Q/q/g" | sed "s/R/r/g" | sed "s/S/s/g" | sed "s/T/t/g" | sed "s/U/u/g" | sed "s/V/v/g" | sed "s/W/w/g" | sed "s/X/x/g" | sed "s/Y/y/g" | sed "s/Z/z/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*a/\1\2\3A/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*b/\1\2\3B/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*c/\1\2\3C/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*d/\1\2\3D/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*e/\1\2\3E/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*f/\1\2\3F/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*g/\1\2\3G/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*h/\1\2\3H/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*i/\1\2\3I/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*j/\1\2\3J/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*k/\1\2\3K/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*l/\1\2\3L/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*m/\1\2\3M/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*n/\1\2\3N/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*o/\1\2\3O/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*p/\1\2\3P/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*q/\1\2\3Q/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*r/\1\2\3R/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*s/\1\2\3S/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*t/\1\2\3T/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*u/\1\2\3U/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*v/\1\2\3V/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*w/\1\2\3W/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*x/\1\2\3X/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*y/\1\2\3Y/g" | sed "s/\([. _-]\)\(mc\)*\(mac\)*z/\1\2\3Z/g"`;
  1142.        
  1143.         # Backing up year to reuse it later
  1144.         if [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" && "$(echo "$title_clean_bis" | grep "[. _-]\([0-9][0-9][0-9][0-9]\)[. _-]*.*$")" == "" ]]; then
  1145.                 echo > /dev/null 2>&1;
  1146.         else
  1147.                 movie_year=`echo "$title_clean_bis" | grep ".[. _-]\([0-9][0-9][0-9][0-9]\)[. _-]*.*$" | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s;\([^_]\)$;\1_;g" | sed "s/1080/_/g" | sed "s/..*[. _-]\([0-9][0-9][0-9][0-9]\)[. _-]*.*$/\1/g"`
  1148.         fi
  1149.         if [ "$movie_year" ]; then movie_year_bis=" ($movie_year)"; fi
  1150.         if [ "$movie_year" ]; then movie_year_ter=" %28$movie_year%29"; fi
  1151.         if [ "$movie_year" ]; then movie_year=" $movie_year"; fi
  1152.        
  1153.         # Here we ll try to guess the audio quality of the file based on patterns
  1154.         title_clean_bis="$(echo "$title_clean_bis" | sed "s;\([^_]\)$;\1_;g" | sed 's/[()]//g')"
  1155.         movies_title_clean_bis="$(echo "$title_clean_bis" | sed 's/[()]//g')"
  1156.         if [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]] || [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1157.                 for aq in $(echo -e "$(echo "$audio_quality_patterns" | sed "s;,;\\\n;g")"); do
  1158.                         if [ "$(echo "$movies_title_clean_bis" | egrep -i "[. _-]$aq[. _-]")" ]; then
  1159.                                 regexp_pat="$(echo "$aq" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1160.                                 audio_quality="$aq";
  1161.                                 movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;$(echo "$movies_title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p').*;_;")";
  1162.                         fi
  1163.                 done
  1164.         else
  1165.                 for aq in $(echo -e "$(echo "$audio_quality_patterns" | sed "s;,;\\\n;g")"); do
  1166.                         if [ "$(echo "$title_clean_bis" | egrep -i "[. _-]$aq[. _-]")" ]; then
  1167.                                 regexp_pat="$(echo "$aq" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1168.                                 audio_quality="$aq";
  1169.                                 title_clean_bis="$(echo "$title_clean_bis" | sed "s;$(echo "$title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p');_;")";
  1170.                         fi
  1171.                 done
  1172.         fi
  1173.        
  1174.         # Here we ll try to guess the video quality of the file based on patterns and aggregate audio quality
  1175.         title_clean_bis="$(echo "$title_clean_bis" | sed "s;\([^_]\)$;\1_;g")"
  1176.         movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;\([^_]\)$;\1_;g")"
  1177.         if [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]] || [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1178.                 for q in $(echo -e "$(echo "$movies_detect_patterns,$movies_detect_patterns_pt_2" | sed "s;,;\\\n;g")"); do
  1179.                         if [ "$(echo "$movies_title_clean_bis" | egrep -i "[. _-]$q[. _-]")" ]; then
  1180.                                 regexp_pat="$(echo "$q" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1181.                                 quality=" ($q)";
  1182.                                 quality_quality=" ($audio_quality-$q)";
  1183.                                 movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;$(echo "$movies_title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p').*;_;")";
  1184.                         fi
  1185.                 done
  1186.         else
  1187.                 for q in $(echo -e "$(echo "$movies_detect_patterns,$movies_detect_patterns_pt_2" | sed "s;,;\\\n;g")"); do
  1188.                         if [ "$(echo "$title_clean_bis" | egrep -i "[. _-]$q[. _-]")" ]; then
  1189.                                 regexp_pat="$(echo "$q" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1190.                                 quality=" ($q)";
  1191.                                 quality_quality=" ($audio_quality-$q)";
  1192.                                 title_clean_bis="$(echo "$title_clean_bis" | sed "s;$(echo "$title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p');_;")";
  1193.                         fi
  1194.                 done
  1195.         fi
  1196.        
  1197.         # Remove unnecessary information in filename
  1198.         title_clean_bis="$(echo "$title_clean_bis" | sed "s;\([^_]\)$;\1_;g")"
  1199.         movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;\([^_]\)$;\1_;g")"
  1200.         if [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]] || [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1201.                 for i in $(echo -e "$(echo "$other_movies_patterns" | sed "s;,;\\\n;g")"); do
  1202.                         if [ "$(echo "$movies_title_clean_bis" | egrep -i "[. _-]$i[. _-]")" ]; then
  1203.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1204.                                 movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;$(echo "$movies_title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p').*;_;")";
  1205.                         fi
  1206.                 done
  1207.         else
  1208.                 for i in $(echo -e "$(echo "$other_movies_patterns" | sed "s;,;\\\n;g")"); do
  1209.                         if [ "$(echo "$title_clean_bis" | egrep -i "[. _-]$i[. _-]")" ]; then
  1210.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")";
  1211.                                 title_clean_bis="$(echo "$title_clean_bis" | sed "s;$(echo "$title_clean_bis" | egrep -o "[. _-]$regexp_pat[. _-]" | sed -n '1p');_;")";
  1212.                         fi
  1213.                 done
  1214.         fi
  1215.  
  1216.         # Remove scene names in filename
  1217.         title_clean_bis="$(echo "$title_clean_bis" | sed "s;\([^_]\)$;\1_;g" | sed "s;^\([^_]\);_\1;g")"
  1218.         movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;\([^_]\)$;\1_;g" | sed "s;^\([^_]\);_\1;g")"
  1219.         if [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]] || [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1220.                 for i in $(echo -e "$(echo "$scene_patterns" | sed "s;,;\\\n;g")"); do
  1221.                         if [ "$(echo "$movies_title_clean_bis" | egrep -i "[. _-]$i[. _-]")" ]; then
  1222.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")"; title_clean_bis="$(echo "$title_clean_bis" | sed "s;[. _-]$regexp_pat[. _-];_;")";
  1223.                                 movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;[. _-]$regexp_pat[. _-];_;")"
  1224.                         fi
  1225.                 done
  1226.                 for i in $(echo -e "$(echo "$user_defined_scene_patterns_hothead_edition" | sed "s;,;\\\n;g")"); do
  1227.                         echo "toto"
  1228.                         if [ "$(echo "$title_clean_bis" | egrep -i "$i")" ]; then
  1229.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")"; title_clean_bis="$(echo "$title_clean_bis" | sed "s;$regexp_pat.*;_;")";
  1230.                                 movies_title_clean_bis="$(echo "$movies_title_clean_bis" | sed "s;$regexp_pat.*;_;")"
  1231.                         fi
  1232.                 done
  1233.         else
  1234.                 for i in $(echo -e "$(echo "$scene_patterns" | sed "s;,;\\\n;g")"); do
  1235.                         if [ "$(echo "$title_clean_bis" | egrep -i "[. _-]$i[. _-]")" ]; then
  1236.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")"; title_clean_bis="$(echo "$title_clean_bis" | sed "s;[. _-]$regexp_pat[. _-];_;")";
  1237.                         fi
  1238.                 done
  1239.                 for i in $(echo -e "$(echo "$user_defined_scene_patterns_hothead_edition" | sed "s;,;\\\n;g")"); do
  1240.                         echo "toto"
  1241.                         if [ "$(echo "$title_clean_bis" | egrep -i "$i")" ]; then
  1242.                                 regexp_pat="$(echo "$i" | sed "s/[aA]/[aA]/g" | sed "s/[bB]/[bB]/g" | sed "s/[cC]/[cC]/g" | sed "s/[dD]/[dD]/g" | sed "s/[eE]/[eE]/g" | sed "s/[fF]/[fF]/g" | sed "s/[gG]/[gG]/g" | sed "s/[hH]/[hH]/g" | sed "s/[iI]/[iI]/g" | sed "s/[jJ]/[jJ]/g" | sed "s/[kK]/[kK]/g" | sed "s/[lL]/[lL]/g" | sed "s/[mM]/[mM]/g" | sed "s/[nN]/[nN]/g" | sed "s/[oO]/[oO]/g" | sed "s/[pP]/[pP]/g" | sed "s/[qQ]/[qQ]/g" | sed "s/[rR]/[rR]/g" | sed "s/[sS]/[sS]/g" | sed "s/[tT]/[tT]/g" | sed "s/[uU]/[uU]/g" | sed "s/[vV]/[vV]/g" | sed "s/[wW]/[wW]/g" | sed "s/[xX]/[xX]/g" | sed "s/[yY]/[yY]/g" | sed "s/[zZ]/[zZ]/g")"; title_clean_bis="$(echo "$title_clean_bis" | sed "s;$regexp_pat.*;_;")";
  1243.                         fi
  1244.                 done
  1245.         fi
  1246.        
  1247.         # Doing some more cleanup
  1248.         title_clean_bis=`echo "$title_clean_bis" | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s/_/ /g"`;
  1249.         if [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]] || [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1250.                 series_title_clean_bis=`echo "$movies_title_clean_bis" | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s/_/ /g"`;
  1251.         else
  1252.                 series_title_clean_bis=`echo "$title_clean_bis" | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s/_/ /g"`;
  1253.         fi
  1254.         movies_title_clean_bis=`echo "$movies_title_clean_bis" | sed 's/\(.\)[. _-][0-9][0-9][0-9][0-9][. _-].*/\1/g' | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s/_/ /g"`;
  1255.  
  1256.         # Remove underscores at the beginning and at the end of the filename
  1257.         title_clean_ter=`echo "$title_clean_bis" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g"`;
  1258.         series_title_clean_ter=`echo "$series_title_clean_bis" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g"`;
  1259.         movies_title_clean_ter=`echo "$movies_title_clean_bis" | sed 's/[()]//g' | sed 's/\[//g' | sed 's/\]//g' | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s/_/ /g"`;
  1260.         if [[ "$repack_handling" == "yes" && "$(echo "$item" | egrep -i "([. _])repack([. _])|([. _])proper([. _])|([. _])rerip([. _])|([. _])real([. _])")" ]]; then is_repack=" REPACK"; else is_repack=""; fi
  1261.  
  1262.         # Focusing on TV Series with a SXXEXX pattern
  1263.         if [[ "$(echo "$item" | egrep -i "([sS])([0-9])([0-9])([eE])([0-9])([0-9])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$source" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1264.                 # For TV series we ll only display quality with 720p and 1080p files
  1265.                 if [[ "$quality" != " (720p)" && "$quality" != " (1080p)" ]] || [[ "$movies_rename_schema" == "type_1" ]]; then quality=""; fi
  1266.                 series_title=`echo "$series_title_clean_ter" | sed 's;.\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;;'`;
  1267.                 series_episode=`echo "$item" | sed 's;.*\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;S\2\3E\5\6;g'`;
  1268.                 # The file will then be renamed "Title SXXEXX.ext", "Title SXXEXX (720p).ext" or "Title SXXEXX (1080p).ext"
  1269.                 ren_file=`echo "$series_title $series_episode$is_repack$quality$extension"`;
  1270.                 file_renamed="yes";
  1271.         # Focusing on TV Shows with a YYYY.MM.DD pattern
  1272.         elif [[ "$(echo "$item" | egrep -i "([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])")" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$source" || "$(echo "$line" | egrep -i "$tv_show_extensions_rev")" ]]; then
  1273.                 # For TV series we ll only display quality with 720p and 1080p files
  1274.                 if [[ "$quality" != " (720p)" && "$quality" != " (1080p)" ]] || [[ "$movies_rename_schema" == "type_1" ]]; then quality=""; fi
  1275.                 talk_show_title=`echo "$series_title_clean_ter" | sed 's/\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\)/\1\2\3\4-\5\6-\7\8/g'`;
  1276.                 # The file will then be renamed "Title YYYY-MM-DD.ext"
  1277.                 ren_file=`echo "$talk_show_title$is_repack$quality$extension"`;
  1278.                 file_renamed="yes";
  1279.         # Focusing on movies. Type_1 renaming will look like "Title (YYYY).ext"
  1280.         elif [[ "$movies_rename_schema" == "type_1" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]]; then
  1281.                 ren_file=`echo "$movies_title_clean_ter$movie_year_bis$extension"`;
  1282.                 # Storing movie title in an imdb friendly format
  1283.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1284.                 file_renamed="yes";
  1285.         # Focusing on movies in directories. Type_1 renaming will look like "Title (YYYY).ext"
  1286.         elif [[ "$movies_rename_schema" == "type_1" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1287.                 ren_file=`echo "$movies_title_clean_ter$movie_year_bis$extension"`;
  1288.                 # Storing movie title in an imdb friendly format
  1289.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1290.                 file_renamed="yes";
  1291.         # Focusing on movies. Type_2 renaming will look like "Title YYYY (Video_Quality).ext"
  1292.         elif [[ "$movies_rename_schema" == "type_2" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]]; then
  1293.                 ren_file=`echo "$movies_title_clean_ter$movie_year$quality$extension"`;
  1294.                 # Storing movie title in an imdb friendly format
  1295.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1296.                 file_renamed="yes";
  1297.         # Focusing on movies in directories. Type_2 renaming will look like "Title YYYY (Video_Quality).ext"
  1298.         elif [[ "$movies_rename_schema" == "type_2" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1299.                 ren_file=`echo "$movies_title_clean_ter$movie_year$quality$extension"`;
  1300.                 # Storing movie title in an imdb friendly format
  1301.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1302.                 file_renamed="yes";
  1303.         # Focusing on movies. Type_3 renaming will look like "Title YYYY (Audio_Quality-Video_Quality).ext"
  1304.         elif [[ "$movies_rename_schema" == "type_3" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && $files -eq 1 ]]; then
  1305.                 ren_file=`echo "$movies_title_clean_ter$quality_quality$extension"`;
  1306.                 # Storing movie title in an imdb friendly format
  1307.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1308.                 file_renamed="yes";
  1309.         # Focusing on movies in directories. Type_3 renaming will look like "Title YYYY (Audio_Quality-Video_Quality).ext"
  1310.         elif [[ "$movies_rename_schema" == "type_3" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ "$(cat "$log_file" | egrep -i "$movies_extensions_rev")" && -d "$source" ]]; then
  1311.                 ren_file=`echo "$movies_title_clean_ter$quality_quality$extension"`;
  1312.                 # Storing movie title in an imdb friendly format
  1313.                 imdb_title=`echo "$(basename "$movies_title_clean_ter")$movie_year_ter" | sed "s/^[. _-]*//g" | sed "s/[. _-]*$//g" | sed "s; [aA][nN][dD] ; ;g" | sed "s; ;+;g"`;
  1314.                 file_renamed="yes";
  1315.         fi
  1316.         # Now we ll apply the renaming
  1317.         if [[ "$file_renamed" != "yes" && "$item" != "$title_clean_ter" ]] && [[ ! "$(echo "$line" | egrep -i "\.iso$|\.img$")" || ! "$(cat "$log_file" | egrep -i "\.dvd$")" ]] && [[ -d "$source" || "$(echo "$line" | egrep -i "$supported_extensions_rev")" ]]; then ren_file="$title_clean_ter$extension"; fi
  1318.         bis="_bis"
  1319.         ren_location=`echo "$(dirname "$source")/$ren_file"`;
  1320.         ren_temp_location=`echo "$(dirname "$source")/$ren_file$bis"`;
  1321.         source_bis=`echo "$line"`;
  1322.         source_ter=$(echo "$line" | sed "s;\([][]\);\\\\\1;g") && source_ter=`echo "$source_ter"`;
  1323.         # Removing brackets
  1324.         ren_location_bis=$(echo "$ren_location" | sed "s;\([][)(]\);\\\\\1;g") && ren_location_bis=`echo "$ren_location_bis"`;
  1325.         if [ "$clean_up_filenames" == "yes" ] && [ "$has_display" == "yes" ] && [ "$item" != "$ren_file" ]; then echo "- Renaming $item to $ren_file";  fi
  1326.         # Working around Mac OS X case insensitive filesystem
  1327.         if [ "$clean_up_filenames" == "yes" ] && [[ -d "$ren_location" && "$(dirname "$source")/" == "$temp_folder" && "$item" != "$ren_file" ]]; then mv -f "$source" "$ren_temp_location"; rm -rf "$ren_location"; source="$ren_temp_location"; fi
  1328.         # Renaming file the bsd sed way
  1329.         if [ "$clean_up_filenames" == "yes" ] && [ "$item" != "$ren_file" ] && [ "$gnu_sed_available" != "yes" ]; then mv -f "$source" "$ren_location" && sed -i '' "s;^$source_ter;$ren_location_bis;g" "$log_file"
  1330.         # Renaming file the gnu sed way
  1331.         elif [ "$clean_up_filenames" == "yes" ] && [ "$item" != "$ren_file" ] && [ "$gnu_sed_available" == "yes" ]; then mv -f "$source" "$ren_location" && sed -i "s;^$source_ter;$ren_location_bis;g" "$log_file"
  1332.         fi
  1333. done
  1334. fi
  1335.  
  1336.  
  1337. # Resetting folder_short value the bsd sed way
  1338. if [[ "$folder_short" && "$tv_shows_fix_numbering" == "yes" && "$gnu_sed_available" != "yes" ]] || [[ "$folder_short" && "$clean_up_filenames" == "yes" && "$gnu_sed_available" != "yes" ]] || [[ "$folder_short" && "$imdb_funct_on" == "yes" && "$gnu_sed_available" != "yes" ]]; then folder_short=`echo "$(cat "$log_file" | sed -n '$p' | sed 's;.*/;;g')"`; sed -i '' '$d' "$log_file"
  1339. # Resetting folder_short value the gnu sed way
  1340. elif [[ "$folder_short" && "$tv_shows_fix_numbering" == "yes" && "$gnu_sed_available" == "yes" ]] || [[ "$folder_short" && "$clean_up_filenames" == "yes" && "$gnu_sed_available" == "yes" ]] || [[ "$folder_short" && "$imdb_funct_on" == "yes" && "$gnu_sed_available" != "yes" ]]; then folder_short=`echo "$(cat "$log_file" | sed -n '$p' | sed 's;.*/;;g')"`; sed -i '$d' "$log_file"
  1341. fi
  1342. # Generating a surrounding folder for movies if force_single_file_movies_folder is turned on
  1343. if [[ "$force_single_file_movies_folder" == "yes" && ! "$folder_short" ]]; then
  1344.         source_file=`echo "$(cat "$log_file")"`
  1345.         if [[ "$(echo "$source_file" | egrep -i "$movies_extensions_rev")" ]] && [[ "$(echo "$quality" | egrep -i "$movies_detect_patterns_rev" )" || "$(echo "$quality" | egrep -i "$movies_detect_patterns_pt_2_rev" )" ]] && [[ "$(echo "$source_file" | egrep -v "[sS][0-9][0-9][eE][0-9][0-9]")" && "$(echo "$source_file" | egrep -v "[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]")" ]]; then
  1346.                 folder_short=`echo "$source_file" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`
  1347.                 new_destination=`echo "$temp_folder$folder_short/"`
  1348.                 mkdir -p "$new_destination"
  1349.                 mv -f "$source_file" "$new_destination"
  1350.                 echo "$(find "$temp_folder$folder_short" -maxdepth 1 -mindepth 1 -type f | egrep -i "$supported_extensions_rev")" > "$log_file"
  1351.         fi
  1352. fi
  1353.  
  1354.  
  1355. ## IMDB routine. This will generate NFO, Poster and fanart files
  1356. if [ "$imdb_title" ] && [ "$imdb_funct_on" == "yes" ] && [ "$subtitles_mode" != "yes" ]; then
  1357.         if [ "$has_display" == "yes" ]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Generating NFO and downloading Poster"; fi
  1358.        
  1359.         # Starting debug log for imdb features
  1360.         if [[ "$debug_mode" == "yes" ]] && [ "$imdb_funct_on" == "yes" ]; then
  1361.                 if [ ! -f "$debug_log" ]; then touch "$debug_log"; fi
  1362.                 echo "--> LOG START <--" >> "$debug_log";
  1363.                 echo "Started on $(date)" >> "$debug_log";
  1364.                 # Adding imdb title to the debug log
  1365.                 echo "IMDB Title: $imdb_title" >> "$debug_log";
  1366.                 # Adding imdb poster format to the debug log
  1367.                 if [[ "$debug_mode" == "yes" ]] && [[ "$imdb_poster" == "yes" ]]; then echo "IMDB Poster format: $imdb_poster_format" >> "$debug_log"; fi
  1368.                 # Adding fanart format to the debug log
  1369.                 if [[ "$debug_mode" == "yes" ]] && [[ "$imdb_fanart" == "yes" ]]; then echo "Fanart Poster format: $imdb_fanart_format" >> "$debug_log"; fi
  1370.         fi
  1371.        
  1372.         # Defining the poster format value to fetch in the imdWebService XML
  1373.         if [ "$imdb_poster_format" == "normal" ]; then poster_size="POSTER"
  1374.         elif [ "$imdb_poster_format" == "small" ]; then poster_size="POSTER_SMALL"
  1375.         elif [ "$imdb_poster_format" == "large" ]; then poster_size="POSTER_LARGE"
  1376.         elif [ "$imdb_poster_format" == "full" ]; then poster_size="POSTER_FULL"
  1377.         fi
  1378.        
  1379.         # Downloading imdbWebService XML and storing it in a variable
  1380.         if [[ "$wget_curl" == *wget* ]]; then
  1381.                 # Using wget to fetch data if available
  1382.                 xml_cont=`echo "$("$wget_curl" -q "http://protoss.no-ip.org/imdbWebService.php?m=$imdb_title&o=xml" -O -; wait)"`;
  1383.         elif [[ "$wget_curl" == *curl* ]]; then
  1384.                 # Using curl to fetch data if available
  1385.                 xml_cont=`echo "$("$wget_curl" -silent -i "http://protoss.no-ip.org/imdbWebService.php?m=$imdb_title&o=xml"; wait)"`;
  1386.         fi
  1387.        
  1388.         # Adding XML path to the debug log
  1389.         if [[ "$debug_mode" == "yes" ]]; then echo "IMDB XML URL: http://protoss.no-ip.org/imdbWebService.php?m=$imdb_title&o=xml" >> "$debug_log"; fi
  1390.         if [ "$xml_cont" ]; then
  1391.                 # Getting IMDB URL from the XML file
  1392.                 imdb_url=`echo "$(echo "$xml_cont" | grep "<IMDB_URL>" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | sed 's/<[^>]*>//g')"`;
  1393.                 # Adding IMDB URL to the debug log
  1394.                 if [[ "$debug_mode" == "yes" ]]; then echo "IMDB URL: $imdb_url" >> "$debug_log"; fi
  1395.                 # Getting IMDB ID from the XML file
  1396.                 imdb_id=`echo "$(echo "$xml_cont" | grep "<TITLE_ID>" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | sed 's/<[^>]*>//g')"`;
  1397.                 # Adding IMDB ID to the debug log
  1398.                 if [[ "$debug_mode" == "yes" ]]; then echo "IMDB ID: $imdb_id" >> "$debug_log"; fi
  1399.                 # Getting IMDB Poster URL from the XML file
  1400.                 poster_url=`echo "$(echo "$xml_cont" | grep "<$poster_size>" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//' | sed 's/<[^>]*>//g')"`;
  1401.                 # Adding IMDB Poster URL to the debug log
  1402.                 if [[ "$debug_mode" == "yes" ]]; then echo "IMDB POSTER URL: $poster_url" >> "$debug_log"; fi
  1403.                 # If IMDB Poster URL is available, downloading it
  1404.                 if [[ "$wget_curl" == *wget* ]] && [[ "$imdb_poster" == "yes" && "$poster_url" ]]; then
  1405.                         # Using wget to fetch data if available
  1406.                         "$wget_curl" -q "$poster_url" -O "$temp_folder_without_slash/temp_poster"; wait;
  1407.                 elif [[ "$wget_curl" == *curl* ]] && [[ "$imdb_poster" == "yes" && "$poster_url" ]]; then
  1408.                         # Using curl to fetch data if available
  1409.                         "$wget_curl" -# -C - -o "$temp_folder_without_slash/temp_poster" "$poster_url" > /dev/null 2>&1; wait;
  1410.                 fi             
  1411.                 # Indicate IMDB Poster as downloaded to the debug log
  1412.                 if [[ "$debug_mode" == "yes" && -f "$temp_folder_without_slash/temp_poster" ]]; then echo "IMDB Poster downloaded" >> "$debug_log"; fi
  1413.                 # Getting Fanart if the imdb_id is available
  1414.                 if [[ "$imdb_fanart" == "yes" && "$imdb_id" ]]; then
  1415.                         # Downloading TheMovieDataBase XML and storing it in a variable
  1416.                         if [[ "$wget_curl" == *wget* ]]; then
  1417.                                 # Using wget to fetch data if available
  1418.                                 themoviedb_xml_cont=`echo "$("$wget_curl" -q "http://api.themoviedb.org/2.1/Movie.imdbLookup/en/xml/57983e31fb435df4df77afb854740ea9/$imdb_id" -O -; wait)"`;
  1419.                         elif [[ "$wget_curl" == *curl* ]]; then
  1420.                                 # Using curl to fetch data if available
  1421.                                 themoviedb_xml_cont=`echo "$("$wget_curl" -silent -i "http://api.themoviedb.org/2.1/Movie.imdbLookup/en/xml/57983e31fb435df4df77afb854740ea9/$imdb_id"; wait)"`;
  1422.                         fi                     
  1423.                         # Adding XML path to the debug log
  1424.                         if [[ "$debug_mode" == "yes" ]]; then echo "TMDB XML URL: http://api.themoviedb.org/2.1/Movie.imdbLookup/en/xml/57983e31fb435df4df77afb854740ea9/$imdb_id" >> "$debug_log"; fi
  1425.                         # Getting Fanart URL from the XML file
  1426.                         fanart_url=`echo "$(echo "$themoviedb_xml_cont" | grep "backdrop" | grep "size=\"$imdb_fanart_format\"" | sed q | sed 's;.*url="\(.*\.jpg\).*;\1;g')"`;
  1427.                         # Adding Fanart URL to the debug log
  1428.                         if [[ "$debug_mode" == "yes" ]]; then echo "TMDB fanart url: $fanart_url" >> "$debug_log"; fi
  1429.                         # If Fanart URL is available, downloading it
  1430.                         if [[ "$wget_curl" == *wget* ]] && [[ "$imdb_fanart" == "yes" && "$fanart_url" ]]; then
  1431.                                 # Using wget to fetch data if available
  1432.                                 "$wget_curl" -q "$fanart_url" -O "$temp_folder_without_slash/temp_fanart"; wait;
  1433.                         elif [[ "$wget_curl" == *curl* ]] && [[ "$imdb_fanart" == "yes" && "$fanart_url" ]]; then
  1434.                                 # Using curl to fetch data if available
  1435.                                 "$wget_curl" -# -C - -o "$temp_folder_without_slash/temp_fanart" "$fanart_url" > /dev/null 2>&1; wait;
  1436.                         fi
  1437.                         # Indicate Fanart as downloaded to the debug log
  1438.                         if [[ "$debug_mode" == "yes" && -f "$temp_folder_without_slash/temp_fanart" ]]; then echo "Fanart downloaded" >> "$debug_log"; fi
  1439.                 fi
  1440.         fi
  1441.  
  1442.        
  1443.         ## If the torrent only contains one file and imdb features are activated, we ll create a surrounding folder
  1444.         if [ ! "$folder_short" ] && [ "$xml_cont" ]; then
  1445.                 folder_short=`echo "$(basename "$(cat "$log_file")")" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`;
  1446.                 mkdir -p "$temp_folder$folder_short/";
  1447.                 mv -f "$(cat "$log_file")" "$temp_folder$folder_short/"
  1448.         fi
  1449.        
  1450.         ## We ll now generate nfo, poster and fanart. We ll also duplicate them for every movie file
  1451.         if [ "$imdb_url" ]; then
  1452.                 for item in $(find "$temp_folder$folder_short" -type f | egrep -i "$(echo "\.$(echo "$movies_extensions" | sed "s;,srt;;" | sed "s;,idx;;" | sed "s;,sub;;" | sed 's;,;\$\|\\\.;g')$")"); do
  1453.                         nfo_file=`echo "$item" | sed 's/\(.*\)\..*/\1.nfo/'`;
  1454.                         poster=`echo "$item" | sed 's/\(.*\)\..*/\1.jpg/'`;
  1455.                         fanart=`echo "$item" | sed 's/\(.*\)\..*/\1.fanart.jpg/'`;
  1456.                         # Generating NFO with the IMDB URL
  1457.                         if [ "$imdb_url" ] && [ "$imdb_nfo" == "yes" ]; then echo "$imdb_url" > "$nfo_file"; fi
  1458.                         # Indicating in the debug log that NFO has been generated
  1459.                         if [[ "$debug_mode" == "yes" && -f "$nfo_file" ]]; then echo "NFO generated: $nfo_file" >> "$debug_log"; fi
  1460.                         # Generating IMDB Poster
  1461.                         if [ -f "$temp_folder_without_slash/temp_poster" ] && [ "$imdb_poster" == "yes" ]; then cp -f "$temp_folder_without_slash/temp_poster" "$poster"; fi
  1462.                         # Indicating in the debug log that Poster has been saved
  1463.                         if [[ "$debug_mode" == "yes" && -f "$temp_folder_without_slash/temp_poster" ]]; then echo "IMDB Poster generated: $poster" >> "$debug_log"; fi
  1464.                         # Generating Fanart
  1465.                         if [ -f "$temp_folder_without_slash/temp_fanart" ] && [ "$imdb_fanart" == "yes" ]; then cp -f "$temp_folder_without_slash/temp_fanart" "$fanart"; fi
  1466.                         # Indicating in the debug log that Fanart has been saved
  1467.                         if [[ "$debug_mode" == "yes" && -f "$temp_folder_without_slash/temp_fanart" ]]; then echo "Fanart Poster downloaded: $fanart" >> "$debug_log"; fi
  1468.                 done
  1469.         elif [ "$disable_nmj_scan" == "yes" ]; then
  1470.                 if [[ "$debug_mode" == "yes" ]]; then echo "Sorry, I could not find that title on IMDB" >> "$debug_log"; fi
  1471.                 touch "$temp_folder$folder_short/.no_all.nmj";
  1472.         fi
  1473.        
  1474.        
  1475.         # Removing remp poster and temp fanart
  1476.         if [ -f "$temp_folder_without_slash/temp_poster" ]; then rm "$temp_folder_without_slash/temp_poster"; fi
  1477.         if [ -f "$temp_folder_without_slash/temp_fanart" ]; then rm "$temp_folder_without_slash/temp_fanart"; fi
  1478.         # Adding jpg, nfo and nmj to the list of supported extensions
  1479.         supported_extensions_rev="$supported_extensions_rev|\.nfo$|\.jpg$|\.nmj$"
  1480.         movies_extensions_rev="$movies_extensions_rev|\.nfo$|\.jpg$|\.nmj$"
  1481.         # End debug log
  1482.         if [[ "$debug_mode" == "yes" ]]; then
  1483.                 echo "Ended on $(date)" >> "$debug_log";
  1484.                 echo "--> LOG END <--" >> "$debug_log";
  1485.         fi
  1486.         # Generating a brand new log_files with all the new imdb files
  1487.         if [ "$folder_short" ]; then echo "$(find "$temp_folder$folder_short" -maxdepth 1 -mindepth 1 -type f | egrep -i "$supported_extensions_rev")" > "$log_file"; fi
  1488. fi
  1489.  
  1490.  
  1491. ## Convert DTS track from MKV files to AC3, img disc images to iso disc images and creates a folder and a cuesheet for Wii backups
  1492. if [ "$has_display" == "yes" ] && [[ "$dts_post" == "yes" || "$img_post" == "yes" || "$wii_post" == "yes" ]] && [ "$subtitles_mode" != "yes" ] && [ "$(cat "$log_file" | egrep -i "\.mkv$|\.img$|([. _-])wii([. _-])")" ]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Converting DTS track to AC3, IMG to ISO and Creating Wii Cuesheet";  fi
  1493. for line in $(cat "$log_file"); do
  1494.         source_trimmed=`echo "$line" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`
  1495.         source_file=`echo "$line"`
  1496.         source_filename=`echo "$(basename "$line")"`
  1497.         source_dir=`echo "$(dirname "$line")"`
  1498.         # Converting DTS track, converting it to AC3 and adding this track to the file
  1499.         if [ "$(echo "$line" | egrep -i "\.mkv$" )" ] && [ "$dts_post" == "yes" ]; then
  1500.                 if [ "$has_display" == "yes" ]; then echo "- Converting $source_filename from DTS to AC3";  fi
  1501.                 if [ "$has_display" == "yes" ]; then "$mkvdts2ac3_bin" -w "$temp_folder" -k "$source_file"; else "$mkvdts2ac3_bin" -w "$temp_folder" -k "$source_file" > /dev/null 2>&1; fi
  1502.         # Converting .img disk images to .iso. If this doesn t work rename .img to .iso
  1503.         elif [ "$(echo "$line" | egrep -i "\.img$" )" ] && [ "$img_post" == "yes" ]; then
  1504.                 iso=`echo "$source_dir/$source_trimmed.iso"`
  1505.                 if [ "$has_display" == "yes" ]; then echo "- Converting $source_filename to an ISO";  fi
  1506.                 if [ "$has_display" == "yes" ]; then "$ccd2iso_bin" "$source_file" "$iso"; else "$ccd2iso_bin" "$source_file" "$iso" > /dev/null 2>&1; fi
  1507.                 iso_size="$(stat -c %s "$iso")"
  1508.                 # Detecting size of resulting file. If too small it means it failed and is already an .iso
  1509.                 if [ "$iso_size" -lt 1000 ]; then
  1510.                         if [ "$has_display" == "yes" ]; then echo "Actually $source_filename is probably already an ISO";  fi
  1511.                         rm -f "$iso" && mv -f "$source_file" "$iso"
  1512.                 else rm "$source_file"
  1513.                 fi
  1514.                 # Rewriting log_file
  1515.                 if [ "$gnu_sed_available" != "yes" ]; then sed -i '' "s;$source_file;$iso;g" "$log_file"; else sed -i "s;$source_file;$iso;g" "$log_file"; fi
  1516.         # Generating a CloneCD Cuesheet for Wii backups
  1517.         elif [ "$(echo "$line" | egrep -i "([. _-])wii([. _-])" )" ] && [ "$(echo "$line" | egrep -i "\.iso$" )" ] && [ "$wii_post" == "yes" ]; then
  1518.                 source_trimmed=`echo "$line" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`
  1519.                 new_folder=`echo "$temp_folder$source_trimmed/"`
  1520.                 dvd_file=`echo "$new_folder$source_trimmed.dvd"`
  1521.                 # Creating a surrounding folder and making changes to the log_file the bsd sed way
  1522.                 if [ ! "$folder_short" ] && [ "$gnu_sed_available" != "yes" ]; then
  1523.                         mkdir -p "$new_folder" && folder_short="$source_trimmed" && mv -f "$source_file" "$new_folder" && sed -i '' "s;$source_file;$new_folder$source_filename;g" "$log_file"
  1524.                 # Creating a surrounding folder and making changes to the log_file the bsd sed way
  1525.                 elif [ ! "$folder_short" ] && [ "$gnu_sed_available" == "yes" ]; then
  1526.                         mkdir -p "$new_folder" && folder_short="$source_trimmed" && mv -f "$source_file" "$new_folder" && sed -i "s;$source_file;$new_folder$source_filename;g" "$log_file"
  1527.                 fi
  1528.                 if [ "$has_display" == "yes" ]; then echo "- Creating a Cuesheet for $source_filename";  fi
  1529.                 # Writing the Cuesheet
  1530.                 echo "$source_filename" > "$dvd_file"
  1531.                 # Rewriting log_file
  1532.                 echo "$dvd_file" >> "$log_file"
  1533.         fi
  1534. done
  1535.  
  1536.  
  1537. ## Copy or move TV Shows, movies and music to a specific folder
  1538. if [ "$has_display" == "yes" ] && [ "$subtitles_mode" != "yes" ] && [[ "$tv_shows_post" != "no" || "$music_post" != "no" || "$movies_post" != "no" ]]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Taking care of TV Shows, Music and Movie files";  fi
  1539.  
  1540. # If in GUI mode, we turn tv_shows_post, music_post and movies_post from move to copy mode, unless the user says otherwise
  1541. user_confirm_move=0
  1542. if [ "$has_display" == "yes" ] && [[ "$tv_shows_post" == "move" || "$music_post" == "move" || "$movies_post" == "move" ]] && [[ "$alt_dest_enabled" == "yes" ]]; then
  1543.         while [[ "$user_confirm_move" != "1" ]] && [[ "$user_confirm_move" != "2" ]]; do
  1544.                 echo -e "\n\nOops, are you sure you want to move your files and not simply copy them ?\nYour processed torrent may not show up in your destination folder\n\n1) Yes, I'm sure\n2) You may be right, I'd rather copy them\n\n"
  1545.                 read user_confirm_move
  1546.                 if [[ "$user_confirm_move" != "1" ]] && [[ "$user_confirm_move" != "2" ]]; then
  1547.                         echo -e "\n\nUh-oh, I cannot understand your answer, please try again"
  1548.                 elif [[ "$user_confirm_move" == "1" ]] || [[ "$user_confirm_move" == "2" ]]; then
  1549.                         echo -e "\n\nDuly noted\n\n"
  1550.                 fi
  1551.         done
  1552.         if [ "$tv_shows_post" != "no" ] && [[ "$user_confirm_move" == "2" ]]; then
  1553.                 tv_shows_post="copy";
  1554.         fi
  1555.         if [ "$music_post" != "no" ] && [[ "$user_confirm_move" == "2" ]]; then
  1556.                 music_post="copy";
  1557.         fi
  1558.         if [ "$movies_post" != "no" ] && [[ "$user_confirm_move" == "2" ]]; then
  1559.                 movies_post="copy";
  1560.         fi
  1561. fi
  1562.  
  1563. additional_permissions="additional_permissions"
  1564. touch "$temp_folder$additional_permissions"
  1565.  
  1566. if [[ "$tv_shows_post" != "no" || "$music_post" != "no" || "$movies_post" != "no" ]]; then
  1567. for line in $(cat "$log_file"); do
  1568.         source_file=`echo "$line"`
  1569.         source_filename=`echo "$(basename "$line")"`
  1570.        
  1571.         # Getting default values for post
  1572.         if [ "$(echo "$line" | egrep -i "$music_extensions_rev" )" ] && [ "$music_post" != "no" ]; then
  1573.                 new_destination=`echo "$music_post_path"`
  1574.         elif [[ "$(echo "$quality" | egrep -i "$movies_detect_patterns_rev" )" || "$(echo "$quality" | egrep -i "$movies_detect_patterns_pt_2_rev" )" ]] && [ "$(echo "$line" | egrep -i "$movies_extensions_rev" )" ] && [ "$movies_post" != "no" ];
  1575.                 then new_destination=`echo "$movies_post_path"`
  1576.         else new_destination=`echo "$movies_post_path"`
  1577.         fi
  1578.        
  1579.         # Guessing path for /Series/Episode (s) or /Series/Season X/Episode (ss)
  1580.         # or /Series/Season XX/Episode (sss) ordering
  1581.         if [ "$(echo "$source_filename" | egrep -i "([. _])s([0])([0-9])e([0-9])([0-9])([. _])")" ]; then
  1582.                 series_season_v1=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;Season \4;g'`;
  1583.                 series_season_v2=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;Season \3\4;g'`;
  1584.                 series_title=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;\1/;' | sed 's;\(.*\).\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).*;\1/;'`;
  1585.         elif [ "$(echo "$source_filename" | egrep -i "([. _])s([1-9])([0-9])e([0-9])([0-9])([. _])")" ]; then
  1586.                 series_season_v1=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;Season \3\4;g'`;
  1587.                 series_season_v2=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;Season \3\4;g'`;
  1588.                 series_title=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;\1/;' | sed 's;\(.*\).\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).*;\1/;'`;
  1589.         elif [ "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]; then
  1590.                 series_season_v1=`echo "$source_filename" | sed 's;\(.*\).\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).*;Season \2\3\4\5;g'`;
  1591.                 series_season_v2=`echo "$source_filename" | sed 's;\(.*\).\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).*;Season \2\3\4\5;g'`;
  1592.                 series_title=`echo "$source_filename" | sed 's;\(.*\).\([sS]\)\([0-9]\)\([0-9]\)\([eE]\)\([0-9]\)\([0-9]\).*;\1/;' | sed 's;\(.*\).\([0-9]\)\([0-9]\)\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).\([0-9]\)\([0-9]\).*;\1/;'`;
  1593.         fi
  1594.        
  1595.         # Trying to find the path to an existing series
  1596.         if [ "$series_title" ] && [ "$tv_shows_post" != "no" ]; then
  1597.                 series_list=$(ls -d */ "$tv_shows_post_path")
  1598.                 # Looking for a perfect match
  1599.                 if [ "$(echo "$series_list" | egrep -i "^$series_title$")" ]; then
  1600.                         series_title="$(echo -e "$series_list" | egrep -i "^$series_title$" | sed -n '$p')";
  1601.                 # Trying with or without the prefix
  1602.                 elif [ "$(echo "$series_list" | egrep -i "$(echo "$series_title" | sed 's;The ;;' | sed 's;Le ;;'| sed 's;La ;;'| sed 's;Les ;;'| sed 's;El ;;' | sed 's;^;(The |Le |La |Les |El ){0,1};' | sed 's;^\(.*\)/$;^\1/$;')")" ]; then
  1603.                         series_title="$(echo -e "$series_list" | egrep -i "$(echo "$series_title" | sed 's;The ;;' | sed 's;Le ;;'| sed 's;La ;;'| sed 's;Les ;;'| sed 's;El ;;' | sed 's;^;(The |Le |La |Les |El ){0,1};' | sed 's;^\(.*\)$;^\1$;')" | sed -n '$p')";
  1604.                 # Trying with or without year
  1605.                 elif [ "$(echo "$series_list" | egrep -i "$(echo "$series_title" | sed 's;The ;;' | sed 's;Le ;;'| sed 's;La ;;'| sed 's;Les ;;'| sed 's;El ;;' | sed 's;^;(The |Le |La |Les |El ){0,1};' | sed 's; \([0-9][0-9][0-9][0-9]\)$;;' | sed 's;$;( [0-9][0-9][0-9][0-9]){0,1};' | sed 's;^\(.*\)/$;^\1/$;')")" ]; then
  1606.                         series_title="$(echo -e "$series_list" | egrep -i "$(echo "$series_title" | sed 's;The ;;' | sed 's;Le ;;'| sed 's;La ;;'| sed 's;Les ;;'| sed 's;El ;;' | sed 's;^;(The |Le |La |Les |El ){0,1};' | sed 's; \([0-9][0-9][0-9][0-9]\)$;;' | sed 's;$;( [0-9][0-9][0-9][0-9]){0,1};' | sed 's;^\(.*\)/$;^\1/$;')" | sed -n '$p')";
  1607.                 # Lookup failed. We ll use the episode name
  1608.                 fi
  1609.         fi
  1610.        
  1611.         # Reverting to default if tv_shows_post_path_mode is disabled
  1612.         if [[ "$(echo "$line" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$line" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [ "$(echo "$line" | egrep -i "$tv_show_extensions_rev" )" ] && [[ "$tv_shows_post" != "no" && "$tv_shows_post_path_mode" == "no" ]]; then new_destination=`echo "$tv_shows_post_path"`; fi
  1613.        
  1614.         # Adding surrounding folder to the destination path variable
  1615.         old_destination="$new_destination";
  1616.         if [ "$folder_short" ]; then new_destination=`echo "$new_destination$folder_short/"`; fi
  1617.        
  1618.         # Defining destination path to be /Series/Episode (s) or /Series/Season X/Episode (ss)
  1619.         # or /Series/Season XX/Episode (sss) depending on user setting in variable tv_shows_post_path_mode
  1620.         if [[ "$(echo "$source_filename" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [ "$(echo "$line" | egrep -i "$tv_show_extensions_rev" )" ] && [[ "$tv_shows_post" != "no" && "$tv_shows_post_path_mode" == "s" ]]; then
  1621.                 new_destination=`echo "$tv_shows_post_path$series_title"`;
  1622.         elif [[ "$(echo "$source_filename" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [ "$(echo "$line" | egrep -i "$tv_show_extensions_rev" )" ] && [[ "$tv_shows_post" != "no" && "$tv_shows_post_path_mode" == "ss" ]]; then
  1623.                 new_destination=`echo "$tv_shows_post_path$series_title$series_season_v1/"`;
  1624.         elif [[ "$(echo "$source_filename" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [ "$(echo "$line" | egrep -i "$tv_show_extensions_rev" )" ] && [[ "$tv_shows_post" != "no" && "$tv_shows_post_path_mode" == "sss" ]]; then
  1625.                 new_destination=`echo "$tv_shows_post_path$series_title$series_season_v2/"`;
  1626.         fi
  1627.        
  1628.         # Copying TV Shows
  1629.         if [[ "$(echo "$source_filename" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [[ "$(echo "$source_filename" | egrep -i "$tv_show_extensions_rev")" && "$tv_shows_post" == "copy" ]]; then
  1630.                 new_dest_bis="$(echo "$new_destination" | sed "s;^$tv_shows_post_path;;" | sed "s;/;\\\n;g")";
  1631.                 dirpath="$tv_shows_post_path";
  1632.                 for d in $(echo -e "$new_dest_bis"); do
  1633.                         dirpath="$dirpath$d/"
  1634.                         if [ ! -d "$dirpath" ] && [[ "$(cat "$temp_folder$additional_permissions" | grep -o "^$dirpath$")" == "" ]]; then
  1635.                                 echo "$dirpath" >> "$temp_folder$additional_permissions";
  1636.                         fi
  1637.                 done
  1638.                 if [ ! -d "$new_destination" ]; then mkdir -p "$new_destination"; fi;
  1639.                 if [ "$has_display" == "yes" ]; then
  1640.                                 echo "- Copying $source_filename to $new_destination";
  1641.                 fi
  1642.                 cp -f "$source_file" "$new_destination"
  1643.                 echo "$new_destination$source_filename" >> "$temp_folder$additional_permissions"
  1644.        
  1645.         # Moving TV Shows
  1646.         elif [[ "$(echo "$source_filename" | egrep -i "([. _])s([0-9])([0-9])e([0-9])([0-9])([. _])" )" || "$(echo "$source_filename" | egrep -i "([. _])([0-9])([0-9])([0-9])([0-9]).([0-9])([0-9]).([0-9])([0-9])([. _])")" ]] && [[ "$(echo "$source_filename" | egrep -i "$tv_show_extensions_rev")" && "$tv_shows_post" == "move" ]]; then
  1647.                 new_dest_bis="$(echo "$new_destination" | sed "s;^$tv_shows_post_path;;" | sed "s;/;\\\n;g")";
  1648.                 dirpath="$tv_shows_post_path";
  1649.                 for d in $(echo -e "$new_dest_bis"); do
  1650.                         dirpath="$dirpath$d/"
  1651.                         if [ ! -d "$dirpath" ] && [[ "$(cat "$temp_folder$additional_permissions" | grep -o "^$dirpath$")" == "" ]]; then
  1652.                                 echo "$dirpath" >> "$temp_folder$additional_permissions";
  1653.                         fi
  1654.                 done
  1655.                 if [ ! -d "$new_destination" ]; then mkdir -p "$new_destination"; fi;
  1656.                 if [ "$has_display" == "yes" ]; then
  1657.                         echo "- Moving $source_filename to $new_destination";
  1658.                 fi
  1659.                 mv -f "$source_file" "$new_destination";
  1660.                 echo "$new_destination$source_filename" >> "$temp_folder$additional_permissions";
  1661.                 if [[ "$gnu_sed_available" == "yes" ]]; then
  1662.                         sed -i "s;$source_file;$new_destination$source_filename;g" "$log_file";
  1663.                 else
  1664.                         sed -i '' "s;$source_file;$new_destination$source_filename;g" "$log_file";
  1665.                 fi
  1666.        
  1667.         # Copying Music and movies
  1668.    elif [[ "$(echo "$line" | egrep -i "$music_extensions_rev")" && "$music_post" == "copy" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$quality" | egrep -i "$movies_detect_patterns_rev" )" && "$movies_post" == "copy" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$quality" | egrep -i "$movies_detect_patterns_pt_2_rev" )" && "$movies_post" == "copy" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$line" | egrep -i "$movies_detect_patterns_rev" )" && "$movies_post" == "copy" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$line" | egrep -i "$movies_detect_patterns_pt_2_rev" )" && "$movies_post" == "copy" ]]; then
  1669.         if [ ! "$folder_short" ] && [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$force_single_file_movies_folder" == "yes" ]]; then
  1670.                 source_trimmed=`echo "$line" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`;
  1671.                 new_destination=`echo "$new_destination$source_trimmed/"`;
  1672.         fi
  1673.         new_dest_bis="$(echo "$new_destination" | sed "s;^$old_destination;;" | sed "s;/;\\\n;g")";
  1674.                 dirpath="$old_destination";
  1675.                 for d in $(echo -e "$new_dest_bis"); do
  1676.                         dirpath="$dirpath$d/"
  1677.                         if [ ! -d "$dirpath" ] && [[ "$(cat "$temp_folder$additional_permissions" | grep -o "^$dirpath$")" == "" ]]; then
  1678.                                 echo "$dirpath" >> "$temp_folder$additional_permissions";
  1679.                         fi
  1680.                 done
  1681.         if [ ! -d "$new_destination" ]; then mkdir -p "$new_destination"; fi;
  1682.         if [ "$has_display" == "yes" ]; then
  1683.                 echo "- Copying $source_filename to $new_destination";
  1684.         fi
  1685.         cp -f "$source_file" "$new_destination"
  1686.         echo "$new_destination$source_filename" >> "$temp_folder$additional_permissions"
  1687.    
  1688.    # Moving Music and movies
  1689.    elif [[ "$(echo "$line" | egrep -i "$music_extensions_rev")" && "$music_post" == "move" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$quality" | egrep -i "$movies_detect_patterns_rev" )" && "$movies_post" == "move" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$quality" | egrep -i "$movies_detect_patterns_pt_2_rev" )" && "$movies_post" == "move" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$line" | egrep -i "$movies_detect_patterns_rev" )" && "$movies_post" == "move" ]] || [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$(echo "$line" | egrep -i "$movies_detect_patterns_pt_2_rev" )" && "$movies_post" == "move" ]]; then
  1690.         if [ ! "$folder_short" ] && [[ "$(echo "$line" | egrep -i "$movies_extensions_rev")" && "$force_single_file_movies_folder" == "yes" ]]; then
  1691.                 source_trimmed=`echo "$line" | sed 's/\(.*\)\..*/\1/' | sed 's;.*/;;g'`;
  1692.                 new_destination=`echo "$new_destination$source_trimmed/"`;
  1693.         fi
  1694.         new_dest_bis="$(echo "$new_destination" | sed "s;^$old_destination;;" | sed "s;/;\\\n;g")";
  1695.                 dirpath="$old_destination";
  1696.                 for d in $(echo -e "$new_dest_bis"); do
  1697.                         dirpath="$dirpath$d/"
  1698.                         if [ ! -d "$dirpath" ] && [[ "$(cat "$temp_folder$additional_permissions" | grep -o "^$dirpath$")" == "" ]]; then
  1699.                                 echo "$dirpath" >> "$temp_folder$additional_permissions";
  1700.                         fi
  1701.                 done
  1702.         if [ ! -d "$new_destination" ]; then mkdir -p "$new_destination"; fi;
  1703.         if [ "$has_display" == "yes" ]; then
  1704.                 echo "- Moving $source_filename to $new_destination";
  1705.         fi
  1706.         mv -f "$source_file" "$new_destination"
  1707.         echo "$new_destination$source_filename" >> "$temp_folder$additional_permissions"
  1708.         if [[ "$gnu_sed_available" == "yes" ]]; then
  1709.                 sed -i "s;$source_file;$new_destination$source_filename;g" "$log_file";
  1710.         else
  1711.                 sed -i '' "s;$source_file;$new_destination$source_filename;g" "$log_file";
  1712.         fi
  1713.         fi
  1714.         series_title="";
  1715.         series_season_v1="";
  1716.         series_season_v2="";
  1717. done
  1718. fi
  1719.  
  1720.  
  1721. ## Deleting unnecessary surrounding folder
  1722. if [[ "$folder_short" && -d "$temp_folder$folder_short" ]]; then
  1723.         files_in_folder_short=$(ls -1 "$temp_folder$folder_short" | wc -l)
  1724.         if [[ "$music_post" == "move" && $files_in_folder_short -eq 0 ]] || [[ "$tv_shows_post" == "move" && $files_in_folder_short -eq 0 ]] || [[ "$movies_post" == "move" && $files_in_folder_short -eq 0 ]]; then rm -rf "$temp_folder$folder_short" && folder_short="" && echo > "$log_file"; fi
  1725. fi
  1726.  
  1727.  
  1728. ## Move content of temp folder to destination folder
  1729. if [[ "$folder_short" && "$script_failed" != "yes" ]]; then
  1730.         count=1
  1731.         dest=`echo "$destination_folder$folder_short"`
  1732.         # Adding a number into brackets if there s already a directory with the same name
  1733.         while [ -d "$dest" ]; do
  1734.                 if [[ count -eq 1 ]] || [ "$subtitles_mode" == "yes" ]; then
  1735.                         dest=`echo "$destination_folder$folder_short"`;
  1736.                 else
  1737.                         dest=`echo "$destination_folder$folder_short [$count]"`;
  1738.                 fi
  1739.                 count=$(( count + 1 ))
  1740.         done
  1741.         # Moving the directory while renaming it with the optional number into brackets
  1742.         mv -f "$temp_folder$folder_short" "$dest"
  1743.                 if [[ "$gnu_sed_available" == "yes" ]]; then
  1744.                 sed -i "s;^$temp_folder$folder_short;$dest;g" "$log_file";
  1745.         else
  1746.                 sed -i '' "s;^$temp_folder$folder_short;$dest;g" "$log_file";
  1747.         fi
  1748.    folder_short=`echo "$( basename "$dest" )"`
  1749. elif [[ ! "$folder_short" && "$script_failed" != "yes" ]]; then
  1750.         for line in $(cat "$log_file"); do
  1751.                 item=`echo "$line"`;
  1752.                 title_clean=`echo "$(basename "$item")" | sed 's/\(.*\)\..*/\1/'`
  1753.                 extension=`echo "$item" | sed 's;.*\.;.;'`
  1754.                 count=1
  1755.                 dest=`echo "$destination_folder$title_clean$extension"`
  1756.                 # Adding a number into brackets if there s already a file with the same name
  1757.                 while [ -f "$dest" ]; do
  1758.                         if [[ count -eq 1 ]] || [ "$subtitles_mode" == "yes" ]; then
  1759.                                 dest=`echo "$destination_folder$title_clean$extension"`;
  1760.                         else
  1761.                                 dest=`echo "$destination_folder$title_clean [$count]$extension"`;
  1762.                         fi
  1763.                         count=$(( count + 1 ))
  1764.                 done
  1765.                 # Moving the file while renaming it with the optional number into brackets
  1766.                 if [[ "$(echo "$line" | egrep -i "$temp_folder")" && -f "$item" ]]; then mv -f "$item" "$dest"; fi
  1767.                 if [[ "$gnu_sed_available" == "yes" ]]; then
  1768.                 sed -i "s;^$item$;$dest;g" "$log_file";
  1769.         else
  1770.                 sed -i '' "s;^$item$;$dest;g" "$log_file";
  1771.         fi
  1772.         done
  1773. fi
  1774.  
  1775.  
  1776. ## Removing the temp directory and edit the log_file accordingly
  1777. if [ -f "$temp_folder$additional_permissions" ]; then additional_list="$(cat "$temp_folder$additional_permissions")"; fi
  1778. if [[ "$gnu_sed_available" != "yes" ]]; then rm -rf "$temp_folder_without_slash" && sed -i '' "s;^$temp_folder;$destination_folder;g" "$log_file"; else rm -rf "$temp_folder_without_slash" && sed -i "s;^$temp_folder;$destination_folder;g" "$log_file"; fi
  1779.  
  1780.  
  1781. ## Edit files and folders permissions
  1782. if [ "$folder_short" ]; then echo "$destination_folder$folder_short" >> "$log_file"; fi
  1783. all_files_list="$(echo -e "$(cat "$log_file")\n$additional_list")"
  1784. if [[ "$has_display" == "yes" && "$user_perm_post" != "no" ]] || [[ "$has_display" == "yes" && "$files_perm_post" != "no" ]]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Setting permissions";  fi
  1785.  
  1786. # Taking care of file and folders permissions
  1787. for line in $(echo -e "$all_files_list"); do
  1788.         if [[ "$user_perm_post" != "no" && "$group_perm_post" != "no" ]] || [[ "$files_perm_post" != "no" && "$folder_perm_post" != "no" ]]; then
  1789.                 # Chmod part of the routine
  1790.                 item=`echo "$line"`
  1791.                 if [ "$item" == "" ]; then
  1792.                         echo > /dev/null 2>&1
  1793.                 # Chmod files
  1794.                 elif [[ -f "$item" && "$files_perm_post" != "no" ]]; then
  1795.                         chmod "$files_perm_post" "$item"
  1796.                 # Chmod directories
  1797.                 elif [[ -d "$item" && "$folder_perm_post" != "no" ]]; then
  1798.                         chmod "$folder_perm_post" "$item"
  1799.                 fi
  1800.                 # Chown part of the routine
  1801.                 if [ "$item" == "" ]; then
  1802.                         echo > /dev/null 2>&1
  1803.                 # Chown files and directories if run as root
  1804.                 elif [[ $(id -u) -eq 0 && "$user_perm_post" != "no" && "$group_perm_post" != "no" ]] && [[ -f "$item" || -d "$item" ]]; then
  1805.                         chown "$user_perm_post":"$group_perm_post" "$item"
  1806.                 # Chown files and directories with sudo if user interaction is available
  1807.                 elif [[ "$edit_perm_as_sudo" == "yes" && "$has_display" == "yes" && "$user_perm_post" != "no" && "$group_perm_post" != "no" ]] && [[ -f "$item" || -d "$item" ]]; then
  1808.                         sudo chown "$user_perm_post":"$group_perm_post" "$item"
  1809.                 fi
  1810.         fi
  1811. done
  1812.  
  1813.  
  1814. ## Reset timestamp (mtime)
  1815. ## Modification time of the file will be set to the moment the script ends. Useful to find the latest downloads
  1816. if [ "$has_display" == "yes" ] && [ "$reset_timestamp" == "yes" ]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Resetting mtime";  fi
  1817. # setting mtime for files
  1818. for line in $(echo -e "$all_files_list"); do
  1819.         if [ "$reset_timestamp" == "yes" ]; then
  1820.                 item=`echo "$line"`
  1821.                 touch "$line"
  1822.         fi
  1823. done
  1824.  
  1825.  
  1826. ## Use a source / destination log shared with a third party app - Add path to enable
  1827. count=0 && files=$(( $count + $(cat "$log_file"|wc -l) ))
  1828. # If only one file, add its path to the third_party_log file
  1829. if [[ $files -eq 0 ]] && [ "$third_party_log" != "no" ] && [ "$subtitles_mode" != "yes" ]; then echo "" > "$third_party_log"; fi
  1830. if [[ $files -eq 1 ]] && [ "$third_party_log" != "no" ] && [ "$subtitles_mode" != "yes" ]; then echo "$(cat "$log_file")" > "$third_party_log"; fi
  1831. if [[ $files -gt 1 ]] && [ "$third_party_log" != "no" ] && [ "$subtitles_mode" != "yes" ]; then folder_name=`echo "$destination_folder$folder_short"`; echo "$folder_name" > "$third_party_log"; fi
  1832. # If we end up with a directory, add its path to the third_party_log file
  1833. if [ "$third_party_log" != "no" ] && [ "$user_perm_post" == "yes" ] && [ "$subtitles_mode" != "yes" ]; then chown "$user_perm_post":"$group_perm_post" "$third_party_log" && sudo chmod "$files_perm_post" "$third_party_log"; fi
  1834.  
  1835. ## Delete third party log if required
  1836. if [ "$delete_third_party_log" == "yes" ] && [ "$third_party_log" != "no" ] && [ "$subtitles_mode" != "yes" ]; then rm -f "$third_party_log"; fi
  1837.  
  1838.  
  1839. ##################################################################################
  1840. ## Starting the all_files_script and processed_torrent_script
  1841.  
  1842. if [[ "$has_display" == "yes" && "$all_files_script_enabled" == "yes" ]] || [[ "$has_display" == "yes" && "$processed_torrent_script_enabled" == "yes" ]]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Running custom scripts";  fi
  1843.  
  1844.  
  1845. # Consolidating files list
  1846. for line in $(echo -e "$additional_list"); do
  1847.         if [ -f "$line" ]; then
  1848.                 for line2 in $(cat "$log_file"); do
  1849.                         line="$(echo "$line" | grep -v "$(basename "$line2")")"
  1850.                 done
  1851.                 if [ "$line" ]; then
  1852.                         files_list="$files_list\n$line"
  1853.                 fi
  1854.         fi
  1855. done
  1856. for line in $(cat "$log_file"); do
  1857.         if [ -f "$line" ]; then
  1858.                 files_list="$files_list\n$line"
  1859.         fi
  1860. done
  1861.  
  1862. # Correcting IMDB title
  1863. if [ "$imdb_title" ]; then imdb_title="$(echo $imdb_title | sed 's;%28;(;g' | sed 's;%29;);g' | sed 's;\+; ;g' | sed "s;%20; ;g")"; fi
  1864.  
  1865. # Setting IMDB and TMDB variables for all_files_script
  1866. if [ "$all_files_script_variable_1" == "imdb_title" ]; then
  1867.                 all_files_script_variable_1="$imdb_title";
  1868.         elif [ "$all_files_script_variable_1" == "imdb_url" ]; then
  1869.                 all_files_script_variable_1="$imdb_url";
  1870.         elif [ "$all_files_script_variable_1" == "imdb_id" ]; then
  1871.                 all_files_script_variable_1="$imdb_id";
  1872.         elif [ "$all_files_script_variable_1" == "poster_url" ]; then
  1873.                 all_files_script_variable_1="$poster_url";
  1874.         elif [ "$all_files_script_variable_1" == "fanart_url" ]; then
  1875.                 all_files_script_variable_1="$fanart_url";
  1876. fi
  1877. if [ "$all_files_script_variable_2" == "imdb_title" ]; then
  1878.                 all_files_script_variable_2="$imdb_title";
  1879.         elif [ "$all_files_script_variable_2" == "imdb_url" ]; then
  1880.                 all_files_script_variable_2="$imdb_url";
  1881.         elif [ "$all_files_script_variable_2" == "imdb_id" ]; then
  1882.                 all_files_script_variable_2="$imdb_id";
  1883.         elif [ "$all_files_script_variable_2" == "poster_url" ]; then
  1884.                 all_files_script_variable_2="$poster_url";
  1885.         elif [ "$all_files_script_variable_2" == "fanart_url" ]; then
  1886.                 all_files_script_variable_2="$fanart_url";
  1887. fi
  1888. if [ "$all_files_script_variable_3" == "imdb_title" ]; then
  1889.                 all_files_script_variable_3="$imdb_title";
  1890.         elif [ "$all_files_script_variable_3" == "imdb_url" ]; then
  1891.                 all_files_script_variable_3="$imdb_url";
  1892.         elif [ "$all_files_script_variable_3" == "imdb_id" ]; then
  1893.                 all_files_script_variable_3="$imdb_id";
  1894.         elif [ "$all_files_script_variable_3" == "poster_url" ]; then
  1895.                 all_files_script_variable_3="$poster_url";
  1896.         elif [ "$all_files_script_variable_3" == "fanart_url" ]; then
  1897.                 all_files_script_variable_3="$fanart_url";
  1898. fi
  1899. if [ "$all_files_script_variable_4" == "imdb_title" ]; then
  1900.                 all_files_script_variable_4="$imdb_title";
  1901.         elif [ "$all_files_script_variable_4" == "imdb_url" ]; then
  1902.                 all_files_script_variable_4="$imdb_url";
  1903.         elif [ "$all_files_script_variable_4" == "imdb_id" ]; then
  1904.                 all_files_script_variable_4="$imdb_id";
  1905.         elif [ "$all_files_script_variable_4" == "poster_url" ]; then
  1906.                 all_files_script_variable_4="$poster_url";
  1907.         elif [ "$all_files_script_variable_4" == "fanart_url" ]; then
  1908.                 all_files_script_variable_4="$fanart_url";
  1909. fi
  1910. if [ "$all_files_script_variable_5" == "imdb_title" ]; then
  1911.                 all_files_script_variable_5="$imdb_title";
  1912.         elif [ "$all_files_script_variable_5" == "imdb_url" ]; then
  1913.                 all_files_script_variable_5="$imdb_url";
  1914.         elif [ "$all_files_script_variable_5" == "imdb_id" ]; then
  1915.                 all_files_script_variable_5="$imdb_id";
  1916.         elif [ "$all_files_script_variable_5" == "poster_url" ]; then
  1917.                 all_files_script_variable_5="$poster_url";
  1918.         elif [ "$all_files_script_variable_5" == "fanart_url" ]; then
  1919.                 all_files_script_variable_5="$fanart_url";
  1920. fi
  1921.  
  1922. # Setting IMDB and TMDB variables for processed_torrent_script
  1923. if [ "$processed_torrent_script_variable_1" == "imdb_title" ]; then
  1924.                 processed_torrent_script_variable_1="$imdb_title";
  1925.         elif [ "$processed_torrent_script_variable_1" == "imdb_url" ]; then
  1926.                 processed_torrent_script_variable_1="$imdb_url";
  1927.         elif [ "$processed_torrent_script_variable_1" == "imdb_id" ]; then
  1928.                 processed_torrent_script_variable_1="$imdb_id";
  1929.         elif [ "$processed_torrent_script_variable_1" == "poster_url" ]; then
  1930.                 processed_torrent_script_variable_1="$poster_url";
  1931.         elif [ "$processed_torrent_script_variable_1" == "fanart_url" ]; then
  1932.                 processed_torrent_script_variable_1="$fanart_url";
  1933. fi
  1934. if [ "$processed_torrent_script_variable_2" == "imdb_title" ]; then
  1935.                 processed_torrent_script_variable_2="$imdb_title";
  1936.         elif [ "$processed_torrent_script_variable_2" == "imdb_url" ]; then
  1937.                 processed_torrent_script_variable_2="$imdb_url";
  1938.         elif [ "$processed_torrent_script_variable_2" == "imdb_id" ]; then
  1939.                 processed_torrent_script_variable_2="$imdb_id";
  1940.         elif [ "$processed_torrent_script_variable_2" == "poster_url" ]; then
  1941.                 processed_torrent_script_variable_2="$poster_url";
  1942.         elif [ "$processed_torrent_script_variable_2" == "fanart_url" ]; then
  1943.                 processed_torrent_script_variable_2="$fanart_url";
  1944. fi
  1945. if [ "$processed_torrent_script_variable_3" == "imdb_title" ]; then
  1946.                 processed_torrent_script_variable_3="$imdb_title";
  1947.         elif [ "$processed_torrent_script_variable_3" == "imdb_url" ]; then
  1948.                 processed_torrent_script_variable_3="$imdb_url";
  1949.         elif [ "$processed_torrent_script_variable_3" == "imdb_id" ]; then
  1950.                 processed_torrent_script_variable_3="$imdb_id";
  1951.         elif [ "$processed_torrent_script_variable_3" == "poster_url" ]; then
  1952.                 processed_torrent_script_variable_3="$poster_url";
  1953.         elif [ "$processed_torrent_script_variable_3" == "fanart_url" ]; then
  1954.                 processed_torrent_script_variable_3="$fanart_url";
  1955. fi
  1956. if [ "$processed_torrent_script_variable_4" == "imdb_title" ]; then
  1957.                 processed_torrent_script_variable_4="$imdb_title";
  1958.         elif [ "$processed_torrent_script_variable_4" == "imdb_url" ]; then
  1959.                 processed_torrent_script_variable_4="$imdb_url";
  1960.         elif [ "$processed_torrent_script_variable_4" == "imdb_id" ]; then
  1961.                 processed_torrent_script_variable_4="$imdb_id";
  1962.         elif [ "$processed_torrent_script_variable_4" == "poster_url" ]; then
  1963.                 processed_torrent_script_variable_4="$poster_url";
  1964.         elif [ "$processed_torrent_script_variable_4" == "fanart_url" ]; then
  1965.                 processed_torrent_script_variable_4="$fanart_url";
  1966. fi
  1967. if [ "$processed_torrent_script_variable_5" == "imdb_title" ]; then
  1968.                 processed_torrent_script_variable_5="$imdb_title";
  1969.         elif [ "$processed_torrent_script_variable_5" == "imdb_url" ]; then
  1970.                 processed_torrent_script_variable_5="$imdb_url";
  1971.         elif [ "$processed_torrent_script_variable_5" == "imdb_id" ]; then
  1972.                 processed_torrent_script_variable_5="$imdb_id";
  1973.         elif [ "$processed_torrent_script_variable_5" == "poster_url" ]; then
  1974.                 processed_torrent_script_variable_5="$poster_url";
  1975.         elif [ "$processed_torrent_script_variable_5" == "fanart_url" ]; then
  1976.                 processed_torrent_script_variable_5="$fanart_url";
  1977. fi
  1978.  
  1979. # Running the all_files_script
  1980. if [[ "$all_files_script_enabled" == "yes" && -x "$all_files_script" ]] || [[ "$all_files_script_enabled" == "yes" && -x "$(which "$all_files_script")" ]]; then
  1981.         for file_path in $(echo -e "$files_list"); do
  1982.                 name_with_extension="$(basename "$file_path")"
  1983.                 name_without_extension="$(echo "$name_with_extension" | sed 's/\(.*\)\..\{3,4\}$/\1/')"
  1984.                 if [ -e "$file_path" ] && [ "$all_files_script_variable_5" ]; then
  1985.                         "$all_files_script" "$(if [ "$all_files_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_1"; fi)" "$(if [ "$all_files_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_2"; fi)" "$(if [ "$all_files_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_3"; fi)" "$(if [ "$all_files_script_variable_4" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_4" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_4" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_4"; fi)" "$(if [ "$all_files_script_variable_5" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_5" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_5" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_5"; fi)"
  1986.                 elif [ -e "$file_path" ] && [ "$all_files_script_variable_4" ]; then
  1987.                         "$all_files_script" "$(if [ "$all_files_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_1"; fi)" "$(if [ "$all_files_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_2"; fi)" "$(if [ "$all_files_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_3"; fi)" "$(if [ "$all_files_script_variable_4" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_4" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_4" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_4"; fi)"
  1988.                 elif [ -e "$file_path" ] && [ "$all_files_script_variable_3" ]; then
  1989.                         "$all_files_script" "$(if [ "$all_files_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_1"; fi)" "$(if [ "$all_files_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_2"; fi)" "$(if [ "$all_files_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_3"; fi)"
  1990.                 elif [ -e "$file_path" ] && [ "$all_files_script_variable_2" ]; then
  1991.                         "$all_files_script" "$(if [ "$all_files_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_1"; fi)" "$(if [ "$all_files_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$all_files_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$all_files_script_variable_2"; fi)"
  1992.                 else
  1993.                         "$all_files_script" "$(if [ "$all_files_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$all_files_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$file_path"; fi)"
  1994.                 fi
  1995.         done
  1996. fi
  1997.  
  1998. # Setting file_path for processed_torrent_script
  1999. count=0 && files=$(( $count + $(cat "$log_file"|wc -l) ))
  2000. if [[ $files -eq 1 ]]; then
  2001.         file_path="$(cat "$log_file")";
  2002. elif [[ $files -gt 1 ]]; then
  2003.         file_path=`echo "$destination_folder$folder_short"`;
  2004. fi
  2005.  
  2006. # Running the processed_files_script
  2007. if [[ "$processed_torrent_script_enabled" == "yes" && -x "$processed_torrent_script" ]] || [[ "$processed_torrent_script_enabled" == "yes" && -x "$(which "$processed_torrent_script")" ]]; then
  2008.         name_with_extension="$(basename "$file_path")"
  2009.         name_without_extension="$(echo "$name_with_extension" | sed 's/\(.*\)\..\{3,4\}$/\1/')"
  2010.         if [ -e "$file_path" ] && [ "$processed_torrent_script_variable_5" ]; then
  2011.                 "$processed_torrent_script" "$(if [ "$processed_torrent_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_1"; fi)" "$(if [ "$processed_torrent_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_2"; fi)" "$(if [ "$processed_torrent_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_3"; fi)" "$(if [ "$processed_torrent_script_variable_4" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_4" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_4" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_4"; fi)" "$(if [ "$processed_torrent_script_variable_5" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_5" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_5" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_5"; fi)"
  2012.         elif [ -e "$file_path" ] && [ "$processed_torrent_script_variable_4" ]; then
  2013.                 "$processed_torrent_script" "$(if [ "$processed_torrent_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_1"; fi)" "$(if [ "$processed_torrent_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_2"; fi)" "$(if [ "$processed_torrent_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_3"; fi)" "$(if [ "$processed_torrent_script_variable_4" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_4" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_4" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_4"; fi)"
  2014.         elif [ -e "$file_path" ] && [ "$processed_torrent_script_variable_3" ]; then
  2015.                 "$processed_torrent_script" "$(if [ "$processed_torrent_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_1"; fi)" "$(if [ "$processed_torrent_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_2"; fi)" "$(if [ "$processed_torrent_script_variable_3" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_3" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_3" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_3"; fi)"
  2016.         elif [ -e "$file_path" ] && [ "$processed_torrent_script_variable_2" ]; then
  2017.                 "$processed_torrent_script" "$(if [ "$processed_torrent_script_variable_1" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_1"; fi)" "$(if [ "$processed_torrent_script_variable_2" == "file_path" ]; then echo "$file_path"; elif [ "$processed_torrent_script_variable_2" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_2" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$processed_torrent_script_variable_2"; fi)"
  2018.         else
  2019.                 "$processed_torrent_script" "$(if [ "$processed_torrent_script_variable_1" == "name_with_extension" ]; then echo "$name_with_extension"; elif [ "$processed_torrent_script_variable_1" == "name_without_extension" ]; then echo "$name_without_extension"; else echo "$file_path"; fi)"
  2020.         fi
  2021. fi
  2022.  
  2023.  
  2024. ## Restore running environment
  2025. export TR_TORRENT_DIR=""
  2026. export TR_TORRENT_NAME=""
  2027. export torrent=""
  2028. rm -f "$log_file"
  2029.  
  2030. ## This is the subtitles routine. If srt files are found in the subtitles directory, they will be renamed and moved to the destination folder
  2031. if [[ "$subtitles_mode" != "yes" && "$subtitles_handling" != "no" && -d "$subtitles_directory" && "$(find "$subtitles_directory" -maxdepth 1 ! -name "._*" -name "*.srt" -type f)" ]]; then
  2032.         if [ "$has_display" == "yes" ]; then step_number=$(( $step_number + 1 )) && echo "Step $step_number : Fetching new subtitles from the subtitles folder";  fi
  2033.         export subtitles_mode="yes"
  2034.         has_display="no"
  2035.         # Remove invisible Mac OS X files from the subtitles folder
  2036.         for line in $(find "$subtitles_directory" -maxdepth 1 ); do
  2037.                 item=`echo "$line"`
  2038.                 if [[ "$item" == */.AppleDouble* ]] || [[ "$item" == */._* ]] || [[ "$item" == */.DS_Store* ]]; then rm -rf "$item"; fi
  2039.         done
  2040.         # Find subtitles that are available in the subtitles folder
  2041.         for line in $(find "$subtitles_directory" -maxdepth 1 ! -name "._*" -name "*.srt" -type f); do
  2042.                 line=`echo "$line"`
  2043.                 item=`echo "$(basename "$line")"`
  2044.                 item_bis=`echo "$item" | sed 's/\(.*\)\..*/\1/'`
  2045.                 # If a subtitle is found, find the corresponding dummy video file
  2046.                 orig_file=`echo "$(find "$subtitles_directory" -name "$item_bis.*" -type f | egrep -i "\.avi$|\.mkv$|\.divx$|\.mp4$|\.ts$")"`
  2047.                 new_line=`echo "$(cat "$orig_file" | sed '/^ *$/d' | sed 's/\(.*\)\..*/\1\.srt/')"`
  2048.                 # We'll now run this subtitle through torrentexpander once again in order to rename it and move it
  2049.                 # where the movie already is
  2050.                 if [ "$line" != "$new_line" ]; then mv "$line" "$new_line"; fi
  2051.                 "$script_path/torrentexpander.sh" "$new_line" "$destination_folder"
  2052.                 # Removing the subtitle from the srt folder
  2053.                 rm -f "$new_line"
  2054.                 # Removing the dummy video file
  2055.                 rm -f "$orig_file"
  2056.                 # Removing dummy video files older than 30 days
  2057.                 find "$subtitles_directory" -mtime +30 -exec rm -f {} \;
  2058.         done
  2059.         if [ -t 1 ]; then echo "That's All Folks"; fi
  2060. fi
  2061.  
  2062. # Notifying the used that the script is done
  2063. if [ "$has_display" == "yes" ]; then echo "That's All Folks";  fi
  2064.  
  2065. # Resetting exported variables
  2066. export subtitles_mode=""
  2067. export script_updated=""
  2068. export TR_TORRENT_DIR=""
  2069. export TR_TORRENT_NAME=""
  2070. IFS=$SAVEIFS
  2071. export PATH="$path_backup"
  2072.  
  2073. ## Starting the post_run_script
  2074. if [[ "$post_run_script_enabled" == "yes" && -x "$post_run_script" ]] || [[ "$post_run_script_enabled" == "yes" && -x "$(which "$post_run_script")" ]]; then
  2075.         . "$post_run_script"
  2076.         sleep 1
  2077. fi