#!/bin/bash

CWD=`dirname $0`
cd $CWD
CWD=`pwd`
SCRIPT=`basename $0 .sh`
#SUFF="current"
SUFF=${SCRIPT:8}

#DATE=`echo current*-fix.diff | sed 's/current_//' | sed 's/-.*//'`
#BDIFF="current_${DATE}-"
#NDIFF="current_${DATE}"

if [[ "$SUFF" = "current" ]]
then
  NDIFF="current"
  PACK="packages_$SUFF"
  EXT=""
else
  NDIFF="slackware64-$SUFF"
  PACK="$NDIFF.pack"
  EXT=".$SUFF"
fi
BDIFF="${NDIFF}_"
BASE=slackware64-$SUFF
if [[ "$1" = "" ]]
then
  PUB=$BASE.publish
  domd5=1
else
  PUB=$BASE.$1
  domd5=0
fi

# Cloning
if [[ -d $PUB ]]
then
echo "Moving $PUB to backup..."
rm -rf $PUB.backup3
mv $PUB.backup2 $PUB.backup3
mv $PUB.backup $PUB.backup2
mv $PUB $PUB.backup
fi

echo "Cloning $BASE..."
time cp -al $BASE $PUB

cd $PUB

# Remove binary files:
if [[ $domd5 -eq 1 ]]
then
  echo "Removing binaries..."
  rm -rf slackware64 */packages EFI isolinux usb-and-pxe-installers kernels *.TXT CHECKSUM* GPG* R* ANN* Slackware*
fi
rm -f CHECKSUM*

for i in `\ls ../$PACK/rootfiles/`
do
  rm -f $i
done
\cp -p ../$PACK/rootfiles/* .

if [[ $domd5 -eq 1 ]]
then
  cd extra
  rm -rf `\ls | grep -v source`
  #CHECKSUMS* FILE_LIST MANIFEST* *.TXT aspell-word-lists bash-completion bittornado brltty fltk getty-ps google-chrome java php8 sendmail tigervnc xf86-video-fbdev xf86-video-nouveau-blacklist xfractint xv
  cd -
fi

rm -f pasture/*
rm -f patches/*
rm -f testing/*
rm -f source/CHECK*
rm -f source/MANIFEST*

# Applying patches:
echo "Applying patches..."
echo "FIX..."
patch -p1 < $CWD/${BDIFF}fix.diff
sleep 5
echo "ARCH..."
patch -p1 < $CWD/${BDIFF}arm-*.diff
sleep 5
for i in extra pasture testing source 
do 
  $CWD/fix_flags_libdir.sh$EXT $i "BACKUPDIR:../$PUB.backup"
done
echo "BOOTSTRAP..."
patch -p1 < $CWD/${BDIFF}bootstrap.diff
sleep 5
if [[ -f  "$CWD/${BDIFF}cross32.diff" ]]
then
  patch -p1 < $CWD/${BDIFF}cross32.diff
fi
if [[ $domd5 -eq 0 && -f $CWD/${BDIFF}incremental.diff ]]
then
  echo "INCREMENTAL..."
  patch -p1 < $CWD/${BDIFF}incremental.diff
  sleep 5
fi

if [ -d "patches/source" ]
then
  cd patches/source
  $CWD/patch_patches.sh
  cd -
fi

# Copying new files
echo "Copying new files..."
cp -a --reflink $CWD/newfiles-$SUFF/* .
if [[ "$domd5" = "1" ]]
then
  (cd $CWD/newfiles-$SUFF/ ; find . -type f | xargs ls -l > $CWD/$PUB/newfiles-$SUFF.txt )
fi

# Linking sources
echo "Linking sources..."
cd source
for i in extra pasture testing patches extra-bon
do
  if [[ -d ../$i/source ]]
  then
    ln -s ../$i/source $i
    ref=`\ls -rtd $i/*/*|tail -1`
    if [[ "$ref" != "" ]]
    then
      touch -r "$ref" ../$i
    fi
  fi
done
cd -

if [[ "$domd5" = "1" ]]
then
  # Copying scripts
  echo "Copying scripts..."
  mkdir scripts
  cp -p $CWD/scripts-$SUFF/* scripts/

  # Changin group owner:
  echo "Changing group owner..."
  chgrp -R apache $CWD/$PUB/

  if [[ -d ../$PUB.backup ]]
  then
    # Updating from backup:
    echo "Removing .orig files..."
    find . -type f -mmin -30 | xargs $CWD/remove_orig.pl
    echo "Updating timestamps from backup..."
    find . -type f -mmin -30 | xargs $CWD/samebackup.pl ../$PUB.backup
    \cp -p ../$PUB.backup/CHECKSUMS.md5.cache .
  fi

  echo Generating file list...
  date >  FILELIST.TXT
  find . | grep -v '^./FILELIST.TXT' | xargs ls -ld >> FILELIST.TXT

  echo Generating md5...
  #\rm  CHECKSUMS.md5 # File is copied from rootfiles, to get the header
  find -L . -type f | grep -v '^./CHECKSUMS.md5' | ../md5sumcache.pl CHECKSUMS.md5.cache >> CHECKSUMS.md5
  echo Signing md5...
  echo $pass | ../sign.sh onlymd5
else
  if [[ -d ../$PUB.backup ]]
  then
    # Updating from backup:
    echo "Removing .orig files..."
    find . -type f -mmin -30 | xargs $CWD/remove_orig.pl
    echo "Updating timestamps from backup..."
    find . -type f -mmin -30 | xargs $CWD/samebackup.pl ../$PUB.backup
    echo "Removing backup..."
    time rm -rf ../$PUB.backup3
    mv ../$PUB.backup{2,3}
    mv ../$PUB.backup{,2}
  fi
  echo "Making backup..."
  time cp -a --reflink ../$PUB ../$PUB.backup

  echo "Linking packages repository..."
  ln -s ../$PACK/slackware_* .
  ln -s ../$PACK/commonpkgs*/common commonpkgs
  cp -p $CWD/scripts-$SUFF/installation_order.txt .
  cp -p $CWD/scripts-$SUFF/altversions.txt .
fi

echo "`date` - DONE."
