dropbox-publish.sh

Sun, 06 Jul 2014 20:46:36 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 06 Jul 2014 20:46:36 +0300
changeset 9
7e8ccf76a84d
parent 8
90cb48cd977f
permissions
-rwxr-xr-x

- added various safeguards to pullreq

8
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #!/bin/bash
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 # dropbox-publish.sh
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 # publishes a file into dropbox, conveniently managed under subfolders
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 dropboxidfile="$(dirname "$0")/dropbox-id"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 if [ ! -f "${dropboxidfile}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 echo "cannot find ${dropboxidfile}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 echo "please create this file, it shall only contain your numeric dropbox ID as found in public links, e.g. 66055976"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 exit 1
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 dropboxid="$(cat ${dropboxidfile})"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 inputfile="$1"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 with_xclip="1"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 if [ -z "$( which xclip )" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 echo "no xclip found, cannot copy public URL into clipboard" >/dev/stderr
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 with_xclip=""
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 if [ -z "${inputfile}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 echo "usage: $0 <file> [directory]"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 exit 1
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 if [ ! -f "${inputfile}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 echo "error: ${inputfile} is not a regular file" >/dev/stderr
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 exit 1
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 year=$( date +%Y )
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 remote="https://dl.dropboxusercontent.com/u/${dropboxid}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 local="${HOME}/Dropbox/Public"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 mime=$( file --brief --mime-type "${inputfile}" )
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 folder=""
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 ext3=$( basename "${inputfile}" |tail -c 5 )
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 if [ -n "$2" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 folder="$2"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 reason="of user input"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 elif [ "$( basename "${inputfile}" |head -c 10 )" = "Screenshot" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 folder="screenshots"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 reason="filename starts with Screenshot"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 elif [ "$( echo ${mime} |head -c 6 )" = "image/" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 folder="images"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 reason="mime type starts with image/"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 elif [ "${mime}" = "text/plain" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 folder="text"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 reason="mime type is text/plain"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 elif [ "$( echo ${mime} |head -c 5 )" = "text/" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 folder="code"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 reason="mime type starts with text/ but is not text/plain"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 elif [ "${ext3}" = ".wad" -o "${ext3}" = ".pk3" -o "${ext3}" = ".pk7" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 folder="wads"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 reason="filename ends with .wad, .pk3 or .pk7"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 else
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 folder="misc"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 reason="no other rule matched"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 echo "using folder \"${folder}\" because ${reason}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 path="${year}/${folder}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 localdir="${local}/${path}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 mkdir -pv "${localdir}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 num=0
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 suffix=""
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 localbasename=$(basename "${inputfile}")
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 root=${localbasename%.*}
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 ext=${localbasename##*.}
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 if [ -n "${ext}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 ext=".${ext}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 while [ -f "${localdir}/${root}${suffix}${ext}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 do
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 let num+=1
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 suffix="-${num}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 done
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 filename="${root}${suffix}${ext}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 cp -iv "${inputfile}" "${localdir}/${filename}"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 url="${remote}/${path}/$(echo "${filename}" |sed 's/ /%20/g')"
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 if [ "${with_xclip}" ]
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 then
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 echo -n "${url}" |xclip
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 fi
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
90cb48cd977f - added dropbox-publish.sh
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 echo "${url}"

mercurial