#!/bin/sh
#(c) Copyright Barry Kauler 2012. License: GPL3 (/usr/share/doc/legal)
#this script called from /usr/sbin/quicksetup, /usr/bin/wmreboot, wmpoweroff.
#passed in params: $NEWLANG
#120401 change logic to modify init (see also quicksetup).
#121124 ensure that all [ ] are escaped in initrd translation-file. see also /usr/sbin/fixdesk, fixmenus.
#121125 revert 121124.
#121126 restore 121124, plus escape '.' chars in regex.

export TEXTDOMAIN=lang2initrd
export OUTPUT_CHARSET=UTF-8
. gettext.sh

. /etc/rc.d/PUPSTATE

NEWLANG=$1
LANG1="`echo -n $NEWLANG | cut -f 1 -d '_'`"  #ex: de
LANG12="`echo -n $NEWLANG | cut -f 1 -d '.'`" #ex: de_DE

#various reasons for not doing it...
case $PDEV1 in
 sr*) exit 1 ;;
esac
[ "$DEV1FS" = "iso9660" ] && exit 1
if [ "$PLANG" ];then #120401
 PLANG1="${PLANG%_*}" #defined in PUPSTATE by initrd. ex: de
 PLANG12="${PLANG%.*}" #ex: de_DE
 [ "$PLANG" = "$NEWLANG" ] && exit 1 #initrd already in this language.
else
 [ "$LANG1" = "en" ] && exit 1
fi
[ ! -d /initrd ] && exit 1 #dir exists, then puppy booted up with an initrd.
INITRANS=''
[ -f /usr/share/sss/initrd_strings/initrd_strings.${LANG12} ] && INITRANS="/usr/share/sss/initrd_strings/initrd_strings.${LANG12}"
[ ! "$INITRANS" ] && [ -f /usr/share/sss/initrd_strings/initrd_strings.${LANG1} ] && INITRANS="/usr/share/sss/initrd_strings/initrd_strings.${LANG1}"
[ "$INITRANS" ] && TRANSLANG="`echo -n "$INITRANS" | rev | cut -f 1 -d '.' | rev`" #120401
[ "$LANG1" != "en" ] && [ ! "$INITRANS" ] && exit 1 #120401
[ "$LANG1" = "en" ] && TRANSLANG='en' #120401

if [ "$DISPLAY" ];then
 pupdialog --background '#FF80FF' --colors --backtitle "$(gettext 'Early boot messages')" --yesno "$(gettext 'Do you want to translate the early boot messages? These are in what is known as the initrd or initramfs, when the Linux operating system (kernel) is first loading. It is usually good to answer yes here.')

$(gettext 'They will be translated to:') ${NEWLANG}" 0 0
else
 dialog --yesno "$(gettext 'Do you want to translate the early boot messages? These are in what is known as the initrd or initramfs, when the Linux operating system (kernel) is first loading. It is usually good to answer yes here.')

$(gettext 'They will be translated to:') ${NEWLANG}" 0 0 >/dev/console
fi
if [ $? -ne 0 ];then
 echo "DECLINED ${DISTRO_IDSTRING}" >> /var/local/lang2initrd.log #variable in /etc/DISTRO_SPECS
 exit 2
fi

#locate the initrd.gz and open it up...
#try and figure out where the initrd.gz is... (init script doesn't know)
#this is an assumption, GRUB can specify elsewhere... these variables are in PUPSTATE...
INITRDPART="$PDEV1"
INITRDFS="$DEV1FS"
INITRDSPEC="${PSUBDIR}/initrd.gz"
iniPTN="^/dev/${INITRDPART} "
INITRDMNT="`mount | grep "$iniPTN" | cut -f 3 -d ' '`"
if [ ! "$INITRDMNT" ];then
 mkdir -p /mnt/$INITRDPART
 mount -t $INITRDFS /dev/$INITRDPART /mnt/$INITRDPART
 if [ $? -eq 0 ];then
  INITRDMNT="/mnt/$INITRDPART"
  FLAGINIUNMOUNT='yes'
 fi
