REM welcome window, first boot of Puppy.
REM (c) Copyright Barry Kauler 2011, bkhome.org
REM License GPL v3 (/usr/share/doc/legal)
REM 110412 first release.
REM 110414 tweak dimensions for non-English (thanks L18L and rodin.s).
REM 120630 1st bootup in PUPMODE=2, removed section about saving session.
REM 130204 include hug.bac, avoid needs recompiling when hug.so updated. (but much bigger!)
REM 130209 go back to hug.so.
REM 130710 Puppy default dpi has changed from 78 to 96.
REM 131210 modified for quirky6.
REM 140205 L18L modified for puppy and quirky and fatdog (anything in 1st word of /etc/issue)
REM 140207 /usr/sbin/delayedrun now tests active network and internet connection.
REM 140210 L18L choose from available languages, lang_names in new /usr/share/i18n/lang_names
REM 140211 display lang_names, language code in tooltip
REM 140212 bugfix
REM 140215 01micko: disable button_ic if connected; add slacko method which also enables test runs after 1stboot
REM 140715 01micko: tidy up for bacon-3.0.1
REM 151026 ping duckduckgo, instead of sourceforge

REM compile and create pot file: bacon -x -d /tmp welcome1stboot.bac

OPTION INTERNATIONAL TRUE
REM .po/.mo files have 'charset=UTF-8', so either set UTF-8 on in LANG variable, or do this...
SETENVIRON "OUTPUT_CHARSET", "UTF-8"

REM Final exe size: incl hug.bac: 178K, func list: 134K, shared hug.so: 47K...
REM INCLUDE "/usr/share/BaCon/hug.bac",ATTACH,BUTTON,CALLBACK,DISPLAY,FONT,HIDE,HUGOPTIONS,IMAGE,INIT,MARK,MSGDIALOG,PROPERTY,QUIT,SHOW,WINDOW
INCLUDE "/usr/share/BaCon/hug_imports.bac"
REM http://developer.gnome.org/gtk/stable/GtkWidget.html#gtk-widget-set-tooltip-text
IMPORT "gtk_widget_set_tooltip_text(long,char*)" FROM "libgtk-x11-2.0.so" TYPE void

ISSUE$=EXEC$("cat /etc/issue | head -n 1 | cut -d ' ' -f1")
SETENVIRON "ISSUE", ISSUE$

INIT

SUB ic_func()
 SYSTEM "/usr/sbin/connectwizard & "
END SUB

SUB setup_func()
 SYSTEM "/usr/sbin/wizardwizard & "
END SUB

SUB close_dlg(NUMBER widget)
 HIDE(widget)
END SUB


REM ============================ <language> ======================140210

FUNCTION available_languages$()
 GLOBAL langs$
 REM langs$=CONCAT$("en ", CHOP$(EXEC$("echo `find /usr/share/locale/*/LC_MESSAGES/welcome1stboot.mo | cut -d '/' -f 5`")))
 langs$="en"
 OPEN "/usr/share/locale/" FOR DIRECTORY AS textdomaindir
 REPEAT
  GETFILE oneLANG$ FROM textdomaindir
  IF FILEEXISTS(CONCAT$("/usr/share/locale/", oneLANG$, "/LC_MESSAGES/", ARGUMENT$, ".mo")) THEN langs$=CONCAT$(langs$, " ", oneLANG$)
 UNTIL ISFALSE(LEN(oneLANG$))
 CLOSE DIRECTORY textdomaindir
 RETURN langs$
END FUNCTION

SUB get_app_language
 OPTION BASE 1
 GLOBAL num_languages, language_win
 LOCAL num_cols
 no_LANG_NAME$ = "no name"
 GLOBAL language_checked[100]
 REM GLOBAL language_select[100]
 langs$=available_languages$ ()
 SPLIT langs$ BY " " TO language$ SIZE num_languages 
 
 IF num_languages > 1 THEN
  num_cols = 2 : IF num_languages > 15 THEN num_cols = 3
  HUGOPTIONS("FONT DejaVu Sans 20")
  win_width=640
  win_height=480
  language_win = WINDOW("?", win_width, win_height)
  check_width = FLOOR((win_width - 10) / num_cols)
  check_height = win_height / (1 + FLOOR(num_languages / num_cols))
  padding_x = (win_width - (num_cols * check_width))
  padding_y = 0

  n = 1 : row = 1 : col = 1
  WHILE n < num_languages + 1
   IF n = 1 THEN 
    LANG_NAME$="English"
   ELIF FILEEXISTS("/usr/share/i18n/lang_names") THEN
    'get language name in language of language
    LANG_NAME$=CHOP$(EXEC$(CONCAT$("x=`grep ^", language$[n],": /usr/share/i18n/lang_names || echo ", no_LANG_NAME$, "`;echo ${x#*:}")))
   ELSE
    LANG_NAME$=CONCAT$("Code: ", language$[n], "   ")
   END IF
   language_checked[n]=CHECK(LANG_NAME$, check_width, check_height) 
   ATTACH(language_win, language_checked[n], padding_x + (col -1) * check_width , padding_y + (row - 1) * check_height )
   gtk_widget_set_tooltip_text(language_checked[n],CONCAT$(language$[n], " ⇌ ", LANG_NAME$))
   CALLBACKX(language_checked[n], use_language, n)
   INCR n : INCR col
   IF col > num_cols THEN 
    INCR row : col = 1  
   END IF 
  WEND
  DISPLAY
 END IF 
