| 1 | #!/bin/sh |
| 2 | set -e -u |
| 3 | |
| 4 | SCRIPTNAME=termux-share |
| 5 | show_usage () { |
| 6 | echo "Usage: $SCRIPTNAME [-a action] [-c content-type] [-d] [-t title] [file]" |
| 7 | echo "Share a file specified as argument or the text received on stdin if no file argument is given." |
| 8 | echo "" |
| 9 | echo " -a action which action to performed on the shared content:" |
| 10 | echo " edit/send/view (default:view)" |
| 11 | echo " -c content-type content-type to use (default: guessed from file extension," |
| 12 | echo " text/plain for stdin)" |
| 13 | echo " -d share to the default receiver if one is selected" |
| 14 | echo " instead of showing a chooser" |
| 15 | echo " -t title title to use for shared content (default: shared file name)" |
| 16 | echo "" |
| 17 | exit 0 |
| 18 | } |
| 19 | |
| 20 | validate_share () { |
| 21 | SHARETYPE=$1 |
| 22 | case "$SHARETYPE" in |
| 23 | edit) ;; |
| 24 | send) ;; |
| 25 | view) ;; |
| 26 | *) echo "$SCRIPTNAME: Unsupported action: '$SHARETYPE'"; exit 1;; |
| 27 | esac |
| 28 | } |
| 29 | |
| 30 | PARAMS="" |
| 31 | while getopts :ha:c:dt: option |
| 32 | do |
| 33 | case "$option" in |
| 34 | h) show_usage;; |
| 35 | a) validate_share $OPTARG; PARAMS="$PARAMS --es action $OPTARG";; |
| 36 | c) PARAMS="$PARAMS --es content-type $OPTARG";; |
| 37 | d) PARAMS="$PARAMS --ez default-receiver true";; |
| 38 | t) PARAMS="$PARAMS --es title $OPTARG";; |
| 39 | ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1; |
| 40 | esac |
| 41 | done |
| 42 | shift $(($OPTIND-1)) |
| 43 | |
| 44 | if [ $# -gt 1 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi |
| 45 | |
| 46 | if [ $# != 0 ]; then |
| 47 | # Note that the file path can contain whitespace. |
| 48 | @TERMUX_API@ Share $PARAMS --es file "`realpath "$1"`" |
| 49 | else |
| 50 | @TERMUX_API@ Share $PARAMS |
| 51 | fi |