Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- set -e
- shopt -s extglob
- TEMPFILE= TEMPDIR= DIR=
- usage () {
- cat <<EOF
- This script saves image from cliboard into
- a png file which name is sha256sum of that
- image.
- This script uses xclip and therefore is
- valid only for X11 sessions.
- Options:
- -1 Nothing will work without
- this option. Don't ask why.
- -h (--help): Show this help.
- -d (--directory): Specify target directory.
- Otherwise current is used.
- -t (--tempdir): Set directory for temp file.
- -f (--tfile): Set temporary file name.
- EOF
- }
- validate_and_set_tfile () {
- # $1 is tempdir
- local files=(
- ".image"
- "._image"
- ".__image"
- ".IMAGE"
- "._IMAGE"
- ".__IMAGE"
- )
- for file in "${files[@]}"
- do
- if [[ ! -a ${1}/$file ]]
- then
- TEMPFILE="$file"
- return 0
- fi
- done
- cat <<EOF
- Could not set tempfile. Try specifying
- it manually. The reason is that all
- tried files already exist.
- EOF
- return 1
- }
- validate_and_set_tdir () {
- local dirs=(
- "/tmp"
- "/tmp/$USER"
- "/tmp/_$USER"
- "/tmp/__$USER"
- )
- for dir in "${dirs[@]}"
- do
- if [[ -d $dir ]] && touch -c "$dir" &>/dev/null
- then
- TEMPDIR="$dir"
- return 0
- elif mkdir "$dir" &>/dev/null
- then
- TEMPDIR="$dir"
- return 0
- fi
- done
- cat <<EOF
- Could not set tempdir. Try doing it
- manually. The reason is that all
- tried directories either do not
- exist or are inacessible.
- EOF
- return 1
- }
- validate_tfile () {
- # $1 is tempdir, $2 is tempfile.
- if [[ -a ${1}/${2} ]]
- then
- echo "File ""${1}/${2}"" exists."
- return 1
- fi
- return 0
- }
- validate_dir () {
- if [[ -d $1 ]] && touch -c "$1" &>/dev/null
- then
- return 0
- fi
- cat <<EOF
- Could not access "$1" or it does not exist.
- EOF
- return 1
- }
- if [[ -z $1 || $1 = @(-h|--help) ]]
- then
- usage
- exit $(( $# ? 0 : 1 ))
- fi
- while (( $# ))
- do
- case $1 in
- -1)
- GO=1
- shift
- ;;
- -d|--directory)
- DIR="$2"
- shift 2
- ;;
- -t|--tempdir)
- TEMPDIR="$2"
- shift 2
- ;;
- -f|--tfile)
- TEMPFILE="$2"
- shift 2
- ;;
- *)
- echo "Unknown option ""$1"""
- exit 1
- ;;
- esac
- done
- # Looking if we are ready to go.
- if (( ! GO ))
- then
- usage
- exit 1
- fi
- # Setting directory where the resulting file
- # will be stored
- if [[ -n $DIR ]]
- then
- validate_dir "$DIR" ||
- exit $?
- else
- DIR="$(pwd)"
- fi
- # Setting tempdir
- if [[ -n $TEMPDIR ]]
- then
- validate_dir "$TEMPDIR" ||
- exit $?
- else
- validate_and_set_tdir ||
- exit $?
- fi
- # Setting tempfile
- if [[ -n $TEMPFILE ]]
- then
- validate_tfile "$TEMPDIR" "$TEMPFILE" ||
- exit $?
- else
- validate_and_set_tfile "$TEMPDIR" ||
- exit $?
- fi
- xclip -selection c -t image/png -o > \
- "${TEMPDIR}/${TEMPFILE}"
- sha256sum - <"${TEMPDIR}/${TEMPFILE}" |
- cut -d' ' -f 1 |
- cp -n "${TEMPDIR}/${TEMPFILE}" \
- "${DIR}/$(cat -).png"
- rm "${TEMPDIR:?}/${TEMPFILE}"
- rmdir "$TEMPDIR" &>/dev/null || true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement