REM setup proxy server for wget
REM Barry Kauler, my first BaCon GTK program using HUG
REM (c) Copyright Barry Kauler Nov 2010, license GPL v3 /usr/share/doc/legal
REM 20110314 internationalization and dpi improvements in BaCon
REM 20110416 label alignment improvements.

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 use shared /usr/lib/hug.so instead of including hug.bac...
INCLUDE "/usr/share/BaCon/hug_imports.bac"
INIT

REM fix window font layout regardless of dpi...
REM 78 is the Xft.dpi in /root/.Xresources when I designed the layout...
HUGOPTIONS("BASEXFTDPI 78")

REM User may have GTK theme font size too big/small. set to required size...
HUGOPTIONS("FONT DejaVu Sans 12")
font_mono$="Monospace 13"

REM read the previous setting...
proxy_flag=0
http_proxy$="http://foo.org:8080"
ftp_proxy$="ftp://foo.org:8080"
username$=""
password$=""
IF FILEEXISTS("/etc/profile.d/http_proxy") THEN
 REM env vars may have username/password in them, split up...
 http_proxy$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '")
 username$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '")
 password$=EXEC$("cat /etc/profile.d/http_proxy | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '")
 proxy_flag=1
ENDIF
IF FILEEXISTS("/etc/profile.d/ftp_proxy") THEN
 REM env vars may have username/password in them, split up...
 ftp_proxy$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | sed -e 's%//.*@%//%' | tr '\n' ' ' | tr -d ' '")
 username$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | grep '@' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%:.*%%' | tr '\n' ' ' | tr -d ' '")
 password$=EXEC$("cat /etc/profile.d/ftp_proxy | cut -f 2 -d '=' | grep ':' | sed -e 's%^.*//%%' -e 's%@.*$%%' -e 's%^.*:%%' | tr '\n' ' ' | tr -d ' '")
 proxy_flag=1
ENDIF

SUB check1_callback
 LOCAL status
 status=GET(check1)
 IF status = 1 THEN
  ENABLE(httpproxy)
  ENABLE(ftpproxy)
  ENABLE(username)
  ENABLE(password1)
  ENABLE(password2)
 ELSE
  DISABLE(httpproxy)
  DISABLE(ftpproxy)
  DISABLE(username)
  DISABLE(password1)
  DISABLE(password2)
 ENDIF
ENDSUB

REM update the environment variables...
SUB ok_callback
 LOCAL pass1$,pass2$

 status2=GET(check1)
 IF status2 = 0 THEN
  SYSTEM "rm -f /etc/profile.d/http_proxy"
  SYSTEM "rm -f /etc/profile.d/ftp_proxy"
  HIDE(mainwin)
  SHOW(no_dlg1)
  GOTO label1
 ENDIF

 httpproxy$=GRAB$(httpproxy)
 ftpproxy$=GRAB$(ftpproxy)
 username$=GRAB$(username)
 pass1$=GRAB$(password1)
 pass2$=GRAB$(password2)
 IF pass1$ != pass2$ THEN
  SHOW(err_dlg1)
 ELSE

  IF httpproxy$ != "" THEN
   REM take off the http:// part...
   protocol1$=LEFT$(httpproxy$,5)
   IF protocol1$ = "http:" THEN
    protocol2$="http://"
    httpproxy1$=MID$(httpproxy$,8)
   ELSE
    IF protocol1$ = "https" THEN
     protocol2$="https://"
     httpproxy1$=MID$(httpproxy$,9)
    ELSE
     protocol2$="http://"
     httpproxy1$=httpproxy$
    ENDIF
   ENDIF
   IF username$ = "" THEN
    httpproxy2$=httpproxy1$
   ELSE
    IF pass1$ = "" THEN
     httpproxy2$=CONCAT$(username$,"@",httpproxy1$)
    ELSE
     httpproxy2$=CONCAT$(username$,":",pass1$,"@",httpproxy1$)
    ENDIF
   ENDIF
   http_proxy_profile$=CONCAT$("export http_proxy=",protocol2$,httpproxy2$)
   OPEN "/etc/profile.d/http_proxy" FOR WRITING AS handle
    WRITELN http_proxy_profile$ TO handle
   CLOSE FILE handle
  ENDIF

  REM exactly same thing for ftp...
  IF ftpproxy$ != "" THEN
   REM take off the ftp:// part...
   protocol1$=LEFT$(ftpproxy$,4)
   IF protocol1$ = "ftp:" THEN
    protocol2$="ftp://"
    ftpproxy1$=MID$(ftpproxy$,7)
   ELSE
    IF protocol1$ = "ftps" THEN
     protocol2$="ftps://"
     ftpproxy1$=MID$(ftpproxy$,8)
    ELSE
     protocol2$="ftp://"
     ftpproxy1$=ftpproxy$
    ENDIF
   ENDIF
   IF username$ = "" THEN
    ftpproxy2$=ftpproxy1$
   ELSE
    IF pass1$ = "" THEN
     ftpproxy2$=CONCAT$(username$,"@",ftpproxy1$)
    ELSE
     ftpproxy2$=CONCAT$(username$,":",pass1$,"@",ftpproxy1$)
    ENDIF
   ENDIF
   ftp_proxy_profile$=CONCAT$("export ftp_proxy=",protocol2$,ftpproxy2$)
   OPEN "/etc/profile.d/ftp_proxy" FOR WRITING AS handle
    WRITELN ftp_proxy_profile$ TO handle
   CLOSE FILE handle
  ENDIF

  HIDE(mainwin)
  SHOW(yes_dlg1)
 ENDIF
