aboutsummarylogtreecommitdiffstats
path: root/up
blob: 4ba654d2d71e569722b5284bb8e902a63796d5eb (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env sh

# x0.at paste client

# Vars

AUTOCOPY=1

# Functions

usage () {
  filename=$(basename "$0")
  cat <<- EOF
x0.at paste client

Usage :

# Upload a file
$ $filename [OPTIONS] file
$ cat file.ext | $filename [OPTIONS]

# Upload text
$ echo "upaste rocks" | up
$ up
upaste
really
rocks
C-d C-d

Options :
    -h    Print this usage
    -a    Disable autocopy in clipboard
    -t    Set extension, without dot
EOF
}

upload () {

  # Set vars
  local args
  local extension
  local file

  # Set extension
  if [ -n "$EXTENSION" ]
  then
    extension="filename=.$EXTENSION"
  fi

  if [ -z "$1" ]
  then
    # If argument is empty, upload stdin
    local file="file=@-"

    # If extension not set, set .txt
    if [ -z "$extension" ]
    then
      extension="filename=.txt"
    fi

  else
    # Else upload filename
    local file="file=@\"$1\""
  fi

  args="$file;$extension"

  # Curl call
  curl -s -F "$args" 'https://x0.at/'
}

autocopy () {
  # If xorg or wayland is running
  if env | grep 'DISPLAY' >/dev/null 2>&1
  then
    # If xclip is installed
    if which xclip >/dev/null 2>&1
    then
      xclip "$1"
    # Else if wl-clipboard is installed
    elif which wl-copy >/dev/null 2>&1
    then
      wl-copy -p "$1"
    fi
  fi
}

is_file () {
  # Test if first argument is an existing file
  if [ ! -f "$1" ]
  then
    echo "File \"$1\" does not exist or is not a regular file"
    exit 2
  fi
}

main () {

  # Vars
  local url

  # If no piped data
  if [ -t 0 ]
  then
  
    # If no argument, read stdin and save url
    if [ -z "$1" ]
    then
      url=$(upload)
      # Force a newline
      echo -e "\n"
    # Else upload argument and save url
    else
      # Test if file exists and is a regular file
      if [ -n "$1" ]
      then
        is_file "$1"
      fi
      url=$(upload "$1")
    fi
  
  # Else if piped data
  else
    # Test if argument exists
    if [ -n "$1" ]
    then
      echo "Warning: ignoring file $1..."
    fi
    # Upload and save url
    url=$(upload)
  fi
  
  # Print url
  echo "$url"

  # If autocopy is set
  if [ "$AUTOCOPY" == "1" ]
  then
    # Autocopy url
    autocopy "$url"
  fi
}

# Main

while getopts "hat:" arg
do
  case $arg in
    a)
      AUTOCOPY=0
      ;;
    h)
      usage
      exit
      ;;
    t)
      EXTENSION=${OPTARG}
      ;;
    *)
      usage
      exit 1
      ;;
  esac
done
shift $((OPTIND-1))

main "$@"