6 # $HOME/.flag-cache <- Autogenerated flag (refreshed when hostname changes)
7 # /etc/flag <- Site admin's flag
8 # $HOME/.flag <- Local user's flag
9 # Check them in that order.
12 # Hash the passed string into 8 hex characters
13 [ -z "${crc32_bin}" ] && find_crc32_bin
14 if [ -z "${crc32_bin}" ]
16 echo "No crc32 algo found!" >&2
19 echo -n "${*}" | eval "${crc32_bin}" | awk '{print $1}'
25 for bin in crc32sum crc32sum-linux crc32sum-freebsd
27 bin="$(which "${bin}" 2>/dev/null)"
30 export crc32_bin="${bin}"
31 if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ]
41 for bin in ruby php perl
43 bin="$(which "${bin}" 2>/dev/null)"
47 */ruby) bin="${bin} -e 'require \"zlib\"; printf(\"%08x\n\", Zlib.crc32(STDIN.readlines.join))'" ;;
48 */php) bin="${bin} -r 'printf(\"%08x\n\", crc32(file_get_contents(\"/dev/stdin\")));'" ;;
49 */perl) bin="${bin} -e 'use String::CRC32; printf(\"%08x\n\", crc32(*STDIN));'" ;;
51 export crc32_bin="${bin}"
52 if [ "$(hash_string "meow" 2>/dev/null)" = "8a106afe" ]
62 export crc32_bin="${crc32}"
64 echo "Failed to locate usable crc32 algo" >&2
71 myhost="$(/bin/hostname -f)"
72 cache="${HOME}/.flag-cache"
75 flag_hex="$(awk '/^'${myhost}'/{print $2}' "${cache}")"
77 if [ -z "${flag_hex}" ]
79 flag_hex="$(hash_string "${myhost}")"
80 echo -e "${myhost}\t${flag_hex}" >> "${cache}"
84 if [ -f "${HOME}/.flag" ]
86 flag_hex="$(cat "${HOME}/.flag")"
89 if [ -z "${flag_hex}" -a -f "/etc/flag" ]
91 flag_hex="$(cat "/etc/flag")"
94 if [ -z "${flag_hex}" ]
99 # Now we definitely have flag_hex
101 eval $(echo -n "${data}" | sed -e 's/^\(.\)\(.*\)$/char="\1" data="\2"/g')
110 echo $(( 0x${1} + 0 ))
116 echo "Usage: flag 'hexcode' [symbol] [shellmode]" >&2
117 echo "Generates an ANSI color flag using the specified hex codes" >&2
118 echo "Uses the optional symbol (or ' ') to draw the flag" >&2
119 echo "shellmode encapsulates all nonprintable codes in \[\] to" >&2
120 echo " give the shell hints on how long the string is, for proper" >&2
121 echo " wrapping of command prompts." >&2
127 [ -n "${shm}" ] && open='\\[' shut='\\]'
128 while [ -n "${data}" ]
131 ord=$(hexdec "${char}")
133 [ "$(( ${ord} & 8 ))" -gt 0 ] && bright="1"
134 fore="3$(( ${ord} & 7 ))"
135 back="4$(( ${ord} & 7 ))"
136 printf "${open}\033[%s;%s;%s;7m${shut}%s" "${bright}" "${fore}" "${back}" "${sym:-${char}}"
138 printf "${open}\033[0m${shut}"
142 echo "Usage: $(basename "${0}") [-s #] [-e]" >&2
143 echo "Produces a neat little ansi colour 'flag' based off" >&2
144 echo " a hash of the machine's hostname (or settable via" >&2
145 echo " ~/.flag or /etc/flag) which can uniquely visually" >&2
146 echo " identify a machine, at a glance. Useful for placing" >&2
147 echo " into /etc/issue or your bash prompt, so that you" >&2
148 echo " don't send stupid commands to the wrong machine." >&2
150 echo " -s # Specify which character should be used to" >&2
151 echo " render the flag. If empty, use the hex code." >&2
152 echo " -e Turn on bash encapsulation mode, to provide" >&2
153 echo " hints to Bash on what is or is not printable" >&2
154 echo " so it can wrap your commands appropriately" >&2
155 echo " when used in your prompt" >&2
157 echo "This machine's flag is as such: ($(flag "${flag_hex}"))" >&2
167 -s) shift; symbol="${1}" ;;
168 -e) shellmode="yes" ;;
173 flag "${flag_hex}" "${symbol}" "${shellmode}"