dotfiles

A steaming hot pile of sh...ell scripts and configuration files.
git clone https://kaka.farm/~git/dotfiles
Log | Files | Refs

0x0.st (5308B)


      1 #!/usr/bin/bash
      2 
      3 set -e
      4 
      5 if [ -z "$(command -v xsel)" ] || [ -z "$(command -v curl)" ]; then
      6     printf "Must install xsel and curl.\n"
      7     exit 1
      8 fi
      9 
     10 if [ -z "${URL_FOR_0X0_DOT_ST}" ]; then
     11     # Default URL:
     12     url_for_0x0_dot_st="0x0.st"
     13 else
     14     url_for_0x0_dot_st="$URL_FOR_0X0_DOT_ST"
     15 fi
     16 
     17 self_executable="$0"
     18 
     19 help_message () {
     20     printf "Usage:\n"
     21     printf "\t%s [--debug] -p, --primay\n" "$self_executable"
     22     printf "\t%s [--debug] -s, --secondary\n" "$self_executable"
     23     printf "\t%s [--debug] -b, --clipboard\n" "$self_executable"
     24     printf "\t%s [--debug] -u URL, --url URL\n" "$self_executable"
     25     printf "\t%s [--debug] -x URL, --null-dot-st-url URL\n" "$self_executable"
     26     printf "\t%s [--debug] FILENAME\n" "$self_executable"
     27     printf "\n"
     28     printf "Flags:
     29     -p, --primary          Operate on the PRIMARY selection.
     30     -s, --secondary        Operate on the SECONDARY selection.
     31     -b, --clipboard        Operate on the CLIPBOARD selection.
     32     -u, --url              Tell 0x0.st to mirror the file at the URL.
     33     -x, --null-dot-st-url  The 0x0.st instance address, instead of 0x0.st itself.
     34     --debug                Show debug information and do not upload to 0x0.st.\n"
     35     printf "\n"
     36     printf "Environment Variables:
     37     URL_FOR_0X0_DOT_ST (all upper case)
     38         Its value will be used for the 0x0.st instance address.
     39         If URL_FOR_0X0_DOT_ST is empty, 0x0.st will be used by default.\n"
     40 }
     41 
     42 print_variables() {
     43     printf "Self executable: '%s'\n" "$self_executable" 1>&2
     44 
     45     printf "Flags:\n" 1>&2
     46     printf "    null_dot_st_url_flag: '%s'\n" "$null_dot_st_url_flag"
     47     printf "    null_dot_st_url: '%s'\n" "$null_dot_st_url"
     48     printf "    xsel_flag: '%s'\n" "$xsel_flag" 1>&2
     49     printf "    target_url: '%s'\n" "$target_url" 1>&2
     50 
     51     printf "Environment Variables:\n" 1>&2
     52     printf "    URL_FOR_0X0_DOT_ST: '%s'\n" "$URL_FOR_0X0_DOT_ST" 1>&2
     53 }
     54 
     55 print_provide_one_and_only_one_flag_error_message_and_exit() {
     56     printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard, -u / --url flags.\n"
     57     help_message
     58     if [ -n "$debug_flag" ]; then
     59         print_variables
     60     fi
     61     exit 1
     62 }
     63 
     64 # https://www.baeldung.com/linux/bash-parse-command-line-arguments
     65 flags_flag=""
     66 OUR_ARGUMENTS="$(getopt -o hpsbu:x: --long help,debug,primary,secondary,clipboard,url:,null-dot-st-url: -- "$@" )"
     67 eval set -- "$OUR_ARGUMENTS"
     68 while [ -n "$*" ]; do
     69     case "$1" in
     70         -h | --help)
     71             help_message
     72             exit 0
     73             shift
     74             ;;
     75         --debug)
     76             debug_flag=1
     77             shift
     78             ;;
     79         -p | --primary)
     80             if [ -z "$1" ]; then
     81                 continue;
     82             elif [ -n "$flags_flag" ]; then
     83                 print_provide_one_and_only_one_flag_error_message_and_exit
     84             else
     85                 flags_flag=1
     86             fi
     87 
     88             xsel_flag="$1"
     89             shift
     90             ;;
     91         -s | --secondary)
     92             if [ -z "$1" ]; then
     93                 continue;
     94             elif [ -n "$flags_flag" ]; then
     95                 print_provide_one_and_only_one_flag_error_message_and_exit
     96             else
     97                 flags_flag=1
     98             fi
     99 
    100             xsel_flag="$1"
    101             shift
    102             ;;
    103         -b | --clipboard)
    104             if [ -z "$1" ]; then
    105                 continue;
    106             elif [ -n "$flags_flag" ]; then
    107                 print_provide_one_and_only_one_flag_error_message_and_exit
    108             else
    109                 flags_flag=1
    110             fi
    111 
    112             xsel_flag="$1"
    113             shift
    114             ;;
    115         -u | --url)
    116             if [ -z "$1" ]; then
    117                 continue;
    118             elif [ -n "$flags_flag" ]; then
    119                 print_provide_one_and_only_one_flag_error_message_and_exit
    120             else
    121                 flags_flag=1
    122             fi
    123 
    124             url_flag="$1"
    125             target_url="$2"
    126             shift 2
    127             ;;
    128         -x | --null-dot-st-url )
    129             null_dot_st_url_flag="$1"
    130             null_dot_st_url="$2"
    131             shift 2
    132             ;;
    133         --)
    134             shift
    135             if [ -z "$1" ]; then
    136                 break
    137             elif [ -n "$flags_flag" ]; then
    138                 print_provide_one_and_only_one_flag_error_message_and_exit
    139             fi
    140 
    141             filename="$1"
    142             break
    143             ;;
    144         *)
    145             printf "Something is wrong." 1>&2
    146             exit 1
    147             ;;
    148     esac
    149 done
    150 
    151 if [ -z "$null_dot_st_url_flag" ]; then
    152     null_dot_st_url="$url_for_0x0_dot_st"
    153 fi
    154 
    155 if [ -n "$debug_flag" ]; then
    156     print_variables
    157 fi
    158 
    159 
    160 
    161 remove_temporary_file () {
    162     if [ -n "$debug_flag" ]; then
    163         printf "\nRemoving \"%s\"\n" "$temp_file" 1>&2
    164     fi
    165     rm "$temp_file"
    166 }
    167 
    168 if [ -n "$xsel_flag" ]; then
    169     trap remove_temporary_file EXIT
    170 
    171     temp_file=$(mktemp)
    172 
    173     xsel "$xsel_flag" > "$temp_file"
    174 
    175     if [ -n "$debug_flag" ]; then
    176         cat "$temp_file"
    177     else
    178         curl -F"file=@${temp_file}" "$null_dot_st_url"
    179     fi
    180 elif [ -n "$url_flag" ]; then
    181     if [ -n "$debug_flag" ]; then
    182         curl "$target_url"
    183     else
    184         curl -F"url=$target_url" "$null_dot_st_url"
    185     fi
    186 else
    187     if [ -n "$debug_flag" ]; then
    188         cat "$filename"
    189     else
    190         curl -F"file=@${filename}" "$null_dot_st_url"
    191     fi
    192 fi