Note: A more complete implementation is over here: http://git.cyberleo.net/flag.git


v1

#!/bin/sh

firstchar() {
  eval $(echo -en "${data}" | sed -e 's/^\(.\)\(.*\)$/char="\1" data="\2"/g')
}

hexdec() {
  if [ -z "${1}" ]
  then
    echo "0"
    return
  fi
  echo $(( 0x${1} + 0 ))
}

flag(){
  if [ -z "${1}" ]
  then
    echo "Usage: flag 'hexcode' [symbol]"
    echo "Generates an ANSI color flag using the specified hex codes"
    echo "Uses the optional symbol (or .) to draw the flag"
    return
  fi
  data="${1}"
  sym="${2}"
  [ -z "${sym}" ] && sym=" "
  while [ -n "${data}" ]
  do
    firstchar
    ord=$(hexdec "${char}")
    bright="0"
    [ "$(( ${ord} & 8 ))" -gt 0 ] && bright="1"
    fore="3$(( ${ord} & 7 ))"
    back="4$(( ${ord} & 7 ))"
    printf "\[\033[%s;%s;%s;7m\]%s" "${bright}" "${fore}" "${back}" "${sym}"
  done
  printf "\[\033[0m\]"
}

[ -f "/etc/flag" ] && flag "$(cat /etc/flag)"

v2.incomplete

#!/bin/sh

# Places for flags:
# $HOME/.flag-cache <- Autogenerated flag (refreshed when hostname changes)
# /etc/flag <- Site admin's flag
# $HOME/.flag <- Local user's flag
# Check them in that order.

hash_string(){
  # Hash the passed string into 8 hex characters
}

flagcache(){
  eval $(cat "${HOME}/.flag-cache")
  # Now have flag_hex and flag_host
  myhost="$(/bin/hostname)"
  if [ "${flag_host}" != "${myhost}" ]
  then
    flag_hex="$(hash_string "${myhost}")"
    echo "flag_host='${myhost}'\nflag_hex='${flag_hex}'" > "${HOME}/.flag-cache"
  fi
}

if [ -f "${HOME}/.flag" ]
then
  flag_hex="$(cat "${HOME}/.flag")"
fi

if [ -z "${flag_hex}" && -f "/etc/flag" ]
then
  flag_hex="$(cat "/etc/flag")"
fi

if [ -z "${flag_hex}" ]
then
  flagcache
fi

# Now we definitely have flag_hex

PHP Warning

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead:"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead"

Warning: "preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead:"