fi
if [ "$INITRDMNT" ];then
 if [ -f ${INITRDMNT}${INITRDSPEC} ];then
  cp -a -f ${INITRDMNT}${INITRDSPEC} /root
  CURRDIR="`pwd`"
  [ -d /root/initrd-tree ] && rm -rf /root/initrd-tree
  [ -f /root/initrd ] && rm -f /root/initrd
  mkdir /root/initrd-tree
  cd /root
  gunzip initrd.gz
  if [ $? -eq 0 ];then
   cd initrd-tree
   cat ../initrd | cpio -i -d -m
   if [ "$TRANSLANG" = "en" ];then #120401
    rm -f PUPPYLANG
    rm -f PUPPYKEYMAP
   else
    echo -n "$NEWLANG" > PUPPYLANG #file /PUPPYLANG in initrd sets default LANG.
    [ -f /etc/keymap ] && cat /etc/keymap > PUPPYKEYMAP #120216 also useful in initrd.
    #might need to update the translation files in initrd also (was done by 3builddistro)...
    INITRDTRANSFILES="`find /usr/share/sss/initrd_strings -mindepth 1 -maxdepth 1 -type f -name 'initrd_strings.*'`"
    if [ "$INITRDTRANSFILES" ];then
     for ATRANS in $INITRDTRANSFILES
     do
      ENDBIT="`echo -n "$ATRANS" | rev | cut -f 1 -d '.' | rev`" #ex: de (from initrd_strings.de)
      mkdir -p locale/${ENDBIT}
      #only supporting translating the init script for now...
      sPTN="/^\[init\]/,/^$/p" #this is a multi-line block find expression.
      CODEBLOCK="`sed -n "$sPTN" ${ATRANS} | sed -e '/^#/d' -e '/%%/d' -e '/^$/d' -e '/^\[/d'`" #extracts just the relevant block of lines.
      echo "$CODEBLOCK" > locale/${ENDBIT}/init #translation file, read by /init script in initrd.
      #121124 ensure that all [ ] are escaped... 121125 revert... 121126 restore, plus escape '.' chars...
      sed -i -e 's%\[%\\[%g' -e 's$\]$\\]$g' -e 's%\\\\\[%\\[%g' -e 's%\\\\\]%\\]%g' locale/${ENDBIT}/init
      sed -i -e 's%\.%\\.%g' -e 's%\\\\\.%\\.%g' locale/${ENDBIT}/init #note: 2nd ptn gets rid of prior escape char, so there remains just one.
     done
    fi
   fi
   sync
   #now put the initrd.gz back together again...
   rm -f ../initrd
   find . | cpio -o -H newc | gzip -9 > ../initrd.gz
   sync
   cd ..
   rm -rf initrd-tree
   cp -a -f initrd.gz ${INITRDMNT}${INITRDSPEC}
   sync
   rm -f initrd.gz
   [ "$FLAGINIUNMOUNT" = "yes" ] && umount ${INITRDMNT}
   FLAGINISUCCESS='yes'
  fi
  cd $CURRDIR
 fi
fi
if [ "$FLAGINISUCCESS" = "yes" ];then
 INIMSG="`eval_gettext \"The file \Zbinitrd.gz\ZB (at \\\${INITRDSPEC} on \\\${INITRDPART}) has been successfully translated to language \Zb\\\${NEWLANG}\ZB. You will see the effect at the next boot. Warning: Puppy thinks that this is the correct initrd.gz used at bootup, although it is possible that a boot manager such as GRUB can specify an alternate location, which means that the wrong initrd.gz file has been translated!\"`"
 BACKCOLOR='#80FF80'
 #keep a record that have done it...
 echo "SUCCESS ${DISTRO_IDSTRING}" >> /var/local/lang2initrd.log #variable in /etc/DISTRO_SPECS
else
 INIMSG="`eval_gettext \"Sorry, something went wrong, was unable to translate file \Zbinitrd.gz\ZB (at \\\${INITRDSPEC} on \\\${INITRDPART}). A possible reason for this is that initrd.gz is located somewhere else, specified by a boot manager such as GRUB.\"`"
 BACKCOLOR='#FF8080'
fi
if [ $DISPLAY ];then
 pupdialog --background ${BACKCOLOR} --colors --backtitle "$(gettext 'Initrd translation')" --msgbox "${INIMSG}" 0 0
else
 dialog --colors --msgbox "${INIMSG}" 0 0 >/dev/console
fi

###END###
