#!/bin/bash

# This file defines functions used by make.sh

# Safety check: Only define these functions if WEBDIR and LNXDIR defined.

if [ -z "$WEBDIR" ] || [ -z "$LNXDIR" ]
then
  echo set WEBDIR and LNXDIR
  exit 1
fi

# Update Documentation directory

function update_documentation_dir()
{
  rm -rf "$WEBDIR"/Documentation/
  cp -a "$LNXDIR"/Documentation "$WEBDIR"/Documentation
  # Make the change directory local to this function
  (
    cd $WEBDIR
    make/docdiridx.py
  )
}

# Clone or update mercurial Linux kernel repository from kernel.org

function get_git()
{
  # Make the chdir local to this function
  (
    if [ ! -d "$LNXDIR" ]
    then
      echo "Clone Linux kernel repository from kernel.org"

      mkdir -p "$LNXDIR" &&
      cd "$LNXDIR" &&
      # hg clone http://kernel.org/hg/linux-2.6
      git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 .git --bare &&
      git checkout -f master
    else
      # Update kernel git repository.
      cd "$LNXDIR" &&
      echo "Update Linux kernel from kernel.org" &&
      git clean -fdx && git checkout -f master && git pull
    fi
    [ $? -ne 0 ] && exit 1

    echo "Linux version: $(git describe --abbrev=0)"
  )
}

function make_htmldocs()
{
  # Make the chdir local
  (
    cd "$LNXDIR" &&
    make -j 4 htmldocs
  )
}

function make_htmlnochunks()
{
  # Make the chdir local
  (
    cd "$LNXDIR" &&
    # Probably redundant, but just in case...
    make -j 4 xmldocs &&
    cd Documentation/DocBook &&
    for i in *.xml
    do
      echo "Converting $i"
      xmlto --skip-validation xhtml-nochunks $i -o xhtml-nochunks
    done
  )
}

function install_htmldocs()
{
  (
    rm -rf $WEBDIR/htmldocs
    cd "$LNXDIR"/Documentation/DocBook &&
    # Install nochunks versions
    mv xhtml-nochunks "$WEBDIR"/htmldocs &&

    head -n 2 index.html > "$WEBDIR"/htmldocs/index.html &&
    echo "<ul>" >> "$WEBDIR"/htmldocs/index.html

    [ $? -ne 0 ] && exit 1

    # Install broken-up versions and index.html
    for i in $(ls -p | sed -n 's@/$@@p')
    do
      mv $i "$WEBDIR"/htmldocs &&
      echo "<li>(<a href=\"$i\">chunks</a>) (<a href=\"${i}.html\">nochunks</a>) " \
        >> "$WEBDIR"/htmldocs/index.html &&
      sed -nr 's@.*<title>(.*)</title>.*@\1@p' ${i}.xml | head -n 1 >> \
        "$WEBDIR"/htmldocs/index.html &&
      echo "</li>" >> "$WEBDIR"/htmldocs/index.html

      [ $? -ne 0 ] && exit 1
    done

    cd "$WEBDIR"/htmldocs &&
    echo "</ul>" >> index.html &&
    # Work around a bug in xmlto: it puts an incorrect code page into the
    # converted html documents, which produces line noise in the output.
    sed -i -e 's/.*ISO-8859-1.*//' $(find . -name "*.html")
  )
}

function update_master()
{
  "$WEBDIR"/make/indexsections.py $WEBDIR/master.idx > $WEBDIR/index.html
}

