This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
vcf_compressor/vcf_compressor.sh

119 lines
2.8 KiB
Bash
Raw Normal View History

2015-09-05 16:03:34 +02:00
#!/bin/bash
## Copyright (c) 2015 Rodolphe Breard
2015-11-04 20:43:49 +01:00
##
2015-09-05 16:03:34 +02:00
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
2015-11-04 20:43:49 +01:00
##
2015-09-05 16:03:34 +02:00
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
2015-11-04 20:43:49 +01:00
##
2015-09-05 16:03:34 +02:00
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
set -euo pipefail
IFS=$'\n\t'
version () {
echo "VCF Compressor 0.1.0-dev"
exit 0
}
usage () {
exit_status="${1:-1}"
echo "Usage: vcf_compressor [OPTION]... VCF_FILE"
echo ""
echo "Options:"
echo " -o, --output"
2015-09-05 21:38:18 +02:00
echo " --version"
2015-09-05 16:03:34 +02:00
echo " -h, --help"
exit "$exit_status"
}
raw_img () {
line="$1"
echo "$line" | cut -d ":" -f2 | base64 -d
}
2015-09-05 21:25:13 +02:00
compress_photo () {
line="$1"
max_width="300"
max_height="300"
2015-09-05 21:25:13 +02:00
prefix=$(echo "$line" | cut -d ":" -f1)
width=$(raw_img "$line" | identify -format '%w' -)
height=$(raw_img "$line" | identify -format '%h' -)
if [ "$width" -gt "$max_width" ] || [ "$height" -gt "$max_height" ] ; then
picture=$(raw_img "$line" | convert "-" -quality 50 -resize "${max_width}x${max_height}>" "-" | base64 -w0)
echo "$prefix:$picture"
else
echo "$line"
fi
2015-09-05 21:25:13 +02:00
}
process_line () {
line="$1"
echo "$line" | egrep "^PHOTO;" | grep ";ENCODING=b;" >/dev/null 2>&1
2015-09-05 21:25:13 +02:00
if [ $? -eq 0 ]; then
compress_photo "$line"
else
echo "$line"
2015-09-05 16:03:34 +02:00
fi
}
input_file=""
output_file=""
while :; do
test $# -gt 0 || break
case "$1" in
-o|--output)
if [ -n "$2" ]; then
output_file="$2"
shift 2
continue
else
usage
fi
;;
2015-09-05 21:38:18 +02:00
--version)
2015-09-05 16:03:34 +02:00
version
;;
-h|--help)
usage 0
;;
*)
if [ -z "$input_file" ]; then
input_file="$1"
shift
continue
else
usage
fi
;;
esac
done
test -z "$input_file" && usage
2015-09-05 21:25:13 +02:00
if [ ! -f "$input_file" ]; then
echo "$input_file: file not found"
exit 1
fi
2015-09-05 16:03:34 +02:00
if [ -z "$output_file" ]; then
dir=$(dirname "$input_file")
name=$(basename "$input_file" ".vcf")
output_file="$dir/$name.new.vcf"
fi
2015-09-05 21:25:13 +02:00
echo -n > "$output_file"
while IFS='' read -r line || [[ -n "$line" ]]; do
line=$(process_line "$line")
echo "$line" >> "$output_file"
done < "$input_file"