dotfiles

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

0x0.st (5317B)


      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_message () {
     56     printf "Provide only none or one of -p / --primary, -s / --secondary, -b / --clipboard, -u / --url flags.\n"
     57 }
     58 
     59 # https://www.baeldung.com/linux/bash-parse-command-line-arguments
     60 OUR_ARGUMENTS="$(getopt -o hpsbu:x: --long help,debug,primary,secondary,clipboard,url:,null-dot-st-url: -- "$@" )"
     61 eval set -- "$OUR_ARGUMENTS"
     62 while [ -n "$*" ]; do
     63     case "$1" in
     64         -h | --help)
     65             help_message
     66             exit 0
     67             shift
     68             ;;
     69         --debug)
     70             debug_flag=1
     71             shift
     72             ;;
     73         -p | --primary)
     74             xsel_flag_primary="$1"
     75             if [ -n "$xsel_flag_secondary" ] || [ -n "$xsel_flag_clipboard" ] || [ -n "$url_flag" ]; then
     76                 print_provide_one_and_only_one_flag_message
     77                 help_message
     78                 exit 1
     79             fi
     80             shift
     81             ;;
     82         -s | --secondary)
     83             xsel_flag_secondary="$1"
     84             if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_clipboard" ] || [ -n "$url_flag" ]; then
     85                 print_provide_one_and_only_one_flag_message
     86                 help_message
     87                 exit 1
     88             fi
     89             shift
     90             ;;
     91         -b | --clipboard)
     92             xsel_flag_clipboard="$1"
     93             if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_secondary" ] || [ -n "$url_flag" ]; then
     94                 print_provide_one_and_only_one_flag_message
     95                 help_message
     96                 exit 1
     97             fi
     98             shift
     99             ;;
    100         -u | --url)
    101             url_flag="$1"
    102             if [ -n "$xsel_flag_primary" ] || [ -n "$xsel_flag_secondary" ] || [ -n "$xsel_flag_clipboard" ]; then
    103                 print_provide_one_and_only_one_flag_message
    104                 help_message
    105                 exit 1
    106             fi
    107             target_url="$2"
    108             shift 2
    109             ;;
    110         -x | --null-dot-st-url )
    111             null_dot_st_url_flag="$1"
    112             null_dot_st_url="$2"
    113             shift 2
    114             ;;
    115         --)
    116             shift
    117             filename="$1"
    118             if [ -z "$filename$xsel_flag_primary$xsel_flag_secondary$xsel_flag_clipboard$url_flag" ]; then
    119                 print_provide_one_and_only_one_flag_message
    120                 help_message
    121                 exit 1
    122             fi
    123             xsel_flag="$xsel_flag_primary$xsel_flag_secondary$xsel_flag_clipboard"
    124             break
    125             ;;
    126         *)
    127             printf "Something is wrong." 1>&2
    128             exit 1
    129             ;;
    130     esac
    131 done
    132 
    133 if [ -z "$null_dot_st_url_flag" ]; then
    134     null_dot_st_url="$URL_FOR_0X0_DOT_ST"
    135 fi
    136 
    137 if [ -n "$debug_flag" ]; then
    138     print_variables
    139 fi
    140 
    141 remove_temporary_file () {
    142     if [ -n "$debug_flag" ]; then
    143         printf "\nRemoving \"%s\"\n" "$temp_file" 1>&2
    144     fi
    145     rm "$temp_file"
    146 }
    147 
    148 if [ -n "$xsel_flag" ]; then
    149     trap remove_temporary_file EXIT
    150 
    151     temp_file=$(mktemp)
    152 
    153     xsel "$xsel_flag" > "$temp_file"
    154 
    155     if [ -n "$debug_flag" ]; then
    156         cat "$temp_file"
    157     else
    158         curl -F"file=@${temp_file}" "$null_dot_st_url"
    159     fi
    160 elif [ -n "$url_flag" ]; then
    161     if [ -n "$debug_flag" ]; then
    162         curl "$target_url"
    163     else
    164         curl -F"url=$target_url" "$null_dot_st_url"
    165     fi
    166 else
    167     if [ -n "$debug_flag" ]; then
    168         cat "$filename"
    169     else
    170         curl -F"file=@${filename}" "$null_dot_st_url"
    171     fi
    172 fi