summaryrefslogtreecommitdiff
path: root/etc/functions/put
blob: 4fe8279dd18e20cb8037c169c0b3846b2022d38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
## vim:ft=zsh
## put - make a file available via HTTP
## supports as many 'failover' hosts as an array can hold
## Usage: put <file>
## Copyright (C) 2008, 2009 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>

typeset hostname
typeset -a ssh_options hosts
typeset target_dir server_prefix
typeset file remote_file illegal
typeset -i n_hosts n_ping n_curl

if [[ -z ${commands[curl]} ]]; then
	function curl {return 0}
	print -P "%B%F{yellow}curl not present, won't be able to check upload success%f%b"
fi

function put_aneurysm_int {
	put_aneurysm
	hostname='aneurysm'
}

function put_aneurysm () {
	hostname='derf.homelinux.org'
	ssh_options=(-q)
	target_dir='public_html/tmp/'
	server_prefix='https://derf.homelinux.org/tmp/'
}

function put_becquerel () {
	hostname='becquerel.illdefined.org'
	ssh_options=(-q)
	target_dir='www/tmp/'
	server_prefix='http://static.illdefined.org/~derf/tmp/'
}

hosts=(aneurysm_int aneurysm becquerel)
hosts_nossl=(becquerel)

while [[ ${1} == -* ]] {
	case ${1} in
		-|--) shift; break ;;
		*)
			(( $(eval echo '$#'hosts_${1#-}) )) && eval hosts=\(\$hosts_${1#-}\)
		;;
	esac
	shift
}


n_hosts=${#hosts}
if [[ -f ${1} ]] {
	file=${1}
	while (( ${#hosts} )) {
		remote_file=${file:t}
		put_${hosts[1]}
		ping -c 1 ${hostname} &> /dev/null || ping6 -c 1 ${hostname} &> /dev/null
		if (( ? == 0 )) {
			if [[ ${remote_file} == ${~illegal} ]] {
				remote_file=${remote_file:r}
			}
			scp ${ssh_options} ${file} ${hostname}:${target_dir}/${remote_file}
			ssh ${ssh_options} ${hostname} "chmod 644 ${target_dir}${remote_file}"
			if $(curl -fI ${server_prefix}${remote_file} &> /dev/null); then
				echo ${server_prefix}${remote_file}
				return 0
			else
				(( n_curl++ ))
			fi
		} else {
			(( n_ping++ ))
		}
		shift hosts
	}
	echo "Tried uploading to ${n_hosts} hosts without success" >&2
	((n_ping)) && echo " - ${n_ping} hosts are unreachable"    >&2
	((n_curl)) && echo " - ${n_curl} errors while uploading"   >&2
	return 1
} else {
	echo "${1}: No such file"
	return 1
}