#!/bin/sh

# get lxde sources from git and create tarballs
# by Ponce <matteo.bernardini@sns.it>

TMP=/tmp/lxde-git-sources
SEP="--------------------------"

COMPONENTS=" \
  gpicview \
  gtknetcat \
  lxadmin \
  lxappearance \
  lxappearance-obconf \
  lxde-common \
  lxdm \
  lxinput \
  lxlauncher \
  lxmenu-data \
  lxmusic \
  lxpanel \
  lxpanel-xmms2 \
  lxpolkit \
  lxrandr \
  lxsession \
  lxsession-edit \
  lxshortcut \
  lxtask \
  lxterminal \
  menu-cache"

# Allow for specification of individual tarballs to be created:
if [ -z "$1" ]; then
  MODQUEUE=$COMPONENTS
else
  MODQUEUE=$*
fi

set -e

# delete old checkouts
rm -fR $TMP
mkdir -p $TMP/sources
cd $TMP

for module in \
  $MODQUEUE ;
do
  git clone git://lxde.git.sourceforge.net/gitroot/lxde/$module
  cd $module
  COMMIT=$( git log -1 | head -1 | cut -c 8-14 )
  TODAY=$( git log -1 --date=short | head -3 | tail -1 | awk '{print $2}' | sed 's/-//g' )
  mkdir ../${module}-${TODAY}_${COMMIT}
  git archive master > ../tmp.tar
  cd ../${module}-${TODAY}_${COMMIT}
  tar xf ../tmp.tar
  cd ..
  tar Jcf sources/${module}-${TODAY}_${COMMIT}.tar.xz ${module}-${TODAY}_${COMMIT}
  cd sources
  md5sum ${module}-${TODAY}_${COMMIT}.tar.xz >> md5sums.txt
  cd ..
  if [ $? = 0 ]; then
    echo $SEP ; echo "${module} tarball created." ; echo $SEP
  else
    echo $SEP ; echo "${module} FAILED." ; echo $SEP
    exit 1
  fi
done

for module in "libfm" "pcmanfm" ;
do
  git clone git://pcmanfm.git.sourceforge.net/gitroot/pcmanfm/$module
  cd $module
  COMMIT=$( git log -1 | head -1 | cut -c 8-14 )
  TODAY=$( git log -1 --date=short | head -3 | tail -1 | awk '{print $2}' | sed 's/-//g' )
  mkdir ../${module}-${TODAY}_${COMMIT}
  git archive master > ../tmp.tar
  cd ../${module}-${TODAY}_${COMMIT}
  tar xf ../tmp.tar
  cd ..
  tar Jcf sources/${module}-${TODAY}_${COMMIT}.tar.xz ${module}-${TODAY}_${COMMIT}
  cd sources
  md5sum ${module}-${TODAY}_${COMMIT}.tar.xz >> md5sums.txt
  cd ..
  if [ $? = 0 ]; then
    echo $SEP ; echo "${module} tarball created." ; echo $SEP
  else
    echo $SEP ; echo "${module} FAILED." ; echo $SEP
    exit 1
  fi
done

echo "git sources downloaded in ${TMP}/sources."
