Skip to content

Instantly share code, notes, and snippets.

@jheidt
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheidt/a64f6dcb39def9ce2af6 to your computer and use it in GitHub Desktop.
Save jheidt/a64f6dcb39def9ce2af6 to your computer and use it in GitHub Desktop.
Create a pidgin smily from an image file
#!/bin/bash
#- add-pidgin-smily 0.1
#- Copyright (C) 2014 Jake Heidt admin@jheidt.com
##
##
## :D :D :D CREATES PIDGIN SMILIES :D :D :D
##
##
## Usage: add-pidgin-smily [options] IMAGE_FILE [SHORTCUT] [DIMENSIONS]
##
## arguments:
##
## IMAGE_FILE: required, the image to make a smiley
## SHORTCUT: optional, if not specified a cleaned version of the filename is used
## DIMENSIONS: optional, if not specified in the format of INTxINT, the default of 80x80 is used
##
## options:
##
## --help Display this usage message
## --verbose Display diagnostic messages
## --replace If the shortcut already exists in pidgin, replace that shortcut, otherwise an error is raised
## --force If a destination file already exists during processing, overwrite that file
## --shortcut=SHORTCUT Alternate way of specifying the shortcut to use
##
## Requires imagemagick to be installed on your machine. It probably already is, however.
##
help=$(grep "^## " "${BASH_SOURCE[0]}" | cut -c 4-)
version=$(grep "^#- " "${BASH_SOURCE[0]}" | cut -c 4-)
option_verbose=0
option_replace=0
option_force=0
option_help=0
option_version=0
option_optimize=0
TEMP=`getopt -o hvrfss: --long help,version,verbose,replace,force,optimize,shortcut: -n 'test.sh' -- "$@"`
eval set -- "$TEMP"
# extract options and their arguments into variables.
while true ; do
case "$1" in
-h|--help)
option_help=1 ;
shift ;;
-o|--optimize)
option_optimize=1 ;
shift ;;
--version)
option_version=1 ;
shift ;;
-r|--replace)
option_replace=1 ;
shift ;;
-v|--verbose)
option_verbose=1 ;
shift ;;
-f|--force)
option_force=1 ;
shift ;;
-s|--shortcut)
case "$2" in
"") shift 2 ;;
*) shortcut_name=$2 ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
if [[ $option_help != 0 ]]; then
echo "option_help: $option_help"
echo "$help"
exit
elif [[ $option_version != 0 ]]; then
echo "$version"
exit
fi
source_image=$1
if [[ -z $source_image ]]; then
echo $help
echo 'ERROR: required image file name'
exit 1
fi
if [[ -z $shortcut_name ]]; then
shortcut_name=$2
fi
dimensions=${3:-80x80}
image_file=$(basename $source_image)
smiley_dir=$HOME/.purple/custom_smiley
smiley_xml=$HOME/.purple/smileys.xml
smiley_xml_backup=$HOME/.purple/smileys-backup.xml
image_tmp=/tmp/${image_file}
smiley_xml_work=/tmp/smileys.xml
if [[ -f "${image_tmp}" ]]; then
rm ${image_tmp}
fi
if [[ -f "${smiley_xml_work}" ]]; then
rm ${smiley_xml_work}
fi
[[ $option_verbose != 0 ]] && echo "[execute] convert $source_image -resize $dimensions\> $image_tmp"
convert $source_image -resize $dimensions\> $image_tmp
if [[ $? != 0 ]]; then
rv = $?
echo "[ERROR] during convert: $rv"
exit $rv
fi
image_name=${image_file%.*}
image_ext=${image_file##*.}
image_ext=${image_ext,,}
if [[ $option_optimize != 0 ]]; then
case "${image_ext}" in
png|gif|bmp)
if which optipng &>/dev/null ; then
optipng --quiet ${image_tmp}
else
[[ $option_verbose != 0 ]] && echo "WARNING: optipng not found in path. can not optimize ${image_file}."
fi
;;
jpg|jpeg)
if which jpegoptim &>/dev/null ; then
jpegoptim --quiet ${image_tmp}
else
[[ $option_verbose != 0 ]] && echo "WARNING: jpegoptim not found in path. can not optimize ${image_file}."
fi
;;
*)
echo "Cant optimize unknown image extension: ${image_ext}"
;;
esac
fi
image_sha1=$(sha1sum $image_tmp)
image_sha1=${image_sha1:0:40}
image_dest=${image_sha1}.${image_ext}
image_dest_path=${smiley_dir}/${image_dest}
if [[ -z ${shortcut_name} ]]; then
shortcut_name=":${image_name//[[:space:]]/_}:"
else
shortcut_name=${shortcut_name/^[:!*]/}
shortcut_name=${shortcut_name/[:!*]$/}
fi
echo "processing: ${image_name}"
if [[ $option_verbose != 0 ]]; then
echo "image_sha1: ${image_sha1}"
echo " shortcut: ${shortcut_name}"
echo "image_name: ${image_name}"
echo " image_ext: ${image_ext}"
echo "image_dest: ${image_dest}"
echo "dimensions: ${dimensions}"
echo " [replace]: ${option_replace}"
echo " [force]: ${option_force}"
echo ""
fi
if [[ $option_replace != 0 ]]; then
xml_start=`head --lines=-3 <(grep -Ev "shortcut='${shortcut_name}'" ${smiley_xml})`
xml_end=`tail --lines=3 <(grep -Ev "shortcut='${shortcut_name}'" ${smiley_xml})`
elif grep -F "shortcut='${shortcut_name}'" ${smiley_xml} &>/dev/null ; then
echo "==================================================="
echo "ERROR - shortcut already exists"
echo ""
grep -F "shortcut='${shortcut_name}'" ${smiley_xml}
echo "==================================================="
echo "[option] --replace not set - can not continue."
exit 1
else
xml_start=$(head --lines=-3 $smiley_xml)
xml_end=$(tail --lines=3 $smiley_xml)
fi
xml_line="\t\t\t<smiley shortcut='${shortcut_name}' checksum='${image_sha1}' filename='${image_dest}'/>"
if [[ -f "${image_dest_path}" ]]; then
[[ $option_verbose != 0 ]] && echo "target image ${image_dest_path} exists. deleting."
rm ${image_dest_path}
fi
#
# make a backup of the xml
#
# [[ $option_verbose != 0 ]] && echo "Making a backup of smileys.xml to $smiley_xml_backup"
# cp $smiley_xml $smiley_xml_backup
echo "$xml_start" > $smiley_xml_work
echo -e "$xml_line" >> $smiley_xml_work
echo "$xml_end" >> $smiley_xml_work
cp $image_tmp $image_dest_path
cp $smiley_xml_work $smiley_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment