#/bin/bash
#
# This script converts and existing ocssw tree into a git working copy of
# the remote ocssw repository:  http://oceandata.sci.gsfc.nasa.gov/ocssw
#

if [ ${OCSSWROOT:=crap} == "crap" ]; then
    echo " OCSSWROOT environment variable needs to be set"
    exit
fi

GITUSER=`git config --get user.name`
GITEMAIL=`git config --get user.email`

cd $OCSSWROOT

for dir in build run/bin/linux run/bin/linux_64 run/bin/macosx_intel run/bin3/linux run/bin3/linux_64 run/bin3/macosx_intel run/scripts run/data/common run/data/czcs run/data/hmodisa run/data/hmodist run/data/meris run/data/modis run/data/modisa run/data/modist run/data/ocm1 run/data/ocm2 run/data/ocrvc run/data/octs run/data/osmi run/data/seawifs run/data/viirsn
do
  if [ -d $dir ]; then
    if [ -d $dir/.git ]; then
        continue
    fi
    if [[ $dir != "run/data/ocrvc" && $dir != "run/data/common" ]];then 
        if [ -f "$dir/msl12_defaults.par" ]; then
            if [ ! -d "$dir/aerosol" ]; then
                continue
            fi
        fi
    fi 
    
    repo=`echo $dir|cut -d  \/ -f 2`
    
    if [ $repo == "data" ]; then
        repo=`echo $dir|cut -d  / -f 3`
    elif [[ $repo == "bin" || $repo == "bin3" ]]; then
        repo="$repo-`echo $dir|cut -d  \/ -f 3`"
    fi
    echo $dir $repo
    cd "$OCSSWROOT/$dir"
    if [ $repo == "scripts" ]; then
        chmod +x *.py
    fi
    git init

    if [ -z "$GITUSER" ] && [ "${GITUSER+xxx}" = "xxx" ]; then
        git config  user.name "Default User"
    fi
    if [ -z "$GITEMAIL" ] && [ "${GITEMAIL+xxx}" = "xxx" ]; then
        git config  user.email "seadas-user@localhost"
    fi
    git remote add origin http://oceandata.sci.gsfc.nasa.gov/ocssw/$repo.git
    git config branch.master.remote origin                                      
    git config branch.master.merge refs/heads/master
    git fetch
    #git add .
    #git merge -s recursive -X theirs origin/master -m "merging with repo"
    cd $OCSSWROOT
  fi
done
