#!/bin/bash

# array for tde modules to make individual packages
tdeapps=('applications'
	'dependencies'
	'libraries')

# array for tde modules to create single package from module
tdemods=('common'
	'tdeaccessibility'
	'tdeaddons'
	'tdeadmin'
	'tdeartwork'
	'tdebase'
	'tdebindings'
	'tdeedu'
	'tdegames'
	'tdegraphics'
	'tdelibs'
	'tdemultimedia'
	'tdenetwork'
	'tdepim'
	'tdesdk'
	'tdetoys'
	'tdeutils'
	'tdevelop'
	'tdewebdev')

tdepath="/mnt/pv/home/david/tde13/tde"
dtcode="$(date +%Y%m%d-%H%M%S)"                       # date tag to use with script
srulist="/tmp/v3.5.13-sru_module-list-${dtcode}.txt"
:> $srulist

## check tde path or show usage
[[ -r "$tdepath" ]] || usage "ERROR: local copy of tde not found in '$tdepath'" >&2

## save present working dir and change to tdepath/main
mydir="$PWD"
cd "${tdepath}/main" || usage "Error: unable to change to tde directory '${tdepath}/main'" >&2

## loop through GIT modules in tde/main and checkout v3.5.13-sru
for j in ${tdemods[@]}; do

  cd "${tdepath}/main/${j}" || { echo "Warning: unable to change to '${j}' skipping..." >&2; continue; }
  if git checkout v3.5.13-sru; then
    echo "$j" >> $srulist  # temporary log to identify modules with sru branch
    git pull
    git submodule update
  else
    echo "Warning: '$j' has no v3.5.13-sru branch"
  fi

done

## loop through GIT modules in applications, dependencies and libraries
## and switch to v3.5.13-sru if branch is available
for i in ${tdeapps[@]}; do
  for j in $(find ${i} -maxdepth 1 -type d | sort); do
    [[ ${j##*/} == $i ]] ||  [[ ${j##*/} == admin ]] ||  [[ ${j##*/} == cmake ]] &&  continue

      ## change to module directory and checkout v3.5.13-sru
      cd "${tdepath}/main/${j}" || { echo "Warning: unable to change to '${j}' skipping..." >&2; continue; }
      if git checkout v3.5.13-sru; then
	echo "$j" >> $srulist  # temporary log to identify modules with sru branch
	git pull
	git submodule update
      else
	echo "Warning: '$j' has no v3.5.13-sru branch"
      fi

  done
done

## clean up
cd "$mydir"

exit 0