function htmlize_menuconfig()
{
  (
    cd "$LNXDIR" &&
    LNXTAG="$(git describe --abbrev=0)"

    rm -rf "$WEBDIR"/menuconfig/
    mkdir -p "$WEBDIR"/menuconfig &&
    echo "<html><title>Menuconfig docs for Linux $LNXTAG</title><body><ul>" > \
      "$WEBDIR"/menuconfig/index.html &&
    for i in $(echo arch/*/Kconfig | sort)
    do
      echo Converting "$i" &&
      ARCH=$(echo $i | sed -r 's@.*/(.*)/.*@\1@') &&
      echo "<li><a href=$ARCH.html>$ARCH</a></li>" >> \
        "$WEBDIR"/menuconfig/index.html &&
      "$WEBDIR"/make/menuconfig2html.py $i "$WEBDIR/menuconfig" \
        "$LNXTAG" > "$WEBDIR/menuconfig/$ARCH.html"

      [ $? -eq 0 ] || exit 1
    done
  ) &&
  echo "</ul></body></html>" >> "$WEBDIR"/menuconfig/index.html
}

function mirror_files()
{
  (
    cd "$WEBDIR" &&
    make/mirror.py
    [ $? -ne 0 ] && exit 1

    cat mirror/ols2002.pdf.gz | gunzip > mirror/ols2002.pdf &&
    if [ -z "$(find mirror/ols2001 -name "*.pdf")" ]
    then
      mkdir -p mirror/ols2001 &&
      cd mirror/ols2001 &&
      wget -r -l 1 -nd http://lwn.net/2001/features/OLS/pdf/pdf/ &&
      cd ../..

      [ $? -ne 0 ] && exit 1
    fi

    if [ -z "$(find ols/2010 -name "*.pdf")" ]
    then
      echo "Split ols files"
      mkdir -p ols/{1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010} &&
      cp mirror/ols2001/*.pdf ols/2001 &&
      make/splitols.py mirror ols

      [ $? -ne 0 ] && exit 1
    fi
  )
}

function find_readmes()
{
  (
    rm -rf "$WEBDIR"/readme
    mkdir "$WEBDIR"/readme &&
    cd "$LNXDIR" &&
    make help > "$WEBDIR"/makehelp.txt &&
    for i in $(find . -name "*[Rr][Ee][Aa][Dd][Mm][Ee]*" | grep -v "Documentation/")
    do
      OUT=$(echo $i | sed -e 's@\./@@' -e 's@/@-@g')
      cp $i "$WEBDIR"/readme/"$(echo $i | sed -e 's@\./@@' -e 's@/@-@g')"
    done
  )
}

function findrfc()
{
  (
    > "$WEBDIR"/temp-linuxrfc.txt &&
    cd "$LNXDIR" || exit 1
    echo -n "Search for RFC references"
    x=0
    # Iterate through *.c and *.h in the Linux source code.
    for i in `find "$LNXDIR" -name "*.[hc]"`
    do
      # Progress indicator
      x=$[$x+1]
      if [ $x -gt 100 ]
      then
        echo -n .
        x=0
      fi
      # Grep each source file for mention of an RFC, filtering out the more
      # obvious false positives.
      for j in $(grep -io "rfc *[1-9][0-9]*" $i | grep -v "  " | grep -v "@rfc822\.org" | sed -r 's/[Rr][Ff][Cc] *([0-9]*)/rfc\1/g')
      do
        echo $i:$j >> "$WEBDIR"/temp-linuxrfc.txt
      done
    done
  ) &&

  # Process the temporary directory into an html file.

  echo Converting to html...
  (
    cd "$WEBDIR" || exit 1
    echo "<html><head><title>RFCs used by the Linux kernel</title></head>"
    echo "<ul>"
    for i in $(sed 's/.*://' temp-linuxrfc.txt | sort -u | sort -t c -k 2,2 -n)
    do
      echo '<li><h2><a href="http://tools.ietf.org/html/'$i'">'$i'</a></h2><p>'
      for j in $(sed -rn 's@\'"$LNXDIR"/'(.*):'$i'$@\1@p' temp-linuxrfc.txt)
      do
        echo '<a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob_plain;f='"$j"';hb=HEAD">'$j'</a>'
      done
      echo "</p></li>"
    done
    echo "</ul></html>"
  ) > "$WEBDIR"/rfc-linux.html &&

  # Remove the temporary file
  rm temp-linuxrfc.txt
}

# Set up for out of tree build of Linux.

function setuplinuxtemp()
{
  # Stub this out for now
  return 0

  rm -rf "$LNXDIR"/temp &&
  mkdir "$LNXDIR"/temp &&
  # Building out of tree conflicts with patching the files, so copy.
  cp -lr "$LNXDIR"/hg/* "$LNXDIR"/temp &&
  (
    cd "$LNXDIR"/temp
    # Apply patches
    for i in "$WEBDIR"/make/patches/*.patch
    do
      echo $i
      if [ -f "$i" ]
      then
        patch -p1 -i $i || exit 1
      fi
    done

    # Do setup we'll need later.
    make allnoconfig > /dev/null &&
    make -j 4 xmldocs
  )
}

function wait4background()
{
  # Wait for background task to finish
  while [ $(jobs | wc -l) -ne 0 ]
  do
    sleep 1
    # Without this next line, bash never notices a change in the number of jobs.
    # Bug noticed in Ubuntu 7.04
    jobs > /dev/null
  done
}

