summaryrefslogtreecommitdiff
path: root/etc/functions/put
blob: 62aaebcb4e0b8c47747b5fddbc061ee6069e3d97 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
## vim:ft=zsh
## put - make a file available via HTTP
## supports as many 'failover' hosts as an array can hold
## Usage: put <file>
## Copyright © 2008 - 2010 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>

typeset hostname ssh_name
typeset -a hosts
typeset target_dir server_prefix max_size
typeset file remote_file illegal
typeset -i pic make_random

zmodload zsh/stat

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_flux {
	hostname='flux.derf0.net'
	ssh_name='flux'
	target_dir='web/net.derf0.flux/out/tmp'
	server_prefix='http://flux.derf0.net/tmp/'
	max_size=700000
}

function put_ghost {
	hostname='ghost.derf0.net'
	ssh_name='ghost'
	target_dir='web/net.derf0.ghost/out/tmp'
	server_prefix='http://ghost.derf0.net/tmp/'
	max_size=0
}

function put_steel {
	hostname='steel.derf0.net'
	ssh_name='steel'
	target_dir='web/net.derf0.steel/out/tmp/'
	server_prefix='http://steel.derf0.net/tmp/'
	max_size=0
}

function put_steel_pic {
	hostname='steel.derf0.net'
	ssh_name='steel'
	target_dir='web/org.finalrewind.lib/out/p/'
	server_prefix='http://lib.finalrewind.org/p/'
	max_size=0
}

function put_chaosdorf {
	hostname='frontend.chaosdorf.de'
	ssh_name='chaosdorf-web'
	target_dir='public_html/tmp/'
	server_prefix='http://www.chaosdorf.de/~derf/tmp/'
	max_size=0
}

hosts=(ghost steel chaosdorf)

while [[ ${1} == -* ]] {
	case ${1} in
		-|--) shift; break ;;
		-p)   pic=1 ;;
		-r)   make_random=1 ;;
		*)
			if [[ -n ${functions[put_${1#-}]} ]]; then
				hosts=(${1#-})
			else
				(( $(eval echo '$#'hosts_${1#-}) )) && eval hosts=\(\$hosts_${1#-}\)
			fi
		;;
	esac
	shift
}

if [[ ! -e ${1} ]] {
	echo "${1}: No such file" >&2
	return 1
}

if [[ -d ${1} ]] {
	tar -C ${1:h} -cJf /tmp/${1:t}.tar.xz ${1:t}
	file=/tmp/${1:t}.tar.xz
} else {
	file=${1}
}

remote_file=${file:t}

if (( pic )) {
	hosts=(steel_pic)
}

if (( make_random )) {
	remote_file=$(mktemp -u XXXXXX)
}

while (( ${#hosts} )) {

	put_${hosts[1]}
	ping -c 1 ${hostname} &> /dev/null || ping6 -c 1 ${hostname} &> /dev/null
	if (( ? == 0 )) {

		if (( max_size > 0 && $(zstat +size ${file}) > max_size && ${#hosts} > 1 )) {
			shift hosts
			continue
		}

		if [[ ${remote_file} == ${~illegal} ]] {
			remote_file=${remote_file:r}
		}
		lftp -c "connect sftp://${ssh_name}; cd ${target_dir}; put ${file}"\
" -o ${remote_file}; chmod 644 ${remote_file}"
		if $(curl -fI ${server_prefix}${remote_file} &> /dev/null); then
			echo ${server_prefix}${remote_file}
			return 0
		fi
	}
	shift hosts
}
return 1