#!/bin/sh
#t2 has already created binary pkgs. this script splits them

. /etc/rc.d/PUPSTATE
CURRDIR="`pwd`"

#these variables must be edited manually...
RAWPKGSPATH="/mnt/hda6/t2-15oct07/t2-trunk/build/piglet001-8.0-trunk-piglet-x86-i486/TOOLCHAIN/pkgs"
PROFILEPATH="/mnt/hda6/t2-15oct07/t2-trunk/target/piglet"
TARGETPATH="/mnt/hda3/puppy-development"
SPLITPKGSPATH="${TARGETPATH}/t2packages-split"

if [ ! -d $RAWPKGSPATH ];then
 echo "The path of the raw T2 binary pkgs has to be set in script. Exiting."
 exit
fi

if [ ! -d $SPLITPKGSPATH ];then
 echo "The path in which to create the split pkgs has to be set in script. Exiting."
 exit
fi

NLS_TARGETDIR="${SPLITPKGSPATH}/packages-nls"
DOC_TARGETDIR="${SPLITPKGSPATH}/packages-doc"
DEV_TARGETDIR="${SPLITPKGSPATH}/packages-dev"
EXE_TARGETDIR="${SPLITPKGSPATH}/packages-exe"

#PUP_TARGETDIR="${TARGETPATH}/puppy-unleashed/packages"
#PUP_DEVXDIR="${TARGETPATH}/puppy-devx"
#PUP_UNLEASHED="${TARGETPATH}/puppy-unleashed"

