| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | #!/usr/bin/env bash | 
					
						
							|  |  |  | # This is an attempt to replace xdg-open with something sane. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Source the config file. | 
					
						
							|  |  |  | cfg_file="$HOME/.config/sx-open.cfg" | 
					
						
							|  |  |  | [[ -f "$cfg_file" ]] && { source "$cfg_file"; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-08 08:03:59 +03:00
										 |  |  | usage() { echo "${0##*/} <uri/file>"; } | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | handle_uri() { | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 	local target="$1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | 	for h in "${!uri_handlers[@]}"; do | 
					
						
							| 
									
										
										
										
											2014-10-16 13:46:57 +04:00
										 |  |  | 		[[ "$target" =~ ${uri_handlers[${h}]} ]] && { | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 			${h} "$target" & | 
					
						
							|  |  |  | 			return 0 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	done | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 1 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 19:11:43 +04:00
										 |  |  | handle_fs_target() { | 
					
						
							| 
									
										
										
										
											2014-09-25 19:27:52 +04:00
										 |  |  | 	local target="${1##*file://}" | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 13:48:09 +03:00
										 |  |  | 	target_filename="${target##*/}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for n in "${!filename_handlers[@]}"; do | 
					
						
							|  |  |  | 		[[ "$target_filename" =~ ${filename_handlers[${n}]} ]] && { | 
					
						
							|  |  |  | 			${n} "$target" & | 
					
						
							|  |  |  | 			return 0 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | 	target_mimetype=$(file -ib "$target") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for m in "${!mime_handlers[@]}"; do | 
					
						
							| 
									
										
										
										
											2014-10-16 13:46:57 +04:00
										 |  |  | 		[[ "$target_mimetype" =~ ${mime_handlers[${m}]} ]] && { | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 			${m} "$target" & | 
					
						
							|  |  |  | 			return 0 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	done | 
					
						
							| 
									
										
										
										
											2014-09-25 19:05:17 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 1 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() { | 
					
						
							| 
									
										
										
										
											2014-11-21 13:48:57 +03:00
										 |  |  | 	declare -A uri_handlers | 
					
						
							|  |  |  | 	declare -A mime_handlers | 
					
						
							|  |  |  | 	declare -A filename_handlers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 19:11:43 +04:00
										 |  |  | 	target="$1" | 
					
						
							|  |  |  | 	[[ "$target" ]] || { usage; exit; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 19:27:52 +04:00
										 |  |  | 	if [[ -e "$target" || "$target" == 'file://'* ]]; then | 
					
						
							| 
									
										
										
										
											2014-09-25 19:11:43 +04:00
										 |  |  | 		handle_fs_target "$target" | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		handle_uri "$target" | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 22:16:11 +04:00
										 |  |  | 	[[ "$?" -gt 0 ]] && { echo "No handlers found for $target";	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0 | 
					
						
							| 
									
										
										
										
											2014-09-25 17:26:15 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main "$@" |