#! /bin/ksh
: ${MP:=0}
ROOT=`pwd`
VALID=
while [ $# -gt 0 ]; do
  case $1 in
    osr5) VALID="$VALID osr5" ;;
    osr6 | legend) VALID="$VALID osr6" ;;
    uw7) VALID="$VALID uw7" ;;
    *) echo "ERROR: Unsupported OS $1 specified"; exit 1 ;;
  esac
  shift
done

SKIPGWXOSR5=0
SKIPXORGOSR5=0
SKIPPERLOSR5=0
SKIPAPACHEOSR5=0
SKIPEXTSHOSR5=0
SKIPVIMOSR5=0
SKIPSSHOSR5=0

SKIPGWXUW7=0
SKIPXORGUW7=0
SKIPPERLUW7=0
SKIPAPACHEUW7=0
SKIPEXTSHUW7=0
SKIPVIMUW7=0
SKIPSSHUW7=0

[ -z "$VALID" ] && VALID="osr5 osr6 uw7"

: ${MKCDMT:=metapkg}
: ${HANDOFFS:=$ROOT/data}
export HANDOFFS

[ -d $ROOT/tmp ] && {
  print -n "Cleaning out tmp/ ... "
  rm -fr $ROOT/tmp
  print "done"
}

[ -d $ROOT/final ] && {
  print -n "Cleaning out final/ ... "
  rm -fr $ROOT/final
  print "done"
}

unwind_osr5gwx() {
  cfile=$ROOT/dist/osr5/gwxlibs.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv gwxlibs $2
  else
    SKIPGWXOSR5=1
  fi
}

unwind_osr5xorg() {
  cfile=$ROOT/dist/osr5/xorg.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv xorg $2
    rm -fr $2/var
  else
    SKIPXORGOSR5=1
  fi
}

unwind_osr5perl() {
  cfile=$ROOT/dist/osr5/perl.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv perl $2
  else
    SKIPPERLOSR5=1
  fi
}

unwind_osr5apache() {
  cfile=$ROOT/dist/osr5/apache.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv apache $2
  else
    SKIPAPACHEOSR5=1
  fi
}

unwind_osr5extsh() {
  cfile=$ROOT/dist/osr5/extshells.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv extshells $2
  else
    SKIPEXTSHOSR5=1
  fi
}

unwind_osr5vim() {
  cfile=$ROOT/dist/osr5/vim.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv vim $2
  else
    SKIPVIMOSR5=1
  fi
}

unwind_osr5ssh() {
  cfile=$ROOT/dist/osr5/openssh.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv openssh $2
  else
    SKIPSSHOSR5=1
  fi
}

unwind_uw7gwx() {
  cfile=$ROOT/dist/uw7/gwxlibs.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv gwxlibs $2
  else
    SKIPGWXUW7=1
  fi
}

unwind_uw7xorg() {
  cfile=$ROOT/dist/uw7/xorg.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv xorg $2
    rm -fr $2/var
  else
    SKIPXORGUW7=1
  fi
}

unwind_uw7perl() {
  cfile=$ROOT/dist/uw7/perl.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv perl $2
  else
    SKIPPERLUW7=1
  fi
}

unwind_uw7apache() {
  cfile=$ROOT/dist/uw7/apache.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv apache $2
  else
    SKIPAPACHEUW7=1
  fi
}

unwind_uw7extsh() {
  cfile=$ROOT/dist/uw7/extshells.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv extshells $2
  else
    SKIPEXTSHUW7=1
  fi
}

unwind_uw7vim() {
  cfile=$ROOT/dist/uw7/vim.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv vim $2
  else
    SKIPVIMUW7=1
  fi
}

unwind_uw7ssh() {
  cfile=$ROOT/dist/uw7/openssh.cpio
  if test -f $cfile; then
    cd $1
    cpio -icmud -I $cfile
    mv openssh $2
  else
    SKIPSSHUW7=1
  fi
}

