merge lines into single line based on symbol \t

The symbols are \t and \t\t (note: not tab)
If the line starts with \t merge them into a single line upto symbol \t\t
\t\t to end and start new line

I able to join in a single line but not ending at \t\t and I completely confused
help would be appreciated:b::smiley:
Input
\ta tab XXXXXXXXXX
\te tab YYYYYYYYYYY
\t\t
\tc tab DDDDXXXXXX
\tz tab FFFFFFYYYYYYY

output
a tab XXXXXXXXXX tab e tab YYYYYYYYYYY
c tab DDDDXXXXXX tab z tab FFFFFFYYYYYYY

#!/bin/ksh
savestr=""
# remove first \t, we don't need it
sed "s/^\\\t//" filename | while read line
do
       case "$line" in
               \t*) # \t\t line, so print buffer and empty buffer
                       echo "$savestr"
                       savestr=""
                       ;;
               *)     # not \t\t line, so add current line to the buffer
                       savestr="$savestr$line "
                   ;;
       esac
done
# last line
# if file not end \t\t line, then we have some data in buffer, print it
[ "$savestr" != "" ] && echo "$savestr"

Thanx alot
Working fine

---------- Post updated at 12:28 AM ---------- Previous update was at 12:20 AM ----------

Could you plz explain the code if posiible
Thanx

Another way:

awk '/\\t\\t/{printf("\n");next}{gsub(/\\t/,"");print}END{printf("\n")}' ORS=" " filename

And a pure sed version:

#  sed -e :a -e '$!N;s/\\\t\\\t//;s/\n\\\t/ /;s/\\\t/ /;ta' -e 'P;D' infile
 a tab XXXXXXXXXX e tab YYYYYYYYYYY
 c tab DDDDXXXXXX z tab FFFFFFYYYYYYY

Another pure sed, a bit shorter

sed -n 'H; $ {g; s/\n//g; s/\\t\\t/\n/g; s/\\t//g; p; }' input_file