LABEL label1
ENDSUB

REM callback when click close-button in info boxes...
SUB close_dialog(NUMBER widget)
 HIDE(widget)
 FOCUS(password1)
ENDSUB

mainwin = WINDOW(INTL$("Proxy server setup"), 425, 285)

image1=IMAGE("/usr/local/lib/X11/pixmaps/www48.png",48,48)
ATTACH(mainwin,image1,10,10)

label_hdr1=MARK(INTL$("If you connect to the Internet through a proxy"),370,15)
ATTACH(mainwin,label_hdr1,58,10)
label_hdr2=MARK(INTL$("server, tick the checkbox and fill in the fields"),370,15)
ATTACH(mainwin,label_hdr2,58,25)
label_hdr3=MARK(INTL$("(leave Username/Password blank if not needed)"),370,15)
ATTACH(mainwin,label_hdr3,58,40)

check1 = CHECK(INTL$("Enable Internet connection through proxy server"),350,20)
ATTACH(mainwin,check1,20,70)

label_http = MARK("HTTP:",100,20)
REM param 0.0 means left-align label, 0.5 centre-align, 1.0 right-align...
PROPERTY(label_http,"xalign",1.0)
ATTACH(mainwin, label_http, 10, 100)
httpproxy = ENTRY(http_proxy$,280,20)
ATTACH(mainwin, httpproxy, 120, 100)
FONT(httpproxy,font_mono$)

label_ftp = MARK("FTP:",100,20)
PROPERTY(label_ftp,"xalign",1.0)
ATTACH(mainwin, label_ftp, 10, 130)
ftpproxy = ENTRY(ftp_proxy$,280,20)
ATTACH(mainwin, ftpproxy, 120, 130)
FONT(ftpproxy,font_mono$)

label_user=MARK(INTL$("Username:"),100,20)
PROPERTY(label_user,"xalign",1.0)
ATTACH(mainwin,label_user,10,160)
username = ENTRY(username$,280,20)
ATTACH(mainwin, username, 120, 160)
FONT(username,font_mono$)

label_pass=MARK(INTL$("Password:"),100,20)
PROPERTY(label_pass,"xalign",1.0)
ATTACH(mainwin,label_pass,10,190)
password1=PASSWORD(150,20)
ATTACH(mainwin,password1,120,190)
TEXT(password1,password$)
FONT(password1,font_mono$)

label_pass2=MARK(INTL$("Enter password again:"),170,20)
ATTACH(mainwin,label_pass2,10,220)
password2=PASSWORD(150,20)
ATTACH(mainwin,password2,180,220)
TEXT(password2,password$)
FONT(password2,font_mono$)

ok_btn = BUTTON(INTL$("OKAY"), 65, 25)
ATTACH(mainwin, ok_btn, 240, 250)
cancel_btn = BUTTON(INTL$("Cancel"), 65, 25)
ATTACH(mainwin, cancel_btn, 320, 250)

REM define some info boxes...
err_dlg1=MSGDIALOG(INTL$("Sorry, the password entries are not the same"),400,100,3,2)
yes_dlg1=MSGDIALOG(INTL$("Great, you have modified the proxy server settings. Note, you can see the result in files /etc/profile.d/http_profile and /etc/profile.d/ftp_profile. However, MOST IMPORTANT, you must reboot for this to take effect"),500,160,0,1)
no_dlg1=MSGDIALOG(INTL$("You have chosen NOT to use a proxy server. Note, if you previously did have a proxy server enabled, you MUST REBOOT for the change to take effect"),500,140,0,1)

REM Define the callbacks
CALLBACK(ok_btn,ok_callback)
CALLBACK(cancel_btn,QUIT)
CALLBACK(check1,check1_callback)
CALLBACK(err_dlg1,close_dialog)
CALLBACK(yes_dlg1,QUIT)
CALLBACK(no_dlg1,QUIT)

REM tick or untick the checkbox...
SET(check1,1)
SET(check1,proxy_flag)

REM endless gtk loop...
DISPLAY