END SUB

' restart this programm using LANGUAGE number n
SUB use_language(NUMBER widget, NUMBER n)
 SPLIT langs$ BY " " TO language$ SIZE num_languages 
 SYSTEM CONCAT$("echo -n `pidof ", ARGUMENT$, "`> /tmp/welcome")
 SYSTEM CONCAT$("LANGUAGE=", language$[n], " ", ARGUMENT$) 
 SYSTEM CONCAT$("kill `pidof ", ARGUMENT$)
 END
END SUB

IF GETENVIRON$("LANGUAGE") = "" THEN 
 get_app_language                                  : REM language choice screen
ELSE 
 SYSTEM "kill `cat /tmp/welcome`; rm /tmp/welcome" : REM kill language choice screen
END IF

REM ============================ </language> ======================140210


REM fix window font layout regardless of dpi...
REM 78 is the Xft.dpi in /root/.Xresources when I designed the layout... now 96...
HUGOPTIONS("BASEXFTDPI 96")
REM User may have GTK theme font size too big/small. set to required size...
HUGOPTIONS("FONT DejaVu Sans 10")

REM hack for russian, ru...
mycountry$=LEFT$(GETENVIRON$("LANG"),2)

REM space required for the session-save section, if not PUPMODE=2...
'vert_space_save=0
'IF FILETYPE("/initrd") EQ 2 THEN vert_space_save=112
vert_space_save=4



REM to disable decorations, need to hide window first...
HUGOPTIONS("WIDGET_SHOW 0")
REM reduced height by 112 pixels...
mainwin = WINDOW(INTL$("Welcome"),656,402+vert_space_save)
PROPERTY(mainwin,"decorated",FALSE)
HUGOPTIONS("WIDGET_SHOW 1")

image_logo=IMAGE("/usr/share/doc/puppylogo96.png",96,96)
ATTACH(mainwin,image_logo,68,5)

label_welcome=MARK(INTL$("Welcome,"),350,24)
REM left-align for single-line label...
PROPERTY(label_welcome,"xalign",0.0)
FONT(label_welcome,"DejaVu Sans Bold 20")
ATTACH(mainwin,label_welcome,188,20)

REM label_subhdr=MARK(INTL$("This is the first time you are running Quirky!"),462,24)
pot_msg$=INTL$("This is the first time you are running ${ISSUE}!")
label_subhdr=MARK(EXEC$("eval echo $(gettext 'welcome1stboot' 'This is the first time you are running ${ISSUE}!')"),462,24)

PROPERTY(label_subhdr,"xalign",0.0)
FONT(label_subhdr,"DejaVu Sans 14")
ATTACH(mainwin,label_subhdr,188,50)

button_x=BUTTON("X",24,24)
'image_x=IMAGE("/usr/share/pixmaps/puppy/cancel.svg",16,16)
'PROPERTY(button_x,"image",image_x)
gtk_widget_set_tooltip_text(button_x,INTL$("Close window"))
ATTACH(mainwin,button_x,625,6)
CALLBACK(button_x,QUIT)

button_ic=BUTTON("",58,56)
image_ic=IMAGE("/usr/local/lib/X11/pixmaps/connect48.png",48,48)
PROPERTY(button_ic,"image",image_ic)
gtk_widget_set_tooltip_text(button_ic,INTL$("Connect to Internet"))
ATTACH(mainwin,button_ic,584,116)

label_ic=MARK(INTL$("Internet connection"),250,24)
PROPERTY(label_ic,"xalign",0.0)
FONT(label_ic,"DejaVu Sans Bold 14")
ATTACH(mainwin,label_ic,20,108)

REM 140215 add slacko method which also enables test runs after 1stboot
'# check connection
'#shinobar
'LANG=C route | grep -q 'default[ ].*[ ]0\.0\.0\.0[ ]' && \
'grep -wq nameserver /etc/resolv.conf && \
'ping -c1 duckduckgo.com &>/dev/null && CONNECTED="yes" || CONNECTED=
SYSTEM "echo -n no > /tmp/CONNECTED; LANG=C route | grep -q 'default[ ].*[ ]0.0.0.0[ ]' && grep -wq nameserver /etc/resolv.conf && ping -c1 duckduckgo.com &>/dev/null && echo -n yes > /tmp/CONNECTED "
CONNECTED$=EXEC$("cat /tmp/CONNECTED")

