#!/bin/sh
#BK nov. 2011

[ ! -d woof-code ] && exit

echo
echo "You don't need to run this script.
I ran it once, which should be enough. It applies workarounds for a VCS to the
'woof-code' directory -- see more explanation in 'merge-arch_code_distro-2-out'
script."
echo -n "Press 'c' key to continue, ENTER key only to quit: "
read goforit

[ "$goforit" != "c" ] && exit

echo
echo "Creating file 'EMPTYDIRMARKER' in empty directories..."
for ONEDIR in `find woof-code -type d`
do
 [ ! -d $ONEDIR ] && continue #catch dirs with spaces.
 if [ "`ls $ONEDIR`" = "" ];then
  echo " $ONEDIR/EMPTYDIRMARKER"
  touch $ONEDIR/EMPTYDIRMARKER
 fi
done

echo
echo "Rename [ and [[ files as LEFTSQBRACKETCHAR and DBLLEFTSQBRACKETCHAR..."
for ONEBAD in `find woof-code -name \\[`
do
 ONEDIR="`dirname "$ONEBAD"`"
 [ ! -e ${ONEDIR}/[ ] && continue #catch dirs with spaces
 echo " renaming ${ONEDIR}/[ to ${ONEDIR}/LEFTSQBRACKETCHAR"
 mv -f ${ONEDIR}/[ ${ONEDIR}/LEFTSQBRACKETCHAR
done
for ONEBAD in `find woof-code -name \\[\\[`
do
 ONEDIR="`dirname "$ONEBAD"`"
 [ ! -e ${ONEDIR}/[[ ] && continue #catch dirs with spaces
 echo " renaming ${ONEDIR}/[[ to ${ONEDIR}/DBLLEFTSQBRACKETCHAR"
 mv -f ${ONEDIR}/[[ ${ONEDIR}/DBLLEFTSQBRACKETCHAR
done

echo
echo "Convert 'dev' directories to DEVDIR.tar.gz tarballs..."
if [ -d woof-code/rootfs-skeleton/dev ];then
 echo " woof-code/rootfs-skeleton/dev"
 cd woof-code/rootfs-skeleton
 tar -c -f DEVDIR.tar dev/
 gzip DEVDIR.tar
 mv -f DEVDIR.tar.gz ../../woof-arch/woof-code_rootfs-skeleton_DEVDIR.tar.gz
 echo -n 'woof-code_rootfs-skeleton_DEVDIR.tar.gz' > DEVDIRMARKER
 rm -rf dev
 cd ../../
fi
if [ -d woof-code/boot/initrd-tree0/dev ];then
 echo " woof-code/boot/initrd-tree0/dev"
 cd woof-code/boot/initrd-tree0
 tar -c -f DEVDIR.tar dev/
 gzip DEVDIR.tar
 mv -f DEVDIR.tar.gz ../../../woof-arch/woof-code_boot_initrd-tree0_DEVDIR.tar.gz
 echo -n 'woof-code_boot_initrd-tree0_DEVDIR.tar.gz' > DEVDIRMARKER
 rm -rf dev
 cd ../../../
fi

#echo "Move all image files from woof-code to woof-arch..."
#rm -rf /tmp/vcs-workarounds 2>/dev/null
#mkdir /tmp/vcs-workarounds
#if [ -f woof-arch/woof-code-binary-images.tar.gz ];then #from previous time ran this script.
# cd woof-arch
# tar -zxf woof-code-binary-images.tar.gz
# cp -a -f woof-code-binary-images/* /tmp/vcs-workarounds/
# rm -rf woof-code-binary-images
# rm -f woof-code-binary-images.tar.gz
# cd ..
#fi
#for ONEFILE in `find woof-code -type f -iname '*.gif' -or -iname '*.png' -or -iname '*.jpg' -or -iname '*.jpeg' | sed -e 's% %SPACECHAR%g' | tr '\n' ' '`
#do
# ONEFILE="`echo -n "$ONEFILE" | sed -e 's%SPACECHAR% %g'`"
# [ ! -e "$ONEFILE" ] && continue
# ONENAME="`basename "$ONEFILE"`"
# ONEPATH="`dirname "$ONEFILE"`"
# echo "$ONEFILE" > ${ONEFILE}MARKER
# mkdir -p "/tmp/vcs-workarounds/${ONEPATH}"
# mv -f "$ONEFILE" "/tmp/vcs-workarounds/${ONEFILE}"
#done
#mv -f /tmp/vcs-workarounds woof-arch/woof-code-binary-images
#cd woof-arch
#dir2tgz woof-code-binary-images
#cd ..
#sync


echo
echo "Record special permissions/ownership in woof-code/VCSMETADATA..."
#catch-22 situation, can only do this on the original 'woof-code', not a 'woof-code'
#that is created from a VCS checkout. Assume such a checkout will also contain file
#'VCSMETADATA'...
if [ ! -f woof-code/VCSMETADATA ];then
 cd woof-code
 touch VCSMETADATA
 for ONESPEC in `find rootfs-skeleton -mindepth 1 | tr '\n' ' '`
 do
  [ ! -e $ONESPEC ] && continue
  FSTATS="`stat --format=%a:%U:%G $ONESPEC`"
  [ "$FSTATS" != "755:root:root" ] && [ "$FSTATS" != "644:root:root" ] && echo "${ONESPEC}:${FSTATS}" >> VCSMETADATA
 done
 for ONESPEC in `find packages-templates -mindepth 1 | tr '\n' ' '`
 do
  [ ! -e $ONESPEC ] && continue
  FSTATS="`stat --format=%a:%U:%G $ONESPEC`"
  [ "$FSTATS" != "755:root:root" ] && [ "$FSTATS" != "644:root:root" ] && echo "${ONESPEC}:${FSTATS}" >> VCSMETADATA
 done
 sort -u --key=1,1 --field-separator=: VCSMETADATA > /tmp/fossil-VCSMETADATA
 mv -f /tmp/fossil-VCSMETADATA VCSMETADATA
 if [ -s VCSMETADATA  ];then
  echo "Written to VCSMATADATA:"
  cat VCSMETADATA
 fi
 cd ..
else
 echo "NOTE: woof-code/VCSMETADATA already exists. This can only be edited manually
      if you want to add or modify any file/dir permissions/ownerhsip."
fi

echo "Finished"
###END###
