#!/data/data/com.termux/files/usr/bin/sh
set -e -u

SCRIPTNAME=termux-saf-write
show_usage () {
    echo "Usage: $SCRIPTNAME uri"
    echo "Writes the standard input into an existing file identified by a URI. The previous contents are erased. To create a new file, use termux-saf-create."
    echo " -h  show this help"
    exit 0
}

while getopts :h option
do
    case "$option" in
        h) show_usage;;
        ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
    esac
done
shift $((OPTIND-1))

if [ $# != 1 ]; then echo "$SCRIPTNAME: Invalid argument count"; exit 1; fi

/data/data/com.termux/files/usr/libexec/termux-api SAF --es safmethod writeDocument --es uri "$1"
 
