|
3 | 3 | ##########
|
4 | 4 | # CONFIG #
|
5 | 5 | ##########
|
6 |
| -HOME=~/.chromecastize |
| 6 | +CONFIG_DIRECTORY=~/.chromecastize |
7 | 7 | SUPPORTED_EXTENSIONS=('mkv' 'avi' 'mp4' '3gp' 'mov' 'mpg' 'mpeg' 'qt' 'wmv' 'm2ts' 'flv')
|
8 | 8 |
|
9 | 9 | SUPPORTED_GFORMATS=('MPEG-4' 'Matroska')
|
@@ -39,13 +39,18 @@ in_array() {
|
39 | 39 | }
|
40 | 40 |
|
41 | 41 | print_help() {
|
42 |
| - echo "Usage: chromecastize.sh [ --mp4 | --mkv ] <videofile1> [ videofile2 ... ]" |
| 42 | + echo "Usage: chromecastize.sh [--mp4 | --mkv | --config=/path/to/config/] <videofile1> [videofile2 ...]" |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | unknown_codec() {
|
46 | 46 | echo "'$1' is an unknown codec. Please add it to the list in a CONFIG section."
|
47 | 47 | }
|
48 | 48 |
|
| 49 | +missing_config_directory() { |
| 50 | + echo 'Missing config directory.' |
| 51 | + print_help |
| 52 | +} |
| 53 | + |
49 | 54 | is_supported_gformat() {
|
50 | 55 | if in_array "$1" "${SUPPORTED_GFORMATS[@]}"; then
|
51 | 56 | return 0
|
@@ -90,7 +95,7 @@ is_supported_ext() {
|
90 | 95 |
|
91 | 96 | mark_as_good() {
|
92 | 97 | # add file as successfully converted
|
93 |
| - echo `$REALPATH "$1"` >> $HOME/processed_files |
| 98 | + echo `$REALPATH "$1"` >> "$PROCESSED_FILES" |
94 | 99 | }
|
95 | 100 |
|
96 | 101 | on_success() {
|
@@ -127,7 +132,7 @@ process_file() {
|
127 | 132 | fi
|
128 | 133 |
|
129 | 134 | # test if it's an `chromecastize` generated file
|
130 |
| - if grep -Fxq "`$REALPATH "$FILENAME"`" $HOME/processed_files; then |
| 135 | + if grep -Fxq "`$REALPATH "$FILENAME"`" "$PROCESSED_FILES"; then |
131 | 136 | echo '- file was generated by `chromecastize`, skipping'
|
132 | 137 | return
|
133 | 138 | fi
|
@@ -202,20 +207,83 @@ if [ -z $REALPATH ]; then
|
202 | 207 | exit 1
|
203 | 208 | fi
|
204 | 209 |
|
205 |
| -# check number of arguments |
| 210 | +# Output help if no arguments were passed. |
206 | 211 | if [ $# -lt 1 ]; then
|
207 | 212 | print_help
|
208 | 213 | exit 1
|
209 | 214 | fi
|
210 | 215 |
|
211 |
| -# ensure that processed_files file exists |
212 |
| -mkdir -p $HOME |
213 |
| -touch $HOME/processed_files |
| 216 | +# Process options. |
| 217 | +while :; do |
| 218 | + case $1 in |
| 219 | + -h|-\?|--help) |
| 220 | + print_help |
| 221 | + exit 0 |
| 222 | + ;; |
| 223 | + --mkv|--mp4) |
| 224 | + OVERRIDE_GFORMAT=${1:2} |
| 225 | + ;; |
| 226 | + --config=?*) |
| 227 | + CONFIG_DIRECTORY=${1#*=} |
| 228 | + ;; |
| 229 | + --config=) |
| 230 | + missing_config_directory |
| 231 | + exit 1 |
| 232 | + ;; |
| 233 | + --config) |
| 234 | + if [ "$2" ]; then |
| 235 | + CONFIG_DIRECTORY=$2 |
| 236 | + shift |
| 237 | + else |
| 238 | + missing_config_directory |
| 239 | + exit 1 |
| 240 | + fi |
| 241 | + ;; |
| 242 | + # Ends all options. Everything that follows is considered a |
| 243 | + # filename. |
| 244 | + --) |
| 245 | + shift |
| 246 | + break |
| 247 | + ;; |
| 248 | + -?*) |
| 249 | + echo "Unknown option $1" |
| 250 | + print_help |
| 251 | + exit 1 |
| 252 | + ;; |
| 253 | + *) |
| 254 | + break |
| 255 | + esac |
| 256 | + shift |
| 257 | +done |
| 258 | + |
| 259 | +# Ensure that our config directory exists and is writable. |
| 260 | +if ! [ -e "$CONFIG_DIRECTORY" ]; then |
| 261 | + if ! mkdir -p "$CONFIG_DIRECTORY" &> /dev/null; then |
| 262 | + echo "Config directory $CONFIG_DIRECTORY does not exist and could not be created." |
| 263 | + exit 1 |
| 264 | + fi |
| 265 | +fi |
| 266 | + |
| 267 | +if ! [ -d "$CONFIG_DIRECTORY" ]; then |
| 268 | + echo "Supplied config directory $CONFIG_DIRECTORY is not a directory." |
| 269 | + exit 1 |
| 270 | +fi |
| 271 | + |
| 272 | +if ! [ -w "$CONFIG_DIRECTORY" ]; then |
| 273 | + echo "Config directory $CONFIG_DIRECTORY is not writeable." |
| 274 | + exit 1 |
| 275 | +fi |
| 276 | + |
| 277 | +# Ensure that the processed file list exists and is writeable. |
| 278 | +PROCESSED_FILES="$CONFIG_DIRECTORY/processed_files" |
| 279 | +if ! touch "$PROCESSED_FILES" &> /dev/null || ! [ -f "$PROCESSED_FILES" ] || ! [ -w "$PROCESSED_FILES" ]; then |
| 280 | + echo "Could not write to settings file $PROCESSED_FILES." |
| 281 | + exit 1 |
| 282 | +fi |
214 | 283 |
|
| 284 | +# Process files. |
215 | 285 | for FILENAME in "$@"; do
|
216 |
| - if [ "$FILENAME" = "--mp4" ] || [ "$FILENAME" = "--mkv" ]; then |
217 |
| - OVERRIDE_GFORMAT=`echo "$FILENAME" | sed 's/^--//'` |
218 |
| - elif ! [ -e "$FILENAME" ]; then |
| 286 | + if ! [ -e "$FILENAME" ]; then |
219 | 287 | echo "File not found ($FILENAME). Skipping..."
|
220 | 288 | elif [ -d "$FILENAME" ]; then
|
221 | 289 | ORIG_IFS=$IFS
|
|
0 commit comments