mkcdmt_xorg() {
  cd $1
  XMP=$MP
  [ "$2" = "OSR5" ] && XMP=0
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.xorg -MXORGOS=$2 -MXORGMP=$XMP || {
      echo "FATAL: mkcdmt for XORG failed"
      exit 1
  }
  make || {
    echo "FATAL: XORG package creation failed"
    exit 1
  }
  
  if [ "$2" = "UW7" ]; then
    mv XORG.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_gwx() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.gwxlibs -MGWXOS=$2 -MGWXMP=$MP || {
      echo "FATAL: mkcdmt for GWXLIBS failed"
      exit 1
  }
  make || {
    echo "FATAL: GWXLIBS package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv GWXLIBS.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_perl() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.perl -MPERLOS=$2 -MPERLMP=$MP || {
      echo "FATAL: mkcdmt for PERL failed"
      exit 1
  }
  make || {
    echo "FATAL: PERL package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv Perl.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_apache() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.apache -MAPACHEOS=$2 -MAPACHEMP=$MP || {
      echo "FATAL: mkcdmt for APACHE failed"
      exit 1
  }
  make || {
    echo "FATAL: APACHE package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv APACHE.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_extsh() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.extshells -MEXTSHOS=$2 -MEXTSHMP=$MP || {
      echo "FATAL: mkcdmt for EXTSHELLS failed"
      exit 1
  }
  make || {
    echo "FATAL: EXTSHELLS package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv EXTSHELLS.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_vim() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.vim -MVIMOS=$2 -MVIMMP=$MP || {
      echo "FATAL: mkcdmt for VIM failed"
      exit 1
  }
  make || {
    echo "FATAL: VIM package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv VIM.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkcdmt_ssh() {
  cd $1
  $MKCDMT -d $1 -p $HANDOFFS/metapkg.openssh -MSSHOS=$2 -MSSHMP=$MP || {
      echo "FATAL: mkcdmt for OPENSSH failed"
      exit 1
  }
  make || {
    echo "FATAL: OPENSSH package creation failed"
    exit 1
  }

  if [ "$2" = "UW7" ]; then
    mv OPENSSH.pkg $3
  else
    mv archives/TAPE/VOL* $3
  fi
  make clean
}

mkdir $ROOT/tmp
mkdir $ROOT/final
for bldos in $VALID; do
  mkdir $ROOT/tmp/$bldos
  mkdir $ROOT/final/$bldos
  for d in gwxlibs xorg perl apache extshells vim openssh; do
    mkdir -p $ROOT/tmp/$bldos/$d/dist
    mkdir -p $ROOT/tmp/$bldos/$d/input
    mkdir $ROOT/final/$bldos/$d
  done

  if [ "$bldos" = "osr5" ]; then
    echo "### Doing OSR5"
    
    if test $SKIPGWXOSR5 -eq 0; then
      print -n "Unwinding OSR5 GWXLIBS ... "
      rmdir $ROOT/tmp/$bldos/gwxlibs/dist
      unwind_osr5gwx $ROOT/tmp/$bldos/gwxlibs dist
    fi

    if test $SKIPXORGOSR5 -eq 0; then
      print -n "Unwinding OSR5 XORG ... "
      rmdir $ROOT/tmp/$bldos/xorg/dist
      unwind_osr5xorg $ROOT/tmp/$bldos/xorg dist
    fi

    if test $SKIPPERLOSR5 -eq 0; then
      print -n "Unwinding OSR5 PERL ... "
      rmdir $ROOT/tmp/$bldos/perl/dist
      unwind_osr5perl $ROOT/tmp/$bldos/perl dist
    fi

    if test $SKIPAPACHEOSR5 -eq 0; then
      print -n "Unwinding OSR5 APACHE ... "
      rmdir $ROOT/tmp/$bldos/apache/dist
      unwind_osr5apache $ROOT/tmp/$bldos/apache dist
    fi

    if test $SKIPEXTSHOSR5 -eq 0; then
      print -n "Unwinding OSR5 EXTSHELLS ... "
      rmdir $ROOT/tmp/$bldos/extshells/dist
      unwind_osr5extsh $ROOT/tmp/$bldos/extshells dist
    fi

    if test $SKIPVIMOSR5 -eq 0; then
      print -n "Unwinding OSR5 VIM ... "
      rmdir $ROOT/tmp/$bldos/vim/dist
      unwind_osr5vim $ROOT/tmp/$bldos/vim dist
    fi

    if test $SKIPSSHOSR5 -eq 0; then
      print -n "Unwinding OSR5 OPENSSH ... "
      rmdir $ROOT/tmp/$bldos/openssh/dist
      unwind_osr5ssh $ROOT/tmp/$bldos/openssh dist
    fi

    if test $SKIPXORGOSR5 -eq 0; then
      mkcdmt_xorg $ROOT/tmp/$bldos/xorg OSR5 $ROOT/final/$bldos/xorg
      cd $ROOT/tmp/$bldos/xorg
      mv dist/XORGRT $ROOT/tmp/$bldos/gwxlibs/dist
      mv dist/XORGFonts $ROOT/tmp/$bldos/gwxlibs/dist
    fi

    if test $SKIPGWXOSR5 -eq 0; then
      mkcdmt_gwx $ROOT/tmp/$bldos/gwxlibs OSR5 $ROOT/final/$bldos/gwxlibs
    fi

    if test $SKIPPERLOSR5 -eq 0; then
      mkcdmt_perl $ROOT/tmp/$bldos/perl OSR5 $ROOT/final/$bldos/perl
    fi

    if test $SKIPAPACHEOSR5 -eq 0; then
      mkcdmt_apache $ROOT/tmp/$bldos/apache OSR5 $ROOT/final/$bldos/apache
    fi

    if test $SKIPEXTSHOSR5 -eq 0; then
      mkcdmt_extsh $ROOT/tmp/$bldos/extshells OSR5 $ROOT/final/$bldos/extshells
    fi

    if test $SKIPVIMOSR5 -eq 0; then
      mkcdmt_vim $ROOT/tmp/$bldos/vim OSR5 $ROOT/final/$bldos/vim
    fi

    if test $SKIPSSHOSR5 -eq 0; then
      mkcdmt_ssh $ROOT/tmp/$bldos/openssh OSR5 $ROOT/final/$bldos/openssh
    fi

  elif [ "$bldos" = "osr6" ]; then
    echo "### Doing OSR6"

    if test $SKIPGWXOSR5 -eq 0 -a $SKIPGWXUW7 -eq 0; then
      print -n "Unwinding OSR5 GWXLIBS ... "
      unwind_osr5gwx $ROOT/tmp/$bldos/gwxlibs/dist osr5
      print -n "Unwinding UW7 GWXLIBS ... "
      unwind_uw7gwx $ROOT/tmp/$bldos/gwxlibs/dist uw7
    fi

    if test $SKIPXORGOSR5 -eq 0 -a $SKIPXORGUW7 -eq 0; then
      print -n "Unwinding OSR5 XORG ... "
      unwind_osr5xorg $ROOT/tmp/$bldos/xorg/dist osr5
      print -n "Unwinding UW7 XORG ... "
      unwind_uw7xorg $ROOT/tmp/$bldos/xorg/dist uw7
    fi

    if test $SKIPPERLUW7 -eq 0; then
      print -n "Unwinding UW7 PERL ... "
      rmdir $ROOT/tmp/$bldos/perl/dist
      unwind_uw7perl $ROOT/tmp/$bldos/perl dist
    fi

    if test $SKIPAPACHEUW7 -eq 0; then
      print -n "Unwinding UW7 APACHE ... "
      rmdir $ROOT/tmp/$bldos/apache/dist
      unwind_uw7apache $ROOT/tmp/$bldos/apache dist
    fi

    if test $SKIPEXTSHUW7 -eq 0; then
      print -n "Unwinding UW7 EXTSHELLS ... "
      rmdir $ROOT/tmp/$bldos/extshells/dist
      unwind_uw7extsh $ROOT/tmp/$bldos/extshells dist
    fi

    if test $SKIPVIMUW7 -eq 0; then
      print -n "Unwinding UW7 VIM ... "
      rmdir $ROOT/tmp/$bldos/vim/dist
      unwind_uw7vim $ROOT/tmp/$bldos/vim dist
    fi

    if test $SKIPSSHUW7 -eq 0; then
      print -n "Unwinding UW7 OPENSSH ... "
      rmdir $ROOT/tmp/$bldos/openssh/dist
      unwind_uw7ssh $ROOT/tmp/$bldos/openssh dist
    fi

    if test $SKIPXORGOSR5 -eq 0 -a $SKIPXORGUW7 -eq 0; then
      mkcdmt_xorg $ROOT/tmp/$bldos/xorg OSR6 $ROOT/final/$bldos/xorg
      cd $ROOT/tmp/$bldos/xorg
      mv dist/XORGRT $ROOT/tmp/$bldos/gwxlibs/dist
      mv dist/XORGFonts $ROOT/tmp/$bldos/gwxlibs/dist
    fi

    if test $SKIPGWXOSR5 -eq 0 -a $SKIPGWXUW7 -eq 0; then
      mkcdmt_gwx $ROOT/tmp/$bldos/gwxlibs OSR6 $ROOT/final/$bldos/gwxlibs
    fi

    if test $SKIPPERLUW7 -eq 0; then
      mkcdmt_perl $ROOT/tmp/$bldos/perl OSR6 $ROOT/final/$bldos/perl
    fi

    if test $SKIPAPACHEUW7 -eq 0; then
      mkcdmt_apache $ROOT/tmp/$bldos/apache OSR6 $ROOT/final/$bldos/apache
    fi

    if test $SKIPEXTSHUW7 -eq 0; then
      mkcdmt_extsh $ROOT/tmp/$bldos/extshells OSR6 $ROOT/final/$bldos/extshells
    fi

    if test $SKIPVIMUW7 -eq 0; then
      mkcdmt_vim $ROOT/tmp/$bldos/vim OSR6 $ROOT/final/$bldos/vim
    fi

    if test $SKIPSSHUW7 -eq 0; then
      mkcdmt_ssh $ROOT/tmp/$bldos/openssh OSR6 $ROOT/final/$bldos/openssh
    fi

  else
    echo "### Doing UW7"

    if test $SKIPGWXUW7 -eq 0; then
      print -n "Unwinding UW7 GWXLIBS ... "
      rmdir $ROOT/tmp/$bldos/gwxlibs/dist
      unwind_uw7gwx $ROOT/tmp/$bldos/gwxlibs dist
    fi

    if test $SKIPXORGUW7 -eq 0; then
      print -n "Unwinding UW7 XORG ... "
      rmdir $ROOT/tmp/$bldos/xorg/dist
      unwind_uw7xorg $ROOT/tmp/$bldos/xorg dist
    fi

    if test $SKIPPERLUW7 -eq 0; then
      print -n "Unwinding UW7 PERL ... "
      rmdir $ROOT/tmp/$bldos/perl/dist
      unwind_uw7perl $ROOT/tmp/$bldos/perl dist
    fi

    if test $SKIPAPACHEUW7 -eq 0; then
      print -n "Unwinding UW7 APACHE ... "
      rmdir $ROOT/tmp/$bldos/apache/dist
      unwind_uw7apache $ROOT/tmp/$bldos/apache dist
    fi

    if test $SKIPEXTSHUW7 -eq 0; then
      print -n "Unwinding UW7 EXTSHELLS ... "
      rmdir $ROOT/tmp/$bldos/extshells/dist
      unwind_uw7extsh $ROOT/tmp/$bldos/extshells dist
    fi

    if test $SKIPVIMUW7 -eq 0; then
      print -n "Unwinding UW7 VIM ... "
      rmdir $ROOT/tmp/$bldos/vim/dist
      unwind_uw7vim $ROOT/tmp/$bldos/vim dist
    fi

    if test $SKIPXORGUW7 -eq 0; then
      mkcdmt_xorg $ROOT/tmp/$bldos/xorg UW7 $ROOT/final/$bldos/xorg
      cd $ROOT/tmp/$bldos/xorg
      mv dist/XORGRT $ROOT/tmp/$bldos/gwxlibs/dist
      mv dist/XORGFonts $ROOT/tmp/$bldos/gwxlibs/dist
    fi

    if test $SKIPGWXUW7 -eq 0; then
      mkcdmt_gwx $ROOT/tmp/$bldos/gwxlibs UW7 $ROOT/final/$bldos/gwxlibs
    fi

    if test $SKIPPERLUW7 -eq 0; then
      mkcdmt_perl $ROOT/tmp/$bldos/perl UW7 $ROOT/final/$bldos/perl
    fi

    if test $SKIPAPACHEUW7 -eq 0; then
      mkcdmt_apache $ROOT/tmp/$bldos/apache UW7 $ROOT/final/$bldos/apache
    fi

    if test $SKIPEXTSHUW7 -eq 0; then
      mkcdmt_extsh $ROOT/tmp/$bldos/extshells UW7 $ROOT/final/$bldos/extshells
    fi

    if test $SKIPVIMUW7 -eq 0; then
      mkcdmt_vim $ROOT/tmp/$bldos/vim UW7 $ROOT/final/$bldos/vim
    fi

  fi
done

exit 0