IF FILEEXISTS("/tmp/delayedrun-internet-working") OR CONNECTED$ = "yes" THEN
 label_ic_body=MARK(INTL$("Congratulations, you are connected to the Internet. Experiment with mouse-over, left-click and right-click on the 'network' icon in the tray. Click on the 'connect' icon on the right (or on the desktop) if you need to reconfigure the Internet connection"),500,70)
 DISABLE(button_ic)
ELIF FILEEXISTS("/tmp/delayedrun-active-interfaces") THEN
 label_ic_body=MARK(INTL$("There is a working network interface, but you are not connected to the Internet. Click on the 'connect' icon on the right, or on left-side of the desktop (do NOT double-click), to setup the Internet connection. Note the network status icon in the tray."),500,70)
ELSE
 label_ic_body=MARK(INTL$("Click on the 'connect' button on the right, or icon at left of the screen (one click only -- do not double-click!). You will then see some buttons for choosing how you wish to connect to the Internet. It is easy..."),500,70)
ENDIF
PROPERTY(label_ic_body,"xalign",0.0)
PROPERTY(label_ic_body,"yalign",0.0)
PROPERTY(label_ic_body,"wrap",TRUE)
ATTACH(mainwin,label_ic_body,30,130)

CALLBACK(button_ic,ic_func)
label_c=MARK(INTL$("connect"),82,16)
FONT(label_c,"DejaVu Sans Bold 10")
PROPERTY(label_c,"xalign",0.5)
ATTACH(mainwin,label_c,571,172)

REM bad hack for russian translation...
IF mycountry$ = "ru" THEN
 vert1=186
 vert2=208
ELSE
 vert1=202
 vert2=224
END IF

label_help=MARK(INTL$("I need help!"),280,24)
PROPERTY(label_help,"xalign",0.0)
FONT(label_help,"DejaVu Sans Bold 14")
ATTACH(mainwin,label_help,20,vert1)

REM label_help_body=MARK(INTL$("Explore the 'Menu' -- see bottom-left of screen. Lots of local help is available -- select 'Help' in the menu. The local Help page also has the Release Notes for this version of Quirky -- well worth checking out! When you get online, the web browser home page has many more links."),628,70)
pot_msg$=INTL$("Explore the Menu -- see bottom-left of screen. Lots of local help is available -- select Help in the menu. The local Help page also has the Release Notes for this version of ${ISSUE}-- well worth checking out! When you get online, the web browser home page has many more links.")
label_help_body=MARK(EXEC$("eval echo $(gettext 'welcome1stboot' 'Explore the Menu -- see bottom-left of screen. Lots of local help is available -- select Help in the menu. The local Help page also has the Release Notes for this version of ${ISSUE}-- well worth checking out! When you get online, the web browser home page has many more links.')"),628,70)

PROPERTY(label_help_body,"xalign",0.0)
PROPERTY(label_help_body,"yalign",0.0)
PROPERTY(label_help_body,"wrap",TRUE)
ATTACH(mainwin,label_help_body,30,vert2)

image_tray=IMAGE("/usr/share/doc/tray.png",178,28)
ATTACH(mainwin,image_tray,40,282)
label_tray=MARK(INTL$("...mouse-over and click the tray applets, very helpful!"),431,16)
PROPERTY(label_tray,"xalign",0.0)
ATTACH(mainwin,label_tray,222,286)

label_setup=MARK(INTL$("Setup"),280,24)
PROPERTY(label_setup,"xalign",0.0)
FONT(label_setup,"DejaVu Sans Bold 14")
ATTACH(mainwin,label_setup,20,320+vert_space_save)
label_setup_body=MARK(INTL$("Want to install an upgraded video driver? Country localization? Printing? Sound? Mouse? Keyboard? Click here (or 'setup' icon at top of screen):"),500,70)
PROPERTY(label_setup_body,"xalign",0.0)
PROPERTY(label_setup_body,"yalign",0.0)
PROPERTY(label_setup_body,"wrap",TRUE)
ATTACH(mainwin,label_setup_body,30,342+vert_space_save)

button_setup=BUTTON("",58,56)
image_setup=IMAGE("/usr/local/lib/X11/pixmaps/configuration48.png",48,48)
PROPERTY(button_setup,"image",image_setup)

REM gtk_widget_set_tooltip_text(button_setup,INTL$("Setup Quirky"))
pot_msg$=INTL$("Setup ${ISSUE}")
gtk_widget_set_tooltip_text(button_setup,EXEC$("eval echo -n $(gettext 'welcome1stboot' 'Setup ${ISSUE}')"))

ATTACH(mainwin,button_setup,584,323+vert_space_save)
CALLBACK(button_setup,setup_func)
label_set=MARK(INTL$("setup"),87,16)
FONT(label_set,"DejaVu Sans Bold 10")
PROPERTY(label_set,"xalign",0.5)
ATTACH(mainwin,label_set,568,379+vert_space_save)

SHOW(mainwin)
DISPLAY
REM the end 
