| 
									
										
										
										
											2013-01-20 19:04:38 +04:00
										 |  |  |  | #!/bin/bash | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | # Convert ebooks to Kindle’s Mobi format | 
					
						
							|  |  |  |  | # | 
					
						
							|  |  |  |  | # Usage: anytomobi <file or folder> [destination folder] | 
					
						
							|  |  |  |  | # | 
					
						
							|  |  |  |  | # Author: Artem Sapegin, sapegin.me | 
					
						
							|  |  |  |  | # | 
					
						
							|  |  |  |  | # Require: | 
					
						
							| 
									
										
										
										
											2013-02-12 00:27:49 +04:00
										 |  |  |  | # - Calibre - http://calibre-ebook.com/download (install command line tools from Calibre’s settings) | 
					
						
							| 
									
										
										
										
											2013-01-20 19:04:38 +04:00
										 |  |  |  | # | 
					
						
							|  |  |  |  | # Manuals: | 
					
						
							|  |  |  |  | # - http://manual.calibre-ebook.com/cli/ebook-convert.html | 
					
						
							|  |  |  |  | # - http://manual.calibre-ebook.com/cli/ebook-meta.html | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | function convert_file() { | 
					
						
							|  |  |  |  | 	local infile="$1" | 
					
						
							|  |  |  |  | 	local intype=${infile##*.} | 
					
						
							|  |  |  |  | 	local intype=$(echo $intype | tr '[:upper:]' '[:lower:]') | 
					
						
							|  |  |  |  | 	local infilename=$(basename "$1") | 
					
						
							|  |  |  |  | 	local outfilename="${infilename%.*}.mobi" | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-12 00:21:36 +04:00
										 |  |  |  | 	if [[ "$intype" == "pdf" ]]; then | 
					
						
							|  |  |  |  | 		# Just copy PDF files to destination directory | 
					
						
							| 
									
										
										
										
											2013-01-20 19:04:38 +04:00
										 |  |  |  | 		if [[ "$outdir" != "." ]]; then | 
					
						
							|  |  |  |  | 			cp "$infile" "$outdir/" | 
					
						
							|  |  |  |  | 		fi | 
					
						
							|  |  |  |  | 		return | 
					
						
							|  |  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2013-02-12 00:21:36 +04:00
										 |  |  |  | 		# Convert any other format (MOBI too) to old MOBI format to force left text alignment | 
					
						
							|  |  |  |  | 		ebook-convert "$infile" "$outdir/$outfilename" --output-profile=kindle_pw --mobi-file-type=old \ | 
					
						
							|  |  |  |  | 			--mobi-ignore-margins --mobi-keep-original-images --no-inline-toc --remove-paragraph-spacing \ | 
					
						
							| 
									
										
										
										
											2013-02-12 00:53:38 +04:00
										 |  |  |  | 			--change-justification=left --keep-ligatures --smarten-punctuation --pretty-print --filter-css=color | 
					
						
							| 
									
										
										
										
											2013-01-20 19:04:38 +04:00
										 |  |  |  | 	fi | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	echo | 
					
						
							|  |  |  |  | 	echo "Filename            : $outfilename" | 
					
						
							|  |  |  |  | 	ebook-meta "$outdir/$outfilename" | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | function convert_dir() { | 
					
						
							|  |  |  |  | 	local dir=$1 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for file in "$dir"/*.{mobi,pdf,epub,fb2}; do | 
					
						
							|  |  |  |  | 		if [[ -e "$file" ]]; then | 
					
						
							|  |  |  |  | 			convert_file "$file" | 
					
						
							|  |  |  |  | 		fi | 
					
						
							|  |  |  |  | 	done | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | outdir=${2-.} | 
					
						
							|  |  |  |  | mkdir -p "$outdir" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | if [[ -f "$1" ]]; then | 
					
						
							|  |  |  |  | 	convert_file "$1" | 
					
						
							|  |  |  |  | elif [[ -d "$1" ]]; then | 
					
						
							|  |  |  |  | 	convert_dir "$1" | 
					
						
							|  |  |  |  | else | 
					
						
							| 
									
										
										
										
											2013-02-12 00:21:36 +04:00
										 |  |  |  | 	echo "Usage: `basename $0` <file or folder> [destination folder]" | 
					
						
							| 
									
										
										
										
											2013-01-20 19:04:38 +04:00
										 |  |  |  | 	exit 1 | 
					
						
							|  |  |  |  | fi |