Skip to content

Commit 46c0cc5

Browse files
committed
Provide an option to override the config directory.
Fixes #44.
1 parent 16a1947 commit 46c0cc5

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Install requirements by running e.g. `apt-get install ffmpeg mediainfo` (Debian)
1616
Usage
1717
-----
1818
```
19-
./chromecastize.sh [--mp4 | --mkv] <videofile1> [videofile2 ...]
19+
./chromecastize.sh [--mp4 | --mkv | --config=/path/to/config] <videofile1> [videofile2 ...]
2020
```
2121

2222
### Examples:
@@ -26,6 +26,7 @@ Usage
2626
### Options:
2727
- `--mp4` forces conversion to MPEG-4 container
2828
- `--mkv` forces conversion to Matroska container
29+
- `--config=/path/to/config` specify where to store configuration. When omitted the default folder `~/.chromecastize` is used.
2930

3031
Authors
3132
-------

Diff for: chromecastize.sh

+79-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
##########
44
# CONFIG #
55
##########
6-
HOME=~/.chromecastize
6+
CONFIG_DIRECTORY=~/.chromecastize
77
SUPPORTED_EXTENSIONS=('mkv' 'avi' 'mp4' '3gp' 'mov' 'mpg' 'mpeg' 'qt' 'wmv' 'm2ts' 'flv')
88

99
SUPPORTED_GFORMATS=('MPEG-4' 'Matroska')
@@ -39,13 +39,18 @@ in_array() {
3939
}
4040

4141
print_help() {
42-
echo "Usage: chromecastize.sh [ --mp4 | --mkv ] <videofile1> [ videofile2 ... ]"
42+
echo "Usage: chromecastize.sh [--mp4 | --mkv | --config=/path/to/config/] <videofile1> [videofile2 ...]"
4343
}
4444

4545
unknown_codec() {
4646
echo "'$1' is an unknown codec. Please add it to the list in a CONFIG section."
4747
}
4848

49+
missing_config_directory() {
50+
echo 'Missing config directory.'
51+
print_help
52+
}
53+
4954
is_supported_gformat() {
5055
if in_array "$1" "${SUPPORTED_GFORMATS[@]}"; then
5156
return 0
@@ -90,7 +95,7 @@ is_supported_ext() {
9095

9196
mark_as_good() {
9297
# add file as successfully converted
93-
echo `$REALPATH "$1"` >> $HOME/processed_files
98+
echo `$REALPATH "$1"` >> "$PROCESSED_FILES"
9499
}
95100

96101
on_success() {
@@ -127,7 +132,7 @@ process_file() {
127132
fi
128133

129134
# 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
131136
echo '- file was generated by `chromecastize`, skipping'
132137
return
133138
fi
@@ -202,20 +207,83 @@ if [ -z $REALPATH ]; then
202207
exit 1
203208
fi
204209

205-
# check number of arguments
210+
# Output help if no arguments were passed.
206211
if [ $# -lt 1 ]; then
207212
print_help
208213
exit 1
209214
fi
210215

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
214283

284+
# Process files.
215285
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
219287
echo "File not found ($FILENAME). Skipping..."
220288
elif [ -d "$FILENAME" ]; then
221289
ORIG_IFS=$IFS

0 commit comments

Comments
 (0)