#!/bin/bash

# Usage
#
#   sh make_people_list.sh english-index-file norwegian-index-file < people_list.txt 
#
# The syntax of the input-file is
#   
#   Title;Tittle
#   name1; position; stilling; affiliation; picture-link; home-page
#   name2; position; stilling; affiliation; picture-link; home-page
#   ....
#
# eg.
#
# The title is the title of the people list, eg. "Leadership and Administration"
#
# The output-file is the name of the generated file, e.g. index_test.html to replace the old index.html after inspection

en=$1
no=$2

IFS=''
read line
title=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 1)
tittel=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 2)


#print header
echo '{                                    '   | tee    $no $en
echo '  "resourcetype": "frontpage-two",   '   | tee -a $no $en
echo '  "properties": {                    '   | tee -a $no $en
echo '    "title": "'$title'",             '   | tee -a     $en
echo '    "title": "'$tittel'",            '   | tee -a $no
echo '    "hideDocumentTitle":false,       '   | tee -a $no $en
echo '    "content": [                     '   | tee -a $no $en
echo '     {                               '   | tee -a $no $en
echo '      "color": "white",              '   | tee -a $no $en
echo '      "hidden": false,               '   | tee -a $no $en
echo '      "layout": 1,                   '   | tee -a $no $en
echo '      "boxes": [                     '   | tee -a $no $en

DONE=false
read line || DONE=true
LEFT=true
until $DONE
do
  name=$(echo $line | sed 's/; */;/g' | cut -d ';' -f 1)
  pos=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 2)
  stilling=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 3)
  aff=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 4)
  pic=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 5)
  www=$( echo $line | sed 's/; */;/g' | cut -d ';' -f 6)
  #Make person entry
  echo '          {'                                                | tee -a $no $en
  echo '            "boxContent": "<p><a href=\"'$www'\">'$name'</a></p> \\n\\n<p>'$pos' <br />\\n'$aff'</p>\\n"'                                                                           | tee -a     $en
  echo '            "boxContent": "<p><a href=\"'$www'\">'$name'</a></p> \\n\\n<p>'$stilling' <br />\\n'$aff'</p>\\n"'                                                                      | tee -a $no
  echo '            "boxType": "person-image-box",'                 | tee -a $no $en
  echo '            "boxCssColor": "white-box",'                    | tee -a $no $en
  echo '            "boxPicture": "'$pic'",'                        | tee -a $no $en
  echo '            "boxPictureAlt": "portrettbilde",'              | tee -a $no $en
  if [ "$LEFT" = true ];then
    echo '            "boxCssSizeAndPlacement": "half-box-left",'   | tee -a $no $en
    LEFT=false
  else
    echo '            "boxCssSizeAndPlacement": "half-box-right",'  | tee -a $no $en
    LEFT=true
  fi
  echo '            "boxTitle": "",'                                | tee -a $no $en
  echo '          },'                                               | tee -a $no $en
  read line || DONE=true
  if [ "$DONE" = true ];then 
    if [ ! "$LEFT" = true ];then
      echo '          {'                                            | tee -a $no $en
      echo '            "boxTitle": "",'                            | tee -a $no $en
      echo '            "boxCssSizeAndPlacement": "half-box-right",'| tee -a $no $en
      echo '            "boxContent": ""'                           | tee -a $no $en
      echo '          },'                                           | tee -a $no $en
    fi
  fi
  
done 
echo '    ],'                                                | tee -a $no $en
echo '   }'                                                  | tee -a $no $en
echo '  ],'                                                  | tee -a $no $en
echo ' }'                                                    | tee -a $no $en
echo '}'                                                     | tee -a $no $en
