#!/bin/sh
#
# $DevNullCZ: ukol-09.reseni.txt,v 1.1 2005/04/26 21:00:03 jp Exp $
#

[ $# -ne 1 ] && echo "bad input" && exit
echo $1 | grep '/' >/dev/null || { echo "no mask" && exit; }

ip=`echo $1 | cut -f1 -d/`
mask=`echo $1 | cut -f2 -d/`
dots="........"

bc_out=`echo "obase=2; $ip" | sed -e 's/\./;/g' | bc | tr '\n' ' '`
printf_out=`printf "%08d %08d %08d %08d" $bc_out | sed -e 's/ //g'`
zeroes=`printf %0\`expr 33 - $mask\`d 0 | sed -e s/.$//`

binary=`echo "\`echo $printf_out | cut -c1-$mask\`$zeroes"`
new=`echo $binary | \
  sed -e "s/^\($dots\)\($dots\)\($dots\)\(.*\)$/ibase=2; \1; \2; \3; \4/g" | \
  bc | tr '\n' '.' | sed -e 's/.$//g'`

echo "$new/$mask"