Skip to content

Commit

Permalink
Provide an option to override the config directory.
Browse files Browse the repository at this point in the history
Fixes #44.
  • Loading branch information
pfrenssen committed Aug 8, 2018
1 parent 16a1947 commit 46c0cc5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Install requirements by running e.g. `apt-get install ffmpeg mediainfo` (Debian)
Usage
-----
```
./chromecastize.sh [--mp4 | --mkv] <videofile1> [videofile2 ...]
./chromecastize.sh [--mp4 | --mkv | --config=/path/to/config] <videofile1> [videofile2 ...]
```

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

Authors
-------
Expand Down
90 changes: 79 additions & 11 deletions chromecastize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##########
# CONFIG #
##########
HOME=~/.chromecastize
CONFIG_DIRECTORY=~/.chromecastize
SUPPORTED_EXTENSIONS=('mkv' 'avi' 'mp4' '3gp' 'mov' 'mpg' 'mpeg' 'qt' 'wmv' 'm2ts' 'flv')

SUPPORTED_GFORMATS=('MPEG-4' 'Matroska')
Expand Down Expand Up @@ -39,13 +39,18 @@ in_array() {
}

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

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

missing_config_directory() {
echo 'Missing config directory.'
print_help
}

is_supported_gformat() {
if in_array "$1" "${SUPPORTED_GFORMATS[@]}"; then
return 0
Expand Down Expand Up @@ -90,7 +95,7 @@ is_supported_ext() {

mark_as_good() {
# add file as successfully converted
echo `$REALPATH "$1"` >> $HOME/processed_files
echo `$REALPATH "$1"` >> "$PROCESSED_FILES"
}

on_success() {
Expand Down Expand Up @@ -127,7 +132,7 @@ process_file() {
fi

# test if it's an `chromecastize` generated file
if grep -Fxq "`$REALPATH "$FILENAME"`" $HOME/processed_files; then
if grep -Fxq "`$REALPATH "$FILENAME"`" "$PROCESSED_FILES"; then
echo '- file was generated by `chromecastize`, skipping'
return
fi
Expand Down Expand Up @@ -202,20 +207,83 @@ if [ -z $REALPATH ]; then
exit 1
fi

# check number of arguments
# Output help if no arguments were passed.
if [ $# -lt 1 ]; then
print_help
exit 1
fi

# ensure that processed_files file exists
mkdir -p $HOME
touch $HOME/processed_files
# Process options.
while :; do
case $1 in
-h|-\?|--help)
print_help
exit 0
;;
--mkv|--mp4)
OVERRIDE_GFORMAT=${1:2}
;;
--config=?*)
CONFIG_DIRECTORY=${1#*=}
;;
--config=)
missing_config_directory
exit 1
;;
--config)
if [ "$2" ]; then
CONFIG_DIRECTORY=$2
shift
else
missing_config_directory
exit 1
fi
;;
# Ends all options. Everything that follows is considered a
# filename.
--)
shift
break
;;
-?*)
echo "Unknown option $1"
print_help
exit 1
;;
*)
break
esac
shift
done

# Ensure that our config directory exists and is writable.
if ! [ -e "$CONFIG_DIRECTORY" ]; then
if ! mkdir -p "$CONFIG_DIRECTORY" &> /dev/null; then
echo "Config directory $CONFIG_DIRECTORY does not exist and could not be created."
exit 1
fi
fi

if ! [ -d "$CONFIG_DIRECTORY" ]; then
echo "Supplied config directory $CONFIG_DIRECTORY is not a directory."
exit 1
fi

if ! [ -w "$CONFIG_DIRECTORY" ]; then
echo "Config directory $CONFIG_DIRECTORY is not writeable."
exit 1
fi

# Ensure that the processed file list exists and is writeable.
PROCESSED_FILES="$CONFIG_DIRECTORY/processed_files"
if ! touch "$PROCESSED_FILES" &> /dev/null || ! [ -f "$PROCESSED_FILES" ] || ! [ -w "$PROCESSED_FILES" ]; then
echo "Could not write to settings file $PROCESSED_FILES."
exit 1
fi

# Process files.
for FILENAME in "$@"; do
if [ "$FILENAME" = "--mp4" ] || [ "$FILENAME" = "--mkv" ]; then
OVERRIDE_GFORMAT=`echo "$FILENAME" | sed 's/^--//'`
elif ! [ -e "$FILENAME" ]; then
if ! [ -e "$FILENAME" ]; then
echo "File not found ($FILENAME). Skipping..."
elif [ -d "$FILENAME" ]; then
ORIG_IFS=$IFS
Expand Down

0 comments on commit 46c0cc5

Please sign in to comment.