aboutsummaryrefslogtreecommitdiffstats
path: root/tmp.sh
blob: 49258048413d7a0a5114ded11d94262034bc9b56 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
#
# "THE BEER-WARE LICENSE" (Revision 42):
# Matěj Cepl, <mcepl@cepl.eu> wrote this file.  As long as you retain
# this notice you can do whatever you want with this stuff. If we meet
# some day, and you think this stuff is worth it, you can buy me a beer
# in return.   Matěj Cepl
#
# Instructions for setting up appropriate folders on
# file.rdu.redhat.com see https://mojo.redhat.com/docs/DOC-14590
# See also variable URL_VARIANTS below for necessary configuration.
set -eu

DEBUG=1

debug() {
    if [ $DEBUG -eq 1 ] ; then
        printf "%s\n" "$@"
    fi
}

error() {
    printf "%s\n" "$@"
    exit 1
}

COMPRESSED_EXTENSIONS=('.jpg' '.webm' '.ogg' '.gz' '.bz2' '.zip' \
                       '.xz' '.odp' '.odt' '.ods' '.mp4' '.mp3' \
                       '.png' '.pdf' '.apk' '.rpm' '.epub' '.mkv' \
                       '.avi' '.jar' '.opus' '.ogv' '.flac' '.msi' \
                       '.m4a')

# THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various
# attributes to correspond to your situation.
# If new section is added (as here I have also 'tmp' for fedorapeople.org)
# and the script has a symlink named as the section (so I have tmp as
# a symlink to this script) it saves the file to the location defined in
# such section when called under the other name.
# So, `tmp filename` saves file filename to fedorapeople for me.

export URL_VARIANTS="{
  'barstool': {
    'base_url': 'mcepl@shell.eng.rdu.redhat.com:public_html/',
    'target_url': 'http://file.rdu.redhat.com/~mcepl/',
    'shorten_api': 'https://url.corp.redhat.com/new?'
  },
  'wotan': {
    'base_url': 'wotan:Export/',
    'target_url': 'https://w3.suse.de/~mcepl/'
  },
  'tmp': {
    'base_url': 'fedorapeople.org:public_html/tmp/',
    'target_url': 'https://mcepl.fedorapeople.org/tmp/',
    'shorten_api': 'https://da.gd/s?url='
  }
}"

# THE ALTERNATIVE WAY
# 
# barstool_base_url='mcepl@shell.eng.rdu.redhat.com:public_html'
# barstool_target_url='http://file.rdu.redhat.com/~mcepl/'
# barstool_shorten_api='https://url.corp.redhat.com/new?'
# 
# wotan_base_url='wotan:Export/'
# wotan_target_url='https://w3.suse.de/~mcepl/'
# 
# tmp_base_url='fedorapeople.org:public_html/tmp/'
# tmp_target_url='https://mcepl.fedorapeople.org/tmp/'
# # tmp_shorten_api='http://is.gd/create.php?format=simple&url='
# tmp_shorten_api='https://da.gd/s?url='
# 
# get() { local x; x="${cur_conf}_$1"; echo "${!x}"; }
# 
# Example usage:
# 
# $ cur_conf=barstool
# 
# $ get base_url
# mcepl@shell.eng.rdu.redhat.com:public_html
# 
# $ get target_url
# http://file.rdu.redhat.com/~mcepl/
# 
# $ cur_conf=wotan
# 
# $ get base_url
# wotan:Export/

# Requires GNU sed
CMDNAME="$(basename "$0"|sed 's/./\L&/g')"
CONFIG="$(echo "$URL_VARIANTS" |tr "'" '"'|jq -r ".$CMDNAME")"
BASE_URL="$(echo "$CONFIG" | jq -r ".base_url")"
TARGET_URL="$(echo "$CONFIG" | jq -r ".target_url")"

[ $# -lt 1 ] && exit 1

for processed_file in "${@:1}" ; do
    st=$(stat -c "%s" "$processed_file")
    base_name="$(basename -- "$processed_file")"
    file_ext="${base_name##*.}"
    debug "size of $processed_file is ${st} bytes"
    debug "extension is ${file_ext}"

    # shellcheck disable=SC2076
    if [ "$st" -gt 1000000 ] && [[ ! " ${COMPRESSED_EXTENSIONS[*]} " =~ " ${file_ext} " ]] ; then
        debug "file ${processed_file} should be compressed (${st} b)"
        gzip --keep --best "${processed_file}" || error 'Compressing the file failed!'
        fname="${processed_file}.gz"
        COMPRESSED=1
    else
        debug "file ${processed_file} should not be compressed (${st} b)"
        fname="${processed_file}"
        COMPRESSED=0
    fi

    chmod 644 "$fname"

    # To make scp happy
    modified_fname="$(echo "${fname}"|tr ':' '_')"
    mv -n "${fname}" "${modified_fname}"

    debug "Unshorten fname=${modified_fname}"

    scp "${modified_fname}" "${BASE_URL}"

    target_URL="${TARGET_URL}$(basename "${modified_fname}")"

    # Make target_URL into a safe URL
    safe_target_URL="${target_URL// /%20}"
    echo "${safe_target_URL}"

    # curl -s 'http://is.gd/create.php?format=simple&url=www.example.com'
    # http://is.gd/MOgh5q
    SHORTEN_API="$(echo "$CONFIG" | jq -r ".shorten_api")"
    if [ "x${SHORTEN_API}" != "xnull" ] ; then
        shortened_URL="$(curl -s "${SHORTEN_API}""${safe_target_URL}")"
        echo "${shortened_URL}"
    fi

    # The script should have no side-effects
    if [ "${COMPRESSED}" -eq 1 ] ; then
        rm "${modified_fname}"
    else
        mv "${modified_fname}" "${fname}"
    fi
done