rm -rf ${SPLITPKGSPATH}/packages-nls/*
rm -rf ${SPLITPKGSPATH}/packages-doc/*
rm -rf ${SPLITPKGSPATH}/packages-dev/*
rm -rf ${SPLITPKGSPATH}/packages-exe/*

#list of packages that will go into the devx module only... (not used here)
DEVXONLY=' autoconf automake bin86 binutils bison cmake compositeproto cvs damageproto dietlibc evieext fixesproto fontcacheproto fontsproto flex gcc gccmakedep gdb gettext groff imake inputproto intltool kbproto libcap linux-header m4 make man man-pages mkinitrd nasm panoramixproto patch patchutils perl perl-compress-zlib perl-digest-sha1 perl-extutils-depends perl-extutils-pkgconfig perl-html-parser perl-uri perl-xml-parser perl-xml-simple pkgconfig python randrproto recordproto renderproto resourceproto rman rsync scrnsaverproto strace subversion texinfo trapproto videoproto xcmiscproto xextproto xf86bigfontproto xf86dgaproto xf86miscproto xf86vidmodeproto xineramaproto xtrans xf86driproto glproto '
# intltool ?
# gcc, perl* -- not quite all.

#list of all xorg packages... (not used here)
XORGALL=' appres bdftopcf beforelight bigreqsproto bitmap cf compositeproto damageproto dmxproto driconf editres encodings evieext fdclock fixesproto font-adobe font-adobe-utopia font-adobe-utopia-type1 font-alias font-arabic-misc font-bh font-bh-lucidatypewriter font-bh-ttf font-bh-type1 font-bitstream font-bitstream-type1 fontcacheproto fontconfig font-cronyx-cyrillic font-cursor-misc font-daewoo-misc font-dec-misc font-ibm-type1 font-isas-misc font-jis-misc font-micro-misc font-misc-cyrillic font-misc-misc font-mutt-misc font-schumacher-misc font-screen-cyrillic font-sony-misc fontsproto font-sun-misc fonttosfnt font-util font-winitzki-cyrillic font-xfree86-type1 fslsfonts fstobdf gccmakedep glproto iceauth ico imake inputproto kbproto lbxproxy libdmx libdrm libfontenc libfs libice liblbxutil liboldx libpciaccess libpthread-stubs libsm libx11 libxau libxaw libxcb libxcomposite libxcursor libxdamage libxdmcp libxevie libxext libxfixes libxfont libxfontcache libxft libxi libxinerama libxkbfile libxkbui libxmu libxp libxpm libxprintapputil libxprintutil libxrandr libxrender libxres libxscrnsaver libxt libxtrap libxtst libxv libxvmc libxxf86dga libxxf86misc libxxf86vm listres luit makedepend mkcomposecache mkfontdir mkfontscale oclock package-modular-x-deps.sh package-modular-x.sh panoramixproto pclcomp printproto proxymngr randrproto recordproto rendercheck renderproto resourceproto rgb rstart scripts scrnsaverproto sessreg setxkbmap showfont smproxy trapproto twm videoproto viewres x11perf xauth xbacklight xbiff xbitmaps xcalc xcb-proto xclipboard xclock xcmiscproto xcmsdb xconsole xcursorgen xcursor-themes xdbedizzy xditview xdm xdpyinfo xdriinfo xedit xev xextproto xeyes xf86bigfontproto xf86dga xf86dgaproto xf86driproto xf86-input-acecad xf86-input-aiptek xf86-input-calcomp xf86-input-citron xf86-input-digitaledge xf86-input-dmc xf86-input-dynapro xf86-input-elo2300 xf86-input-elographics xf86-input-evdev xf86-input-evtouch xf86-input-fpit xf86-input-fujitouch xf86-input-hyperpen xf86-input-jamstudio xf86-input-joystick xf86-input-keyboard xf86-input-magellan xf86-input-magictouch xf86-input-microtouch xf86-input-mouse xf86-input-mutouch xf86-input-palmax xf86-input-penmount xf86-input-sample xf86-input-spaceorb xf86-input-summa xf86-input-tek4957 xf86-input-ur98 xf86-input-vmmouse xf86-input-void xf86miscproto xf86rushproto xf86-video-amd xf86-video-apm xf86-video-ark xf86-video-ati xf86-video-avivo xf86-video-chips xf86-video-cirrus xf86-video-cyrix xf86-video-dummy xf86-video-fbdev xf86-video-glide xf86-video-glint xf86-video-i128 xf86-video-i740 xf86-video-i810 xf86-video-impact xf86-video-imstt xf86-video-mga xf86-video-neomagic xf86-video-newport xf86-video-nouveau xf86-video-nsc xf86-video-nv xf86-video-rendition xf86-video-s3 xf86-video-s3virge xf86-video-savage xf86-video-siliconmotion xf86-video-sis xf86-video-sisusb xf86-video-sunbw2 xf86-video-suncg14 xf86-video-suncg3 xf86-video-suncg6 xf86-video-sunffb xf86-video-sunleo xf86-video-suntcx xf86-video-tdfx xf86-video-tga xf86-video-trident xf86-video-tseng xf86-video-vermilion xf86-video-vesa xf86-video-vga xf86-video-via xf86-video-vmware xf86-video-voodoo xf86-video-xgi xf86vidmodeproto xfd xfontsel xfs xfsinfo xfwp xgamma xgc xhost xineramaproto xinit xkbcomp xkbdata xkbevd xkbprint xkbutils xkill xload xlogo xlsatoms xlsclients xlsfonts xmag xman xmessage xmh xmodmap xmore xorg-server xorg-util-macros xphelloworld xplsprinters xpr xprehashprinterlist xprop xproto xproxymanagementprotocol xrandr xrdb xrefresh xset xsetmode xsetpointer xsetroot xsm xstdcmap xtrans xtrap xvidtune xvinfo xwd xwininfo xwud '

#pkg names without version number...
PKGNAMESONLY="`grep '^X ' ${PROFILEPATH}/pkgsel | tr -s ' ' | cut -f 2 -d ' ' | tr '\n' ' '`"
ALLPKGS="`ls -1 $RAWPKGSPATH | sed -e 's%\.tar\.bz2$%%'`"

echo -n "" > /tmp/t2failed2findpkg
echo -n "" > /tmp/t2multiplematch

#now do the splitting...
for PKGNAMEONLY in $PKGNAMESONLY
do
 [ "$PKGNAMEONLY" = "" ] && continue
 echo "Processing $PKGNAMEONLY"
 
 #find name with version number...
 APATTERN="^${PKGNAMEONLY}\\-"
 NEWPKGDIR="`echo "$ALLPKGS" | grep "$APATTERN"`"
 if [ "$NEWPKGDIR" = "" ];then
  echo "PKGNAMEONLY=$PKGNAMEONLY" >> /tmp/t2failed2findpkg
  continue
 fi
 if [ `echo "$NEWPKGDIR" | wc -l` -ne 1 ];then
  #maybe can narrow down selection...
  BPATTERN="^${PKGNAMEONLY}\\-[0-9]"
  NEWPKGDIR="`echo "$ALLPKGS" | grep "$BPATTERN"`"
  if [ `echo "$NEWPKGDIR" | wc -l` -ne 1 ];then
   echo  "PKGNAMEONLY=$PKGNAMEONLY" >> /tmp/t2multiplematch
   continue
  fi
 fi
 
 #expand pkg in sandbox...
 rm -rf $SPLITPKGSPATH/sandbox 2>/dev/null
 mkdir $SPLITPKGSPATH/sandbox
 cp -a ${RAWPKGSPATH}/${NEWPKGDIR}.tar.bz2 $SPLITPKGSPATH/sandbox/
 sync
 cd $SPLITPKGSPATH/sandbox
 tar --absolute-names -jxf ./${NEWPKGDIR}.tar.bz2
 sync
 rm -f ./${NEWPKGDIR}.tar.bz2
  
  #now to split pkg into exe,dev,doc,nls components...
  #this will find all files and symlinks to files...
  find -L ./ -noleaf -type f | sed -e 's/^\.\///' |
  while read ONEFILE
  do
   
   #problem, coming here with paths that have symlinks...
   UPONE="`dirname "$ONEFILE"`"
   while [ "$UPONE" != "." -a "$UPONE" != "/" ];do
    [ -h "$UPONE" ] && continue 2
    UPONE="`dirname "$UPONE"`"
   done
  
   ONEBASE="`basename "$ONEFILE"`"
   ONEPATH="`dirname "$ONEFILE"`"
   [ "$ONEPATH" = "." ] && ONEPATH=""
   #echo "Processing ${ONEFILE}"
   #strip the file...
   if [ ! -h "$ONEFILE" ];then #make sure it isn't a symlink
    [ ! "`file "$ONEFILE" | grep 'ELF' | grep 'shared object'`" = "" ] && strip --strip-debug "$ONEFILE"
    [ ! "`file "$ONEFILE" | grep 'ELF' | grep 'executable'`" = "" ] && strip --strip-unneeded "$ONEFILE"
   fi
   sync
   #find out if this is an international language file...
   if [ ! "`echo -n "$ONEFILE" | grep --extended-regexp '/locale/|/nls/|/i18n/'`" = "" ];then
    mkdir -p "${NLS_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
    cp -af "$ONEFILE" "${NLS_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
    continue
   fi
   #find out if this is a documentation file...
   if [ ! "`echo -n "$ONEFILE" | grep --extended-regexp '/man/|/doc/|/docs/|/info/|/gtk-doc/|/faq/|/manual/|/examples/|/help/|/htdocs/'`" = "" ];then
    mkdir -p "${DOC_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
    cp -af "$ONEFILE" "${DOC_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
    continue
   fi
   #find out if this is development file...
   if [ ! "`echo -n "$ONEFILE" | grep --extended-regexp '/include/|/pkgconfig/|/aclocal|/cvs/|/svn/'`" = "" ];then
    mkdir -p "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
    cp -af "$ONEFILE" "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
    continue
   fi
   #find *.so symlink files...
   if [ -h "$ONEFILE" ];then #-h tests for symlink
    if [ ! "`echo -n "$ONEFILE" | grep '\.so$'`" = "" ];then
     mkdir -p "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
     cp -af "$ONEFILE" "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
     continue
    fi
   fi
   #find various config files...
   if [ ! "`echo -n "$ONEBASE" | grep --extended-regexp '\-config$|config.sh$|Conf.sh$'`" = "" ];then
    mkdir -p "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
    cp -af "$ONEFILE" "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
    continue
   fi
   #all .a and .la files... and any stray .m4 files...
   if [ ! "`echo -n "$ONEBASE" | grep --extended-regexp '\.a$|\.la$|\.m4$'`" = "" ];then
    mkdir -p "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
    cp -af "$ONEFILE" "${DEV_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
    continue
   fi  
   #anything left over goes into the main 'executable' package...
   mkdir -p "${EXE_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}"
   cp -af "$ONEFILE" "${EXE_TARGETDIR}/${NEWPKGDIR}/${ONEPATH}/" 2>/dev/null
  done
  #fix for empty directories and links to dirs...
  find -L ./ -noleaf -type d | sed -e 's/^\.\///' |
  while read ANEWDIR
  do
   UPONE="`dirname "$ANEWDIR"`"
   [ "$UPONE" = "." ] && UPONE=""
   if [ -h "$ANEWDIR" ];then #link
    mkdir -p "${EXE_TARGETDIR}/${NEWPKGDIR}/${UPONE}"
    cp -af "$ANEWDIR" "${EXE_TARGETDIR}/${NEWPKGDIR}/${UPONE}/"
    continue
   fi
   #copy a empty dir...
   [ "`ls -1 $ANEWDIR`" = "" ] && mkdir -p ${EXE_TARGETDIR}/${NEWPKGDIR}/${ANEWDIR}
  done
  cd $CURRDIR
  #end of splitting.

done

sync
###END###
