Advertisement
Guest User

отполировано. вроде

a guest
Mar 9th, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.81 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. set -e
  4. shopt -s extglob
  5.  
  6. TEMPFILE= TEMPDIR= DIR=
  7.  
  8. usage () {
  9. cat <<EOF
  10. This script saves image from cliboard into
  11. a png file which name is sha256sum of that
  12. image.
  13. This script uses xclip and therefore is
  14. valid only for X11 sessions.
  15.  
  16. Options:
  17. -1          Nothing will work without
  18.             this option. Don't ask why.
  19. -h (--help):        Show this help.
  20. -d (--directory):   Specify target directory.
  21.             Otherwise current is used.
  22. -t (--tempdir):     Set directory for temp file.
  23. -f (--tfile):       Set temporary file name.
  24. EOF
  25. }
  26.  
  27. validate_and_set_tfile () {
  28. # $1 is tempdir
  29. local files=(
  30.         ".image"
  31.         "._image"
  32.         ".__image"
  33.         ".IMAGE"
  34.         "._IMAGE"
  35.         ".__IMAGE"
  36.         )
  37.  
  38. for file in "${files[@]}"
  39. do
  40.     if [[ ! -a ${1}/$file ]]
  41.     then
  42.         TEMPFILE="$file"
  43.         return 0
  44.     fi
  45. done
  46. cat <<EOF
  47. Could not set tempfile. Try specifying
  48. it manually. The reason is that all
  49. tried files already exist.
  50. EOF
  51. return 1
  52. }
  53.  
  54. validate_and_set_tdir () {
  55. local dirs=(
  56.         "/tmp"
  57.         "/tmp/$USER"
  58.         "/tmp/_$USER"
  59.         "/tmp/__$USER"
  60.         )
  61.  
  62. for dir in "${dirs[@]}"
  63. do
  64.     if [[ -d $dir ]] && touch -c "$dir" &>/dev/null
  65.     then
  66.         TEMPDIR="$dir"
  67.         return 0
  68.     elif mkdir "$dir" &>/dev/null
  69.     then
  70.         TEMPDIR="$dir"
  71.         return 0
  72.     fi
  73. done
  74. cat <<EOF
  75. Could not set tempdir. Try doing it
  76. manually. The reason is that all
  77. tried directories either do not
  78. exist or are inacessible.
  79. EOF
  80. return 1
  81. }
  82.  
  83. validate_tfile () {
  84. # $1 is tempdir, $2 is tempfile.
  85. if [[ -a ${1}/${2} ]]
  86. then
  87.     echo "File ""${1}/${2}"" exists."
  88.     return 1
  89. fi
  90. return 0
  91. }
  92.  
  93. validate_dir () {
  94. if [[ -d $1 ]] && touch -c "$1" &>/dev/null
  95. then
  96.     return 0
  97. fi
  98. cat <<EOF
  99. Could not access "$1" or it does not exist.
  100. EOF
  101. return 1
  102. }
  103.  
  104. if [[ -z $1 || $1 = @(-h|--help) ]]
  105. then
  106.     usage
  107.     exit $(( $# ? 0 : 1 ))
  108. fi
  109.  
  110. while (( $# ))
  111. do
  112.     case $1 in
  113.         -1)
  114.             GO=1
  115.             shift
  116.             ;;
  117.         -d|--directory)
  118.             DIR="$2"
  119.             shift 2
  120.             ;;
  121.         -t|--tempdir)
  122.             TEMPDIR="$2"
  123.             shift 2
  124.             ;;
  125.         -f|--tfile)
  126.             TEMPFILE="$2"
  127.             shift 2
  128.             ;;
  129.         *)
  130.  
  131.             echo "Unknown option ""$1"""
  132.             exit 1
  133.             ;;
  134.     esac
  135. done
  136.  
  137. # Looking if we are ready to go.
  138. if (( ! GO ))
  139. then
  140.     usage
  141.     exit 1
  142. fi
  143.  
  144. # Setting directory where the resulting file
  145. # will be stored
  146. if [[ -n $DIR ]]
  147. then
  148.     validate_dir "$DIR" ||
  149.         exit $?
  150. else
  151.     DIR="$(pwd)"
  152. fi
  153.  
  154. # Setting tempdir
  155. if [[ -n $TEMPDIR ]]
  156. then
  157.     validate_dir "$TEMPDIR" ||
  158.         exit $?
  159. else
  160.     validate_and_set_tdir ||
  161.         exit $?
  162. fi
  163.  
  164. # Setting tempfile
  165. if [[ -n $TEMPFILE ]]
  166. then
  167.     validate_tfile "$TEMPDIR" "$TEMPFILE" ||
  168.         exit $?
  169. else
  170.     validate_and_set_tfile "$TEMPDIR" ||
  171.         exit $?
  172. fi
  173.  
  174. xclip -selection c -t image/png -o > \
  175.     "${TEMPDIR}/${TEMPFILE}"
  176. sha256sum - <"${TEMPDIR}/${TEMPFILE}" |
  177.     cut -d' ' -f 1 |
  178.     cp -n "${TEMPDIR}/${TEMPFILE}" \
  179.     "${DIR}/$(cat -).png"
  180. rm "${TEMPDIR:?}/${TEMPFILE}"
  181. rmdir "$TEMPDIR" &>/dev/null || true
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement