# 
# make-branch.sh - create a new branch within an archive
################################################################
# Copyright (C) 2001, 2002 Tom Lord
# 
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
# 

set -e 

################################################################
# special options
# 
# Some options are special:
# 
#       --version | -V
#       --help | -h
# 
if test $# -ne 0 ; then

  for opt in "$@" ; do
    case $opt in

      --version|-V) exec larch --version
                    ;;


      --help|-h)
                printf "create a new branch within an archive\\n"
                printf "usage: make-branch [options] branch\\n"
                printf "\\n"
                printf " -V --version                  print version info\\n"
                printf " -h --help                     display help\\n"
                printf "\\n"
                printf " -R --root root                specify the local archive root\\n"
                printf " -A --archive archive          specify the archive name\\n"
                printf "\\n"
                printf " -r --readme file              save FILE as the README\\n"
                printf "                               for this category\\n"
                printf "\\n"
                printf "Create BRANCH as a branch in the indicated archive.\\n"
                printf "\\n"
                printf "The category containing the branch must already exist.\\n"
                printf "That category has the name of the package basename of\\n"
                printf "the branch (\"larch parse-package-name -b \$branch\").\\n"
                printf "See \"larch make-category --help\".\\n"
                printf "\\n"
                printf "For information about how the archive to operate on is selected,\\n"
                printf "try \"larch my-default-archive --help\".\\n"
                printf "\\n"
                exit 0
                ;;

      *)
                ;;
    esac
  done
fi

################################################################
# Ordinary Options
# 
# 

archroot=
archive=
readme=

while test $# -ne 0 ; do

  case "$1" in 

    --silent)           shift   # ignored
                        ;;
    --quiet)            shift   # ignored
                        ;;
    --report)           shift   # ignored
                        ;;
    --verbose)          shift   # ignored
                        ;;


    -R|--root)          shift
                        if test $# -eq 0 ; then
                          printf "make-branch: -R and --root require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        archroot="$1"
                        shift
                        ;;

    -A|--archive)       shift
                        if test $# -eq 0 ; then
                          printf "make-branch: -A and --archive require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        archive="$1"
                        shift
                        ;;


    -r|--readme)        shift
                        if test $# -eq 0 ; then
                          printf "make-branch: -R and --readme require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        readme="$1"
                        shift
                        ;;

    -*)                 printf "make-branch: unrecognized option (%s)\\n" $1 1>&2
                        printf "try --help\\n" 1>&2
                        exit 1
                        ;;

    *)                  break
                        ;;
  esac

done



################################################################
# Ordinary Arguments
# 

if test $# -ne 1 ; then
    printf "usage: make-branch [options] category\\n" 1>&2
    printf "try --help\\n" 1>&2
    exit 1
fi

branch="$1"

################################################################
# Sanity Check 
# 

larch valid-package-name -e make-branch -- "$branch"
idstr="`larch my-id -e make-branch`"

archive="`larch parse-package-name -R \"$archroot\" -A \"$archive\" --arch \"$branch\"`"
cat=`larch parse-package-name -b $branch`
branch="`larch parse-package-name \"$branch\"`"

################################################################
# Get a Temp Dir
# 

tmpdir="`larch make-cache-temp-dir new-branch-$$`"


################################################################
# Create the Category Directory (in the Temp Dir)
# 

tmpbranch="$tmpdir/$branch"
mkdir "$tmpbranch"


################################################################
# Make the =README file
# 

cd "$tmpbranch"
printf "Creator: %s\\nDate: %s\\n\\n" "$idstr" "`date`" > =README


if test "x$readme" != x ; then

  if ! cat "$readme" >> =README ; then
     rm -r "$tmpdir"
  fi

fi


################################################################
# Make the Tip of the Version Sequence
# 
cd "$tmpbranch"
larch make-lock version-lock

################################################################
# Try to install it in the archive
# 

cd "$tmpdir"

status=0

if ! larch with-archive -R "$archroot" -A "$archive" larch putdir -e make-branch -R "$archroot" -A "$archive"  "$branch" "$cat/$branch" ; then
  printf "make-branch: unable to create category\\n" 1>&2
  status=1
fi


if larch my-notifier > /dev/null 2>&1 ; then
  notifier="`larch my-notifier`"
  ( cd "$notifier" ; \
    larch without-archive as-daemon $ARCH_LOG_OPTION --null-output larch notify "$notifier" "$archive/$branch" )
fi

################################################################
# Remove the Cache Temp Directory
# 
cd ..
rm -rf "$tmpdir"

exit